0

我觉得我最近问了很多问题,如果这相对简单,我很抱歉,但我正在努力让它正常工作。

我想在我的产品视图中添加分页,但是当我添加页面底部的分页编号代码时,它们没有显示。

我也知道我应该使用 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>
4

2 回答 2

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 进行了查询并返回了数据。

于 2013-01-02T12:15:47.900 回答
0
$posts = get_posts(null, $_GET['id']); 

返回数组,一些东西。之后,您尝试制作 mysql_query(array); 尝试制作

var_dump($posts); and copy print here. Or try next: 
$posts = get_posts(null, $_GET['id']);
foreach($posts as $post){
$result[] = mysql_query($posts);
}
于 2013-01-02T12:17:04.217 回答