如何以相反的顺序显示行?
CSV文件:
第1栏;第2栏;第3栏 细胞1细胞1 细胞2;细胞2 细胞3;细胞3
应该是这样的:
第1栏;第2栏;第3栏 细胞3;细胞3 细胞2;细胞2 细胞1细胞1
代码:
if (($handle = fopen($path, 'r')) !== FALSE)
{
echo '<table class="table table-striped table-bordered"><thead>';
// Get headers
if (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
}
echo '</thead><tbody>';
// Get the rest
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
echo '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
}
fclose($handle);
echo '</tbody></table>';
}提前谢谢。
发布于 2016-10-24 20:42:04
先收
$collect = array();
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
$collect[]= '<tr><td>'.implode('</td><td>', $data).'</td></tr>';
}
echo implode(PHP_EOL,array_reverse($collect));在末尾反转数组。
发布于 2016-10-24 20:41:27
这里需要做的是:而不是直接将echo行存储到一个变量中:
// Get the rest
$rest = '';
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
// main trick here - add every new row BEFORE old ones
$rest = '<tr><td>'.implode('</td><td>', $data).'</td></tr>' . $rest;
}
// echo gathered data
echo $rest;https://stackoverflow.com/questions/40227108
复制相似问题