drra6593 2014-03-12 00:02
浏览 35
已采纳

PHP For Mysqli的循环问题

I'm trying to get this PHP script to paginate the data pulled from the mysql database.

It is going wrong somewhere in the second for loop. Instead of pulling the data through it is just returning blank or empty fields.

I need it to display the title, description and content fields from the database, along with the ID.

        require_once("../controls/config.php");
        $connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

        if ($connection->connect_errno) {
            printf("Connect failed: %s
", $connection->connect_error);
            exit();
}

        // number of results to show per page
        $per_page = 3;

        // figure out the total pages in the database
        $result = $connection->query("SELECT * FROM pages");
        $total_results = $result->num_rows;
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='?page=$i'>$i</a><br>";
        }

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                echo $i["id"].' ';
                echo $i["title"].' ';
                echo $i["description"].' ';
                echo $i["content"].' ';
                echo '<a href="edit.php?id='.$i["id"].'">Edit</a> ';
                echo '<a href="delete.php?id='.$i["id"].'">Delete</a><br>';

        }

?>
<p><a href="new.php">Add a new record</a></p>

Can anyone point me in the right direction?

  • 写回答

1条回答 默认 最新

  • dtdr57046 2014-03-12 00:18
    关注

    It looks like you are trying to treat $i as an associative array. Though $i is only a integer. Additionally, you do not have an array the contains the results from your mysqli query. You should try:

    // this will loop through results and assign to $rows array
    while($row = $result->fetch_array())
    {
       $rows[] = $row;
    }
    
    // this will loop through $rows array and provide each column result
    foreach($rows as $row)
    {
        echo $row["id"];
        echo $row["title"];
        echo $row["description"];
        echo $row["content"];
    }
    

    For further information please refer to: http://us2.php.net/mysqli_fetch_array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历