[英]Using Pagination on product view using Category ID
I feel I have asked alot of questions recently and Im sorry if this is relatively simple but I am struggling to get this working correctly. 我觉得我最近问了很多问题,很抱歉这是否相对简单,但我正在努力使其正常工作。
I want to add pagination to my product view but when I add the pagination numbers code as seen at the bottom of the page they do not show. 我想将分页添加到产品视图中,但是当我添加分页编号代码时(如页面底部所示),它们不会显示。
I'm also aware that I should be using mysqli but I want to get this working first before moving over. 我也知道我应该使用mysqli,但我想先进行此操作,然后再进行迁移。
Thanks. 谢谢。
Show Product 显示产品
<div class="link" style="width:100%; height:100%; background-color: white">
<?php
include("../script/dbconnect.php");
include("../script/get_product.php");
$posts = get_posts(null, $_GET['id']);
foreach ( $posts as $post ) {
if ( ! category_exists('name', $post['name']) ) {
$post['name'] = 'Uncategorised';
}
?>
<ul class='featured'>
<li class='headhighlight'><?php echo $post['title']; ?></li>
<li class='pricehigh'><?php echo $post['price']; ?></li>
<li class='imagefeat'><img class='imagelink' src='<?php echo $post['picture']; ?>' alt='$name'></li>
</ul>
<?php
}
?>
</div>
get_product.php get_product.php
<?php
function get_posts($id = null, $cat_id = null) {
$posts = array();
//Pagination Code
$perpage = 10;
if(isset($_GET["page_num"]))
{
$page_num = intval($_GET["page_num"]);
}
else
{
$page_num = 1;
}
if ($page_num < 1)
{
$page_num = 1;
}
$calc = $perpage * $page_num;
$start = $calc - $perpage;
//End pagination code
$query ="SELECT `products`.`id` AS `name` , `products_cat`.`id` AS `category_id` , `products`.`name` AS `title` , `description` , `price` , `sale` , `picture`
FROM `products`
INNER JOIN `products_cat` ON `products`.`prod_id` = `products_cat`.`id` ";
if ( isset($id) ) {
$id = (int) $id;
$query .= " WHERE `products`.`id` = {$id}";
}
if ( isset($cat_id) ) {
$cat_id = (int) $cat_id;
$query .= " WHERE `products_cat`.`id` = {$cat_id}";
}
$query .= " ORDER BY `products`.`price` DESC Limit $start, $perpage";
$query = mysql_query($query);
echo mysql_error();
while ( $row = mysql_fetch_assoc($query) ) {
$posts[] = $row;
}
return $posts;
}
Pagination Code - to add page numbers - would be placed in showproduct.php 分页代码-添加页码-将放置在showproduct.php中
<p class="pagination">
<?php
if(isset($page_num))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products");
$rows = mysql_num_rows($result);
if($rows)
{
$rs = mysql_fetch_array($result);
$total = $rs["Total"];
}
$totalPages = ceil($total / $perpage);
if($page_num <=1 )
{
echo '<span id="page_links" style="font-weight:bold;"> < </span>';
}
else
{
$j = $page_num - 1;
echo '<span><a id="page_a_link" href="../admin/admin.master.php?page=list_products.php&page_num=' . $j . '"> < </a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page_num)
{
echo '<span><a href="../admin/admin.master.php?page=list_products.php&page_num=' .$i. '" id="page_a_link">' . $i . '</a></span>';
}
else
{
echo '<span id="page_links" style="font-weight:bold;">' . $i . '</span>';
}
}
if($page_num == $totalPages )
{
echo '<span id="page_links" style="font-weight:bold;">Next ></span>';
}
else
{
$j = $page_num + 1;
echo '<span><a href="../admin/admin.master.php?page=list_products.php&page_num=' .$j. '" id="page_a_link"> > </a></span>';
}
}
?>
</p>
$posts = get_posts(null, $_GET['id']);
$result = mysql_query($posts);
In the get_posts
function you declare this variable as an array, fill it from a query result and then you return it... so it will never work. 在
get_posts
函数中,将此变量声明为数组,从查询结果中填充它,然后返回它...因此它将永远无法工作。 What were you trying to do here? 您想在这里做什么? You have to pass a string to the mysql_query function with correct MySQL query structure.
您必须使用正确的MySQL查询结构将字符串传递给mysql_query函数。
EDIT: adding the part of the code that is giving problems, and a possible fix. 编辑:添加部分代码,它会带来问题,并可能修复。
$posts = get_posts(null, $_GET['id']);
$i = 0;
foreach ($posts as $post){
....
does it work this way? 这样行吗? You already did the query with the LIMIT inside the get_posts function and returned the data.
您已经使用get_posts函数中的LIMIT进行查询并返回了数据。
$posts = get_posts(null, $_GET['id']);
returns array, of something. 返回数组。 After it you trying to make mysql_query(array);
之后,您尝试制作mysql_query(array); Try to make
尝试使
var_dump($posts); and copy print here. Or try next:
$posts = get_posts(null, $_GET['id']);
foreach($posts as $post){
$result[] = mysql_query($posts);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.