duanpo1498 2013-01-03 18:47
浏览 39

使用php脚本生成的html表的分页问题

I'm handling a php code that reads a .csv file inside the same folder and print a table in html. As the file contains a lot of rows, i'd like to limit the table to a specific number of rows (for example 50). I really don't know how to do, anyone can help a newbie as i'm?

 <?php

 $row = 1;
 if (($handle = fopen("myfile.csv", "r")) !== FALSE) {

echo '<table border="1">';

while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
    $num = count($data);
    if ($row == 1) {
        echo '<thead><tr>';
    }else{
        echo '<tr>';
    }

    for ($c=0; $c < $num; $c++) {
        if(empty($data[$c])) {
           $value = "&nbsp;";
        }else{
           $value = $data[$c];
        }
        if ($row == 1) {
            echo '<th>'.$value.'</th>';
        }else{
            echo '<td>'.$value.'</td>';
        }
    }

    if ($row == 1) {
        echo '</tr></thead><tbody>';
    }else{
        echo '</tr>';
    }
    $row++;
}

echo '</tbody></table>';
fclose($handle);
 }
 ?>
  • 写回答

2条回答 默认 最新

  • duandang2123 2013-01-03 18:56
    关注

    what you will want to do is pass in a GET parameter, something like start, and then use that to set the starting number for your for loop

    for ($c=0; $c < $num; $c++) {
    

    then becomes something like

    for ($c=$_GET['start']; $c < $_GET['start']+50 && $c < $num; $c++) {
    

    then at the bottom of the page you add links like

    <a href="<?=$_SERVER['PHP_SELF']?>?start=<?=$_GET['start']-50 ?>">"><< Prev</a>
    <a href="<?=$_SERVER['PHP_SELF']?>?start=<?=$_GET['start']+50 ?>">">Next >></a>
    

    obviously this is not a robust/secure solution, you'll need to add checks to make sure that the start variable is a number, and add bounds checking.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP