Php Arrange Table Content From Horizontal To Vertical
I have a script which read a CSV file. Title Here
Solution 1:
Well you'd read the CSV file into a multidimensional array.
Consider that each line in the CSV file is now a column (goes up to down instead of left to right). This is called Transposing rows to columns.
For a table you'll need to loop through each row, not each column. So you create a loop within a loop as shown here:
<tableborder="0"cellspacing="1"cellpadding="1"class="sortable"border="1"><caption>Title Here</caption><thead><tr><thclass="header">Time:</th><thclass="header">Value 1:</th><thclass="header">Value 2:</th><thclass="header">Value 3:</td class="header"><thclass="header">Value 4:</th><thclass="header">Value 5:</th><thclass="header">Value 6:</th><thclass="header">Value 7:</th><thclass="header">Value 8:</th><thclass="header">Value 9:</th></tr></thead><tbody><?php#read CSV fileif (($handle = fopen("data.csv", "r")) !== FALSE) {
$mycsv = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) $mycsv[] = $data;
fclose($handle);
#Find the length of the transposed row$row_length = count($mycsv);
#Loop through each row (or each line in the csv) and output all the columns for that rowforeach($mycsv[0] as$col_num => $col)
{
echo"<tr>";
for($x=0; $x<$row_length; $x++)
echo"<td>".$mycsv[$x][$col_num]."</td>";
echo"</tr>";
}
}
?></tbody></table>
Try that out and let me know if it works.
Solution 2:
I'm not exactly sure how your csv file is laid out, but it looks like you might need to store these values in separate arrays for the different digits and then loop through those arrays after you've finished reading the whole csv file. Can you show a brief sample of the csv file so I can get an idea of the data you're reading in?
Post a Comment for "Php Arrange Table Content From Horizontal To Vertical"