dqdmvg7332 2015-12-25 23:24
浏览 34

添加分页后,PHP函数不再显示mysql数组数据

I have made a PHP function that pulls data from a table (product_reviews) in my database, showing all reviews for a unique product_id. Here is the code:

function showReviews ($product_id)
{
    include('database_conn.php');

    $query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."'";

    if ($queryresult = mysqli_query($conn, $query))
    {
        while ($currentrow = mysqli_fetch_assoc($queryresult))
        {
            $result_list[] = $currentrow;
        }

        foreach ($result_list as $currentrow)
        {
            $productitems[] = array(
            $customer_forename = $currentrow['customer_forename'],
            $customer_surname = $currentrow['customer_surname'],
            $review_title = $currentrow['review_title'],
            $review_text  = $currentrow['review_text']);

            echo '<article class="Review_Box">';
                echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
                echo "<p>".$review_text."</p>";

            echo "</article>";

        }

    }

}

This function is then called from the products page and it works as intended.

But when I add pagination to the function ( following an ehow tutorial), the function doesn't have any output(see here).

if(!function_exists('showReviews'))
{
    function showReviews ($product_id)
    {
        include('database_conn.php');
        include('functions.php');

        $rowsPerPage = 3;

        $currentPage = ((isset($_GET['page']) && $_GET['page'] > 0) ? (int)$_GET['page'] : 1);

        $offset = ($currentPage-1)*$rowsPerPage;


        $query = "SELECT * FROM product_reviews WHERE product_reviews.productID='".$product_id."' LIMIT '".$offset."','".$rowsPerPage."'";



        if ($queryresult = mysqli_query($conn, $query))
        {
            while ($currentrow = mysqli_fetch_assoc($queryresult))
            {
                $result_list[] = $currentrow;
            }

            foreach ($result_list as $currentrow)
            {
                $productitems[] = array(
                $customer_forename = $currentrow['customer_forename'],
                $customer_surname = $currentrow['customer_surname'],
                $review_title = $currentrow['review_title'],
                $review_text  = $currentrow['review_text']);

                echo '<article class="Review_Box">';
                    echo "<h3>".$review_title." by ".$customer_forename." ".$customer_surname."</h3></br>";
                    echo "<p>".$review_text."</p>";

                echo "</article>";

            }

        }
        $count = countReviews (1);

        $totalPages = $count/$rowsPerPage;
        if($currentPage > 1) 
        {
            echo '<a href="SingleProductPage.php?id='.$product_id.'&page=' . ($currentPage-1) . '#Customer_Reviews">Previous Page</a> ';

        }

        if($currentPage < $totalPages) 
        {
            echo '<a href="SingleProductPage.php?id='.$product_id.'&page=' . ($currentPage+1) . '#Customer_Reviews">Next Page</a>';

        }
    }
}

I tested my sql query and it works fine in mysql Workbench. What am I doing wrong? Can anyone recommend a better way to do this?

  • 写回答

1条回答 默认 最新

  • dst67283 2015-12-26 19:33
    关注

    When you create your query, you are encapsulating the offset and limit between single quotes (if I read your code correctly).

    SELECT * FROM product_reviews WHERE product_reviews.productID='25' LIMIT '0','3'

    Try removing those single quotes.

    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致