dtch60248 2015-10-31 06:55
浏览 76

如何在php中对数据库表进行分页

i am learning php recently. i get the table from database but i face problem giving pagination to that table.
i don't know how to give pagination to the database table how can i do that.

This is my code

        <?php

            // If user not logged in then show login message
            if (!isset($_SESSION['userid'])) {

                ?><div class="message">
                    To access this page, you must be logged.<br />
                    <a href="connexion.php">Log in</a>
                </div><?php
            }
            else{

                //Video count values of the database
                if(isset($_REQUEST['date1']) && isset($_REQUEST['date2'])){
                    $dnn = mysqli_query($conn, 'select video_date,video_count from video_master where  rec_id="'.$_SESSION['userid'].'" and video_date >= "' . $_REQUEST['date1'] . '" and video_date <= "' . $_REQUEST['date2'] . '"');
                }
                else{
                    $dnn = mysqli_query($conn, 'select video_date,video_count from video_master where  rec_id="'.$_SESSION['userid'].'"');
                }


                if(mysqli_num_rows($dnn)>0)
                {



                    ?><table>
    <thead>
                        <tr>
                            <th>Date</th>
                            <th>Video Count</th>
                        </tr></thead><?php

                        while($rowData = mysqli_fetch_array($dnn)){
                            //$video_date = date("Y-m-d","d-m-Y",$rowData['video_date']);
                            $video_date = $rowData['video_date'];
                            $video_count = $rowData['video_count'];

                            ?><tbody><tr>
                                <td><?php print($video_date); ?></td>
                                <td><?php print($video_count); ?></td>
                            </tr></tbody><?php

                        }
                    ?></table><?php
                }   

                ?>
            <?php

            }

    ?>

THE CODE WHICH I TRY BUT IN THIS CODE I GET ONLY NUMBERS UNDER MY TABLE THE VALUE IS NOT GET CHANGE ON CLICKING ON NUMBER.

<?php

                $per_page=5;
                if (isset($_GET["page"])) {

        $page = $_GET["page"];

        }

        else {

        $page=1;

        }
        // Page will start from 0 and Multiple by Per Page
        $start_from = ($page-1) * $per_page;

        //Selecting the data from table but with limit
        $query = "SELECT * FROM video_master LIMIT $start_from, $per_page";
        $result = mysqli_query ($conn, $query);

                // If user not logged in then show login message
                if (!isset($_SESSION['userid'])) {

                    ?><div class="message">
                        To access this page, you must be logged.<br />
                        <a href="connexion.php">Log in</a>
                    </div><?php
                }
                else{

                    //Video count values of the database
                    if(isset($_REQUEST['date1']) && isset($_REQUEST['date2'])){
                        $dnn = mysqli_query($conn, 'select video_date,video_count from video_master where  rec_id="'.$_SESSION['userid'].'" and video_date >= "' . $_REQUEST['date1'] . '" and video_date <= "' . $_REQUEST['date2'] . '"');
                    }
                    else{
                        $dnn = mysqli_query($conn, 'select video_date,video_count from video_master where  rec_id="'.$_SESSION['userid'].'"');
                    }


                    if(mysqli_num_rows($dnn)>0)
                    {



                        ?><table>
        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Video Count</th>
                            </tr></thead><?php

                            while($rowData = mysqli_fetch_array($dnn)){
                                //$video_date = date("Y-m-d","d-m-Y",$rowData['video_date']);
                                $video_date = $rowData['video_date'];
                                $video_count = $rowData['video_count'];

                                ?><tbody><tr>
                                    <td><?php print($video_date); ?></td>
                                    <td><?php print($video_count); ?></td>
                                </tr></tbody><?php

                            }
                        ?></table><?php
                        //Now select all from table
        $query = "select * from video_master";
        $result = mysqli_query($conn, $query);

        // Count the total records
        $total_records = mysqli_num_rows($result);

        //Using ceil function to divide the total records on per page
        $total_pages = ceil($total_records / $per_page);

        //Going to first page
        echo "<center><a href='reports.php?page=1'>".'First Page'."</a> ";
        for ($i=1; $i<=$total_pages; $i++) {

        echo "<a href='reports.php?page=".$i."''>".$i."</a>";
        };
        // Going to last page
        echo "<a href='reports.php?page=$total_pages'>".'Last Page'."</a></center> ";


                    }   

                    ?>
                <?php

                }

        ?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据