echoing the date modified on top of the files - php

can I echo the date or date modified? on top of every csv file? I had an idea on how to do that but that didn't work out.
<?php
$arrFiles = glob("../Csv_folder/*.csv");
$arrSortedFiles = array();
foreach($arrFiles as $strFileName) {
$arrSortedFiles[$strFileName] = filemtime($strFileName);
}
arsort($arrSortedFiles);
foreach(array_keys($arrSortedFiles) as $strFileName)
{
$file_handle = fopen($strFileName, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>';
}
fclose($file_handle);
}
?>

Use filemtime
Returns the time the file was last modified, or FALSE on failure. The time is returned as a Unix timestamp, which is suitable for the date() function.
Just before you start echoing out your file, you can do
echo "Time: " . date ("F d Y H:i:s.", filemtime($strFileName));
And add a line break or a table row etc. as required.
Edit
Replace your echo with this one
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td><td>Time: '.date ("F d Y H:i:s.", filemtime($strFileName)).'</td></tr>';
Edit 2
Replace your loop with this
foreach(array_keys($arrSortedFiles) as $strFileName)
{
$file_handle = fopen($strFileName, "r");
echo "<tr><td colspan='5'></td><td>".date ("F d Y H:i:s.", filemtime($strFileName))."</td></tr>";
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>';
}
fclose($file_handle);
}

Related

Stop table from being generated IF less then X

I have a table that is generated upon REST API, I generate for the first 50 in the result. My issue is if the response from REST is less then 50 the table still make the table for 50, just with empty rows on the X amounts that is not available in the response.
So e.g I get result of 47 , then my code generates for 50 still only so last three rows is empty. How do I avoid this ?
for($x=0;$x<50;$x++)
echo "<table><tr><td>" . "<img style='width:200px; height:150px;' src='" . $imagehost . $newImgUrl = preg_replace($pattern, $replacement, $hotelSummary[$x]['thumbNailUrl']) . "'/>" . "</td><td>" . $hotelSummary[$x]['name'] . "</td><td>" . $hotelSummary[$x]['hotelId'] . "</td><td>" . $hotelSummary[$x]['city'] . "</td><td>" . $hotelSummary[$x]['RoomRateDetailsList']['RoomRateDetails']['RateInfos']['RateInfo']['ChargeableRateInfo']['#total'] . "</td><td><button type='button'>Hotel Info</button><td><a href='" . $hotelSummary[$x]['deepLink'] . "' ><button type='button'>Book Now</button></a></td></tr></table>";
So if the response is less then 50 only create for the x amount found, and if the amount is less then 1 echo out e.g No results matches your search.
Can I implement an if sentence in this or how should I do this ?
Don't compare
$x<50
but
$x < $responseFromRest && $x < 50
You could add an if to check to see if the sizeof your response is bigger than $x, if so, echo your line else break. Example:
$reponse = $data;
for($x=0;$x<50;$x++) {
if($x > sizeof($respnse)) {
break;
}
echo "<table><tr><td>" . "<img style='width:200px; height:150px;' src='" . $imagehost . $newImgUrl = preg_replace($pattern, $replacement, $hotelSummary[$x]['thumbNailUrl']) . "'/>" . "</td><td>" . $hotelSummary[$x]['name'] . "</td><td>" . $hotelSummary[$x]['hotelId'] . "</td><td>" . $hotelSummary[$x]['city'] . "</td><td>" . $hotelSummary[$x]['RoomRateDetailsList']['RoomRateDetails']['RateInfos']['RateInfo']['ChargeableRateInfo']['#total'] . "</td><td><button type='button'>Hotel Info</button><td><a href='" . $hotelSummary[$x]['deepLink'] . "' ><button type='button'>Book Now</button></a></td></tr></table>";
}

simple pagination with this script?

Hello stackoverflow I was wondering if it is possible to get a simple pagination thingy with this little script? is it possible to get an example? this script takes all csv files in a folder and shows it on a webpage. The only problem is if the csv has 2000 records each csv then the page crashes. can I like show each csv file on a seperate page?
<?php
$arrFiles = glob("../Csv_folder/EMCY_GEN/*.csv");
$arrSortedFiles = array();
foreach($arrFiles as $strFileName) {
$arrSortedFiles[$strFileName] = filemtime($strFileName);
}
arsort($arrSortedFiles);
foreach(array_keys($arrSortedFiles) as $strFileName)
{
$file_handle = fopen($strFileName, "r");
echo "<th><font size='3pt'>".date ("F d Y H:i:s.", filemtime($strFileName))."</td></tr></font></th>";
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
echo '<tr><td>' . $line_of_text[0] . '</td><td>' . $line_of_text[1] . '</td><td>' . $line_of_text[2] . '</td><td>' . $line_of_text[3] . '</td><td>' . $line_of_text[4] . '</td></tr>' ;
}
fclose($file_handle);
}
?>
You want to not have a loop printing out the contents of each CSV. Have an index page that displays the files then link to another page that dynamically loads the CSV.
$arrFiles = glob("../Csv_folder/EMCY_GEN/*.csv");
$leg = count($arrFiles);
while($num < $leg){echo $arrFiles[$num]; $num = $num + 1};

Making selected value of option with php

