Styling created div's from a csv file using PHP? - php

I'm loading some columns from a csv file using php.
I am using divs to display the columns, but the issue is that the contents of the div for each heading is not perfectly aligned underneath the heading.
this is my php code:
<?php
$file_handle = fopen("myCSV.csv", "r");
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
print "<div style='float:left; margin-right:15px;'>".$line_of_text[2].' '.$line_of_text[3].' '.$line_of_text[4].' '.$line_of_text[5].' '.$line_of_text[6].' '.$line_of_text[7].' '.$line_of_text[8].' '.$line_of_text[9]."</div><br /> ";
}
fclose($file_handle);
?>
How can I align the columns under each related headings properly?
here is the output data:
http://jsfiddle.net/w9v5q8vp/
This is how I tried to use a table:
print "<table>\n";
print "<tr><td>".$line_of_text[2]."</td></tr><tr><td>".$line_of_text[3]."</td></tr>";
print '</table>';

When you output a table you should loop only over the rows:
$file_handle = fopen("myCSV.csv", "r");
print "<table>\n";
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
print "<tr><td>".$line_of_text[2].'</td><td>'.$line_of_text[3].'</td><td>'.$line_of_text[4].'</td><td>'.$line_of_text[5].'</td><td>'.$line_of_text[6].'</td><td>'.$line_of_text[7].'</td><td>'.$line_of_text[8].'</td><td>'.$line_of_text[9]."</td></tr>\n";
}
print '</table>';
fclose($file_handle);
The beginning/end table tags should be printed only once.
You can read more about table tr td tags at w3schools.

Related

fgetcsv(), delimiter is not being recognised

I am trying to use fgetcsv() to grab data from a CSV file to a HTML table.
I am able to put the data into the HTML table, but I would like the table to have two columns, and separated by a "|" character.
I have the line :
fgetcsv($file, 999, "|");
However, the delimiter seems to be ignored and everything is input into one column?
this is my code:
<table>
<?php
$file = fopen("WSfile.csv", "r");
while (! feof($file)){
$lines = fgetcsv($file, 999, "|");
echo "<tr>";
foreach((array) $lines as $line){
echo "<td>" . $line . "</td>";
}
echo"</tr>";
}
fclose($file);
?>
</table>

find character in string from csv file using php?

I am really a newbie in php. I have a problem in doing this..
I have sample.csv file contains 3 rows: inbound(1st row), outbound(2nd row), and date(3rd row).
sample.csv
**inbound** **outbound** **date**
IN/15#001234 OUT/000000163-000000as 1/12/2014
IN/15#004323 NOT/000000141-00000043 1/14/2014
IN/15#005555 OUT/000000164-000000jk 1/15/2014
is it possible to display the all columns where 2ndrow is start with "NOT" and a number before char "-" is 141???
output:
IN/15#004323 NOT/000000141-00000043 1/14/2014
i dont know if it is possible... please help me..
I have a code below. But it only open the csv file...
$file = fopen('Master.csv', 'r');
echo "<table style='border: 2px solid black; text-align:left'>";
while (($line = fgetcsv($file)) !== FALSE) {
list($inbound, $outbound, $date) = $line;
echo "<tr>";
echo "<td>$inbound</td>";
echo"<td>$outbound</td>";
echo "<td>$date</td>";
echo "</tr>";
}
echo "</table>";
is it possible to display the all columns where 2ndrow is start with "NOT" and a number before char "-" is 141???
Inserting
if (preg_match('/^NOT/', $outbound)) continue;
after the list()... statement should be sufficient.
But your data does not look like being comma-seperated, rather than tab-seperated. And perhaps you mean columns when talking about rows at the beginning?
You can use strpos()
if ( strpos($outbound, 'NOT') !== false ) {
// "NOT" WORD FOUND IN STRING
}
Try this out. This will work with comma separated csv file.
echo "<table border = 1><tr><td>first</td><td>second</td><td>third</td></tr>"; //creating table
$handle = fopen('fe.csv', "r"); //open csv file
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) //read csv file row by row
{
//check both NOT and 141- in the string
if ( (strpos($data[1], 'NOT') !== false ) && (strpos($data[1], '141-') !== false )) {
//add required field data to table
echo "<tr>";
echo "<td>".$data[0]."</td>";
echo"<td>".$data[1]."</td>";
echo "<td>".$data[2]."</td>";
echo "</tr>";
}
}
echo "</table>"; //close table
?>

PHP: sorting CSV output by numeric of one column?

