Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何按类别分页并转到特定记录?_Php_Mysql - Fatal编程技术网

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

Php 如何按类别分页并转到特定记录?,php,mysql,Php,Mysql,大家好,我是mysql和php的新手。我有一个产品库,我设法把它放在一起,并复制其他人的代码,以便对记录进行分页。画廊有两张桌子;其中一个保存数据,例如:productID、title、online……和productTypeID,后者是productType中的一个关系字段。现在,我想在单击一个图像时传递两个参数productID和productTypeID,并在详细信息页面中创建一个分页,该分页按productTypeID对所有记录进行分组,但转到一个与在参数上传递的productID相等的

大家好,我是mysql和php的新手。我有一个产品库,我设法把它放在一起,并复制其他人的代码,以便对记录进行分页。画廊有两张桌子;其中一个保存数据,例如: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>";
}
你正在设置

  $page_rows = 1;
然后

这将使每页只有一行

如果$rows=30 那么$last将是ceil30/1=30,并且每个页面只有一行

将$page_rows设置为每页要显示的记录数

更新

并设置偏移值

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

提示:你最好做一个选择计数*。。。以获取总记录数。执行select*和num_rows命令时,数据库将开始从磁盘检索数据,您只需按类别进行分页。我已经有了一个页面,该页面显示所有记录,而不考虑类别。我想完成的是在细节页面上对所有匹配同一类别的记录进行分页…完成后,类将对所有记录进行分组和分页,但我希望它转到一个特定的记录,该记录的ID与我通过参数ID传递的记录的ID相同。
$offset=($page_num-1) * $page_rows;
$max = 'limit ' . $offset .',' .$page_rows;