MySQL Query与PHP的分页

时间:2014-03-24 19:00:53

标签: php mysql pagination

如何在这个简单的项目显示中添加分页系统?我如何为过滤器的结果添加分页?我只是迷失了那部分,我无法弄明白! 我也想在菜单上应用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>";
}

?>

1 个答案:

答案 0 :(得分:2)

你应该有这样的东西:

// 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);

请注意,代码是&#34; pseudoish&#34;,未经过测试,但应该为您提供基本想法。

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

更新

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

  

没有必要在PHP中初始化变量,但它是一个   非常好的做法。未初始化的变量的默认值为   它们的类型取决于它们的使用环境 - 布尔值   默认为FALSE,整数和浮点数默认为零,字符串(例如   在echo中使用)被设置为空字符串,数组变为空   阵列。