I'm creating a table using PHP from a CSV file.
I am trying to sort the output (DESCENDING) by one column from the CSV file which is: $line_of_text[13]
But I have no idea how to this as it is not as straight forward as sorting the output if I was using mysql.
This is my entire code:
<?php
$file_handle = fopen("MY-CSV-FILE.csv", "r");
print "<table style='width:100%; float:left;'>\n";
while (!feof($file_handle) ) {
//print '<tr>';
$line_of_text = fgetcsv($file_handle, 1024);
$line_of_text = str_replace('http', '<a href=""><img title="Click To Enlarge" class="fancybox" style="width:210px; height=210px; float:left;" src ="http', $line_of_text);
$line_of_text = str_replace('jpg', 'jpg"/></a>', $line_of_text);
$line_of_text = str_replace(',', '', $line_of_text);
$line_of_text = str_replace('PictureRefs', '', $line_of_text);
asort($line_of_text);
foreach($line_of_text as $key => $value)
print "<tr><td>".$line_of_text[9].'</td><td>'.$line_of_text[10].'</td><td>'.$line_of_text[2].'</td><td>'.$line_of_text[5].'</td><td>'.$line_of_text[6].'</td><td>'.$line_of_text[7].'</td><td>'.$line_of_text[8].'</td><td>'.$line_of_text[3].'</td><td>'.$line_of_text[4].'</td><td>'.$line_of_text[11].'</td><td>'.$line_of_text[12].'</td><td>'.$line_of_text[13].'</td><td>'.$line_of_text[14].'</td><td>'.$line_of_text[15]."</td></tr>\n<tr><td width='100%' colspan='100'><div style=' width:100%;'>".$line_of_text[16]."</div></td></tr>";
}
print '</table>';
fclose($file_handle);
?>
As you can see I have started doing this:
asort($line_of_text);
foreach($line_of_text as $key => $value)
but I don't think this is correct!
could someone please advise on this ?
any help would be appreciated.
You need to load all of your rows into an array, sort, and then echo the table. You need to split this into two loops.

Populate table from excel file using php

I am trying to use data from an excel spreasheet to populate an html table using php. I am a beginner at PHP. I have tried to use code from other questions, and they were close but not quite what I needed. The excel document will be periodically updated by another person.
Here's an example of code I've used:
$file = file("/calendar.txt");
print "<table>
<tr><td>Date</td><td>Start Time</td><td>Venue</td><td>Description</td></tr>";
foreach($file as $line){
$line = trim($line);
$split = mb_split("\t",$line);
print "<tr><td>$split[3]</td><td>$split[4]</td><td>$split[5]</td><td>$split[6]</td></tr>";
}
print "</table>";
?>
But the above example does not allow for auto-population. So I tried this:
<table>
<?php
if (($handle = fopen("/calendar.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
print "<tr><td> $data[$c] </td></tr>";
}
}
fclose($handle);
}
?>
</table>
But I couldn't get the columns I wanted. Plus both examples did not allow for a new row/column created at the end of the last column from the source file (i.e. the data from the last column in the first row is combined with the first column of the second row).
I would also like to echo the line, "There are no upcoming dates currently. Please check back soon!" if there is no information to display. And is there a way to do a colspan in php? Here are my failed attempts: http://www.tonejones.com/calendar3.php
I want the table to look like this: http://www.tonejones.com/calendar.php
To populate Data from Excel to Table. First We need to retrieve all data into Array then we will render all Array values into table.Get reference to retrieve data into array. https://www.studytutorial.in/how-to-upload-or-import-an-excel-file-into-mysql-database-using-spout-library-using-php. IF you get array then use below code
<table>
<?php foreach($rows as $value){ ?>
<tr>
<td><?php echo $value; ?></td>
</tr>
<?php } ?>
</table>
Your block should go around the collection of cells, not each individual cell:
print "<table>
<tr><td>Date</td><td>Start Time</td><td>Venue</td><td>Description</td></tr>";
<?php
if (($handle = fopen("/calendar.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
$num = count($data);
print "<tr>";
for ($c=3; $c < $num; $c++) {
print "<td> $data[$c] </td>";
}
print "<tr>";
}
fclose($handle);
} else {
print "<tr><tdcolspan="4">
There are no upcoming dates currently. Please check back soon!
</td></tr>";
}
?>
</table>

Looping through records in an excel document

I need to loop through some records within an excel file and output them as headings for an HTML table but I don't know what is the best approach as I am new to PHP.
Each the column headings begins as a Title followed by an index so you have:
Country1, Country2, Country3 ....through to Country50.
I would like to have a more automatic way to retrieve these values rather than stick to the
$result .= "$Country1";
each must also contain the id of the country which should also be converted into an array i.e. The other thing so they do not require to be printed if that's the case, so the results could be
<th id="Country1">Value of Country1</th>
<th id="Country2">Value of Country2</th>
<th id="Country3">Value of Country3</th>
<th id="Country8">Value of Country8</th>
<th id="Country24">Value of Country24</th>
<th id="Country30">Value of Country30</th>
What is the best "code light" approach to doing this?
Regards!
something like that should work:
<?php
$handle = #fopen("/tmp/myfile.csv", "r");
if ($handle) {
// first line should be the header, we make it an array of strings
if ( ($buffer = fgets($handle, 4096)) !== false )
$headers[] = explode(",", $buffer);
else
echo "Error: empty file...\n";
// second line should be the values, for each line we output the xml markups
while ( ($buffer = fgets($handle, 4096)) !== false )
{
$values[] = explode(",", $buffer);
if( count($headers) != count(values) )
echo "Error: the 2 lines do not have same number of columns...\n";
else
{
for( $i = 0 ; $i < count($headers) ; $i++ )
echo "<th id='".$headers[$i]."'>".$values[$i]."</th>\n";
}
}
else
echo "Error: second line empty...\n";
if ( !feof($handle) )
echo "Erreur: fgets() failed...\n";
fclose($handle);
}
?>

Categories