【问题标题】:MySQL Query pagination with PHP使用 PHP 进行 MySQL 查询分页
【发布时间】:2014-04-18 12:43:22
【问题描述】:

如何在这个简单的项目显示中添加分页系统?以及如何为过滤器的结果添加分页?我只是迷失了那部分,我无法弄清楚! 我也想在菜单上应用 CSS!

代码如下:

<?php
                include('db.php');
                if(isset($_POST['filter']))
                {
                    $filter = $_POST['filter'];
                    $result = mysql_query("SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
                }
                else
                {
                    $result = mysql_query("SELECT * FROM products");
                }
                while($row=mysql_fetch_assoc($result))
                {
                    echo '<li class="portfolio-item2" data-id="id-0" data-type="cat-item-4">';
                    echo '<div>
                    <span class="image-block">
                    <a class="example-image-link" href="reservation/img/products/'.$row['imgUrl'].'" data-lightbox="example-set" title="'.$row['Product'].'""><img width="225" height="140" src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" />                    
                    </a>
                    </span>
                    <div class="home-portfolio-text">
                    <h2 class="post-title-portfolio"><font color="#666666">'.$row['Product'].'</font></h2>
                    <p class="post-subtitle-portfolio"><font color="#666666">Descrição: '.$row['Description'].'
                    <p class="post-subtitle-portfolio"><font color="#666666">Categoria: '.$row['Category'].'
                    <p class="post-subtitle-portfolio">Código: '.$row['Price'].'</p><br/></font></p>
                    </div>
                    </div>';
                    echo '</li>';
                }
                ?>

编辑:

<?php
                include('db.php');
                if(isset($_POST['filter']))
                {
                    $filter = $_POST['filter'];
                    $result = mysql_query("SELECT * FROM products where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
                }
                else
                {
$start=0;
$limit=6;

if(isset($_GET['id']))
{
    $id = $_GET['id'];
    $start = ($id-1)*$limit;
}

$result = mysql_query("SELECT * FROM products LIMIT $start, $limit");
                }
while($row = mysql_fetch_array($result))
{
    echo '<li class="portfolio-item2" data-id="id-0" data-type="cat-item-4">';
                    echo '<div>
                    <span class="image-block">
                    <a class="example-image-link" href="reservation/img/products/'.$row['imgUrl'].'" data-lightbox="example-set" title="'.$row['Product'].'""><img width="225" height="140" src="reservation/img/products/'.$row['imgUrl'].'" alt="'.$row['Product'].'" title="'.$row['Product'].'" />                    
                    </a>
                    </span>
                    <div class="home-portfolio-text">
                    <h2 class="post-title-portfolio"><font color="#666666">'.$row['Product'].'</font></h2>
                    <p class="post-subtitle-portfolio"><font color="#666666">Descrição: '.$row['Description'].'
                    <p class="post-subtitle-portfolio"><font color="#666666">Categoria: '.$row['Category'].'
                    <p class="post-subtitle-portfolio">Código: '.$row['Price'].'</p><br/></font></p>
                    </div>
                    </div>';
                    echo '</li>';
}
echo "</ul>";


$rows = mysql_num_rows(mysql_query("SELECT * FROM products"));
$total = ceil($rows/$limit);

if($id>1)
{
    echo "<center><a href='?id=".($id-1)."' class='button'>Anterior</a></center>";
}
if($id!=$total)
{
    echo "<center><a href='?id=".($id+1)."' class='button'>Próximo</a></center>";
}

?>

【问题讨论】:

    标签: php mysql pagination


    【解决方案1】:

    你应该有这样的东西:

    // declare a base query
    $q = "SELECT * FROM products";
    if(isset($_POST['filter']))
    {
        $filter = $_POST['filter'];
        // append filter to query
        $q += "where Product like '%$filter%' or Description like '%$filter%' or Category like '%$filter%'");
    }
    // check for "page" URL parameter, if not available, go to first page
    $page = isset($_GET['page']) ? $_GET['page'] : 1;
    // check for "pageSize" URL parameter, if not available, fall back to 20
    $pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 20;
    // append the pagination to your query
    $q += sprintf("LIMIT %d,%d;", ($page-1)*$pageSize, $pageSize);
    // execute the constructed query
    $result = mysql_query($q);
    

    请注意,代码是“伪代码”,未经测试,但应该为您提供基本思路。

    另外,您可以查看this SO post 关于 MySQL 分页的信息。

    更新

    在 PHP 中,如果你使用一个未初始化的变量,那么它将有一个依赖于上下文的默认值。请参阅有关此here 的文档。以下是摘录:

    在 PHP 中不需要初始化变量,但它是 很好的做法。未初始化变量的默认值为 它们的类型取决于使用它们的上下文 - 布尔值 默认为 FALSE,整数和浮点数默认为零,字符串(例如 在 echo 中使用)设置为空字符串,数组变为空 数组。

    【讨论】:

    • 我正在编辑这个几个小时,但我仍然无法使用这个系统。我尝试了该帖子和您的代码中的所有内容!
    • 但是怎么了?究竟是什么不工作?你能发布你的错误信息吗?
    • 没关系,我忘了通知我的数据库。但是我遇到了一个新问题,分页正在工作。您可以在我的代码中看到有一个过滤器可以在数据库中搜索并显示它。我收到此错误:PHP Warning: Division by zero in E:\Domains\site.com\wwwroot\products.php on line 252,我将立即编辑并发布代码。 $total = ceil($rows/$limit); 这是错误所在的行。没想到
    • 我看到您仅在没有过滤器的情况下才设置 $limit 变量。当您应用任何过滤器时,您可能会收到此错误,因为未定义的变量在数值表达式中具有其默认值,即 0。
    • 我怎么没想到?哈哈,我感觉很笨。现在一切正常,我只是为过滤器设置了一个限制。还有什么我可以做的吗?
    猜你喜欢
    • 1970-01-01
    • 2011-07-18
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多