如何按类别分页并转到特定记录?

时间:2012-07-11 02:19:10

标签: php mysql

大家好我是mysql和php的新手。我有一个产品库,我设法将它们放在一起并复制其他代码以通过记录分页。画廊有两张桌子; one保存数据,例如:productID,title,online,...和productTypeID,它是productType的关系字段。现在我想点击一个图像来传递两个参数productID和productTypeID,并在详细信息页面中创建一个分页,该分页按照productTypeID对所有记录进行分组,但是转到等于在参数上传递的productID的特定记录。

我设法通过productTypeID进行分页查询,但它只会转到数组的第一条记录。这是我的代码请帮助。

<?php

if (isset($_GET['id'])) {
  $id = $_GET['id'];

$query_productCount= "SELECT * FROM products WHERE productID ='$id'";
$productCount= mysql_query($query_recipeMenu, $connection) or die(mysql_error());
$row_productCount= mysql_fetch_assoc($productCount);
}

if (isset($_GET['cat'])) {
  $cat = $_GET['cat'];
}
//check for a page number. If not, set it to page 1


if (!(isset($_GET['pagenum']))){
    $pagenum = 1;
}else{
    $pagenum = $_GET['pagenum'];
}

//query for record count to setup pagination
$data = mysql_query("SELECT * FROM products WHERE productTypeID='$cat'");
$rows = mysql_num_rows($data);


//number of record per page
$page_rows = 1;

//get the last page number
$last = ceil($rows/$page_rows);

//make sure the page number isn't below one, or more than last page num
if ($pagenum < 1){
    $pagenum = 1;
}elseif ($pagenum > $last){
    $pagenum = $last;
}

//Set the range to display in query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//get all of the record
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products WHERE productTypeID='$cat' $max");
//check for record
$productCount = mysql_num_rows($sql);

if ($productCount > 0){
    while($row = mysql_fetch_array($sql)){
            $productID = $id;
            $productName = $row["productName"];
            $largeImage = $row["largeImage"];
            $productDescription = $row["productDescription"];

            $dynamicList .= ' 
                            <div>
                                <h3>'.$productName.'</h3>
                                <div><a href="javascript:history.go(-1)"><img src="images/productImages/'.$largeImage.'" alt="'.$productName.'" width="" /></a></div>

                                <p">'.strip_tags(nl2br($productDescription), "<b><br><p><a>").'</p>




                            </div>

                            ';
    }
}else{
    $dynamicList = "<p>Sorry there are no products under this category</p>";
}

1 个答案:

答案 0 :(得分:0)

您正在设置

  $page_rows = 1;

然后

//get the last page number
$last = ceil($rows/$page_rows);

这将使每个页面只有一行,

如果$ rows = 30  那么$ last将是ceil(30/1)= 30,每页只有一行

将$ page_rows设置为您希望每页显示的记录数。

更新

并设置偏移值

$offset=($page_num-1) * $page_rows;
$max = 'limit ' . $offset .',' .$page_rows;