php从水平到垂直排列表内容 [英] php arrange table content from horizontal to vertical
问题描述
您好,我有一个脚本读取csv文件。
<?php
echo' table border =0cellspacing =1cellpadding =1class =sortableborder =1>< caption> Title Here< / caption>
< thead>< tr>< th class =header>时间:< / th>< th class =header>值1:< / th> class =header>值2:< / th>< th class =header>值3:< / td class =header> 4:< / th>< th class =header>值5:< / th>< th class =header>值6:< / th>< th class = >值7:< / th>< th class =header>值8:< / th>< th class =header>值9:< / th> tr>< / thead>< tbody>< tr>
$ row = 1;
if(($ handle = fopen(data.csv,r))!== FALSE){
while(($ data = fgetcsv($ handle,1000,,) )!== FALSE){
$ num = count($ data);
$ row ++;
for($ c = 0; $ c <$ num; $ c ++){
if($ c == 9){echo< td>。$ data [$ c]。 < / td>< / tr>< tr>;}
else {echo< td>。$ data [$ c]。< / td& }
}
}
fclose($ handle);
}
echo'< / tbody>< / table>';
?>
此脚本只是获取数据并将其打印在html表中。我只是想重新排列表。
例如csv可能有这些内容
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
我希望的是:
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
并继续...
添加一个循环。我可以这样做吗?
感谢
你会将CSV文件读入一个多维数组。
请考虑CSV文件中的每一行现在都是一列(上到下,而不是从左到右)。这称为将行转移到列。
对于表,您需要循环遍历每一行,而不是每一列。因此,您可以在循环中创建一个循环,如下所示:
< table border =0cellspacing =1cellpadding =1class =sortableborder =1>< caption>标题这里< / caption>
< thead>< tr>< th class =header>时间:< / th>< th class =header>值1:< / th> class =header>值2:< / th>< th class =header>值3:< / td class =header> 4:< / th>< th class =header>值5:< / th>< th class =header>值6:< / th>< th class = >值7:< / th>< th class =header>值8:< / th>< th class =header>值9:< / th> tr>< / thead>< tbody>
<?php
#read CSV文件
if(($ handle = fopen(data.csv,r))!== FALSE){
$ mycsv = array();
while(($ data = fgetcsv($ handle,1000,,))!== FALSE)$ mycsv [] = $ data;
fclose($ handle);
#查找转置行的长度
$ row_length = count($ mycsv);
#遍历每行(或csv中的每一行)并输出该行的所有列
foreach($ 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>
尝试一下,让我知道是否有效。
Hello I have a script which read a csv file.
<?php
echo '<table border="0" cellspacing="1" cellpadding="1" class="sortable" border="1"><caption>Title Here</caption>
<thead><tr><th class="header">Time:</th><th class="header">Value 1:</th><th class="header">Value 2:</th><th class="header">Value 3:</td class="header"><th class="header">Value 4:</th><th class="header">Value 5:</th><th class="header">Value 6:</th><th class="header">Value 7:</th><th class="header">Value 8:</th><th class="header">Value 9:</th></tr></thead><tbody><tr>';
$row = 1;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$row++;
for ($c=0; $c < $num; $c++) {
if ($c==9) { echo "<td>".$data[$c] ."</td></tr><tr>";}
else {echo "<td>".$data[$c] ."</td>"; }
}
}
fclose($handle);
}
echo '</tbody></table>';
?>
This script just take the data and print them in a html table. I just want to rearrange the table. For example the csv may have these contents
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7
I wish the out to be:
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
and go on... I some I have to put an additional loop..how can I do it?
Thanks
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:
<table border="0" cellspacing="1" cellpadding="1" class="sortable" border="1"><caption>Title Here</caption>
<thead><tr><th class="header">Time:</th><th class="header">Value 1:</th><th class="header">Value 2:</th><th class="header">Value 3:</td class="header"><th class="header">Value 4:</th><th class="header">Value 5:</th><th class="header">Value 6:</th><th class="header">Value 7:</th><th class="header">Value 8:</th><th class="header">Value 9:</th></tr></thead><tbody>
<?php
#read CSV file
if (($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 row
foreach($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.
这篇关于php从水平到垂直排列表内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!