我觉得我最近问了很多问题,如果这个问题比较简单,我很抱歉,但我很难让这个问题正常运作。
我想在我的产品视图中添加分页,但是当我添加分页编号代码时,如页面底部所示,它们不会显示。
我也知道我应该使用mysqli,但我想在移动之前先让它工作。
感谢。
显示产品
<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
<?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;
}
分页代码 - 添加页码 - 将放在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>
答案 0 :(得分:0)
$posts = get_posts(null, $_GET['id']);
$result = mysql_query($posts);
在get_posts
函数中,您将此变量声明为数组,从查询结果中填充它然后返回它...所以它永远不会工作。你在这做什么?您必须使用正确的MySQL查询结构将字符串传递给mysql_query函数。
编辑:添加产生问题的代码部分,以及可能的解决方法。
$posts = get_posts(null, $_GET['id']);
$i = 0;
foreach ($posts as $post){
....
这样做有用吗?您已经使用get_posts函数中的LIMIT进行了查询并返回了数据。
答案 1 :(得分:0)
$posts = get_posts(null, $_GET['id']);
返回数组。之后你试图制作mysql_query(数组);尝试制作
var_dump($posts); and copy print here. Or try next:
$posts = get_posts(null, $_GET['id']);
foreach($posts as $post){
$result[] = mysql_query($posts);
}