I want to make a code which demonstrates that if I choose any option value and press submit button, the value which I chose should be seen in the options box.
The codes are;
<?php
$myfile = fopen("cars_lab5.txt", "r") or die("Unable to open file!");
?>
This is to open the file because I pull the items from a file.
<?php
$index = 0;
$val = $_GET['car'];
//$selection = "";
for ($index=0 ; $index < 5 ; $index++){
$num = 0;
$line = fgets($myfile) . "<br>";
$slide = explode("|",$line);
//echo '<option value="' . $index . '">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros';
if ($val==0){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==1){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==2){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==3){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
else if ($val==4){
echo '<option value="' . $index . '"' . $selection . ' selected">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros'; }
}
fclose($myfile);
?>
When I use this code, everything works properly but if I choose third option and press submit button, again first item is seen on the option box instead of what I choose.
You have set all options with the 'selected' tag, so the browser will show the last option as selected by default.
You need to remove the 'selected' string from the echo statement and configure the $selection var somewhere:
$selection=($index==$val?'selected':null)
You don't need the if/else statement at all.
So you would just have one line:
echo '<option value="' . $index . '"' . $selection . '">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros';
EDIT including example as per comment
Your example code will look like:
<?php
$index = 0;
$val = $_GET['car'];
//$selection = "";
for ($index=0 ; $index < 5 ; $index++){
$num = 0;
$line = fgets($myfile) . "<br>";
$slide = explode("|",$line);
$selection=($index==$val?'selected':null);
echo '<option value="' . $index . '"' . $selection . '">' . $slide[$num] . ' - ' . $slide[num+1] . ' euros';
}
fclose($myfile);
?>

How to remove blank line in CSV file in php

I m trying to fetch data from xml file then write it in CSV file.In generated file its put so many blank line after each record .
<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=spreadsheet.xls");
$url = 'test.xml'; // xml file location with file name
if (file_exists($url)) {
$xml = simplexml_load_file($url);
echo 'item_no' . "\t" . 'description' . "\t" . 'price' . "\t" . 'base_qty' . "\t" . 'var_qty' . "\t" . 'base_price_1' . "\t\n";
foreach ($xml->record as $books) {
$array_count = count($books);
for ($key = 0; $key < $array_count; $key++) {
echo $books->item_no[$key]."\t" . $books->description[$key]."\t" . $books- >price[$key]."\t" . $books->base_qty[$key] . "\t" . $books->var_qty[$key] . "\t" . $books->base_price_1[$key] . "\t" . "\n";
}
}
}
?>
Make sure the \n is not printed at the last record
<?php
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=spreadsheet.xls");
$url = 'test.xml'; // xml file location with file name
$output = "";
if (file_exists($url)) {
$xml = simplexml_load_file($url);
$output.= 'item_no' . "\t" . 'description' . "\t" . 'price' . "\t" . 'base_qty' . "\t" . 'var_qty' . "\t" . 'base_price_1' . "\t\n";
foreach ($xml->record as $books) {
$array_count = count($books);
for ($key = 0; $key < $array_count; $key++) {
$output.= $books->item_no[$key]."\t" . $books->description[$key]."\t" . $books- >price[$key]."\t" . $books->base_qty[$key] . "\t" . $books->var_qty[$key] . "\t" . $books->base_price_1[$key] . "\t" . "\n";
}
}
echo rtrim($output);
}
?>

Using arrays in PHP

I am working on my first program written in PHP. This is a basic question. I am trying to print a string stored in an array. I have two arrays. One array, $content, stores a number at $content[$i][15] (i am using arrays in arrays, and looping through it with a for loop). Lets say this number is 3. I now want to access a string in another array, called $type. This string is at position $type[3]. Logically, I think, $type[3] should print the same thing as $type[$content[$i][15]]. But, when I call $type[$content[$i][15]], it doesn't print anything, but when i print $type[3] it works fine. Is this type of call not allowed in PHP?
$type = array();
$typetemp = readfile("type.csv");
foreach ($typetemp as $sa){
$type[$sa[0]] = $sa[1];
}
$content = readfile("content.csv");
for($i=0; $i<sizeof($content); $i++){
if($content[$i][15] == 3){
if($content[$i][4] == 1){
$temp = $content[$i][15];
echo "id is " . $content[$i][0] . "title is " . $content[$i][1] . "introtext is " . $content[$i][2] . "restoftext is " . $content[$i][3] . "visible is " . $content[$i][4] . "category is " . $content[$i][5] . "creation_date is " . $content[$i][6] . "author is " . $content[$i][7] . "img is " . $content[$i][8] . "ordering is " . $content[$i][9] . "rank is " . $content[$i][10] . "vid is " . $content[$i][11] . "start_date is " . $content[$i][12] . "stop_date is " . $content[$i][13] . "event_date is " . $content[$i][14] . "type is " . $content[$i][15] . "thumb is " . $content[$i][16] . "type name is " . $type[$temp] . "<br/>";
}
}
}
function readfile($filename) {
$line_of_text = array();
$file_handle = fopen($filename, "r");
while (!feof($file_handle) ) {
array_push($line_of_text, fgetcsv($file_handle, 1024));
}
fclose($file_handle);
return $line_of_text;
}
You are missing a $ sign before content:
$type[$content[$i][15]]
This accesses the value in $type with index $content[$i][15]

Categories