如何通过选择类别来创建分页?

时间:2016-08-19 19:32:41

标签: php mysql pagination

我正在尝试学习如何制作一个简单的分页..对于第一次启动(index.php)它的成功, 然后我点击按钮CATEGORIES按类别显示产品,它仍然成功.. 但是,当我点击下一页时,让我们说第5页,它实际上跳转到第5页 但不是在类别中,它会回到主页面。我知道这是因为这个回声是一个href =' index.php', 但我是一个初学者,大约一周前开始使用php和javascript, 所以我需要你"只是纠正我的代码",请不要给我一些像我刚刚开始学习的javascript代码。

<!--index.php-->
<!doctype html>
<?php include ("functions/functions.php"); ?>
<html>
<head>
<link rel="stylesheet" href="styles/style.css" type="text/css" media="all" />
</head>
<body>

    <!--NAV BUTTON-BY CATEGORY-->   
    <ul id="cats">
      <?php getCats(); ?>
    </ul>

    <!--SHOWING PRODUCTS-->
    <div id="wrapper">
        <?php getPro(); ?>  <!--FIRST LAUNCH (index.php)-->
        <?php getCatPro(); ?>  <!--GETTING THE PRODUCTS BY CATEGORIES-->
    </div>

</body>
</html>



//functions.php
<?php
$con = mysqli_connect("localhost","root","","justLearning");
if (mysqli_connect_errno())
  {
   echo "The connection was not established: " . mysqli_connect_error();
  }

//GETTING THE CATEGORIES (
function getCats(){
    global $con;
    $get_cats = "select * from categories";
    $run_cats = mysqli_query($con, $get_cats);
    while ($row_cats=mysqli_fetch_array($run_cats)){
        $cat_id = $row_cats['cat_id'];
        $cat_title = $row_cats['cat_title'];
    echo "<li><button><a href='index.php?cat=$cat_id'>$cat_title</a></button></li>";
    }
}

//FIRST LAUNCH (index.php)
function getPro(){
    if(!isset($_GET['cat'])){
    if(!isset($_GET['brand'])){
        global $con;
        $per_page=9;
        if (isset($_GET["page"])) {
        $page = $_GET["page"];
        }
        else {
        $page=1;
        }
        $start_from = ($page-1) * $per_page;
        $query = "SELECT * FROM products ORDER BY product_id DESC LIMIT $start_from, $per_page";
        $result = mysqli_query ($con, $query);
        while ($row = mysqli_fetch_array($result)){
            $pro_title = $row['product_title'];
            $pro_image = $row['product_image'];

            echo "<div id='product'>
                    <h3>$pro_title</h3>
                    <img src='admin_area/product_images/$pro_image' width='135' height='145'/>
                  </div>";
        }
            $query = "select * from products";
            $result = mysqli_query($con, $query);
            $total_records = mysqli_num_rows($result);
            $total_pages = ceil($total_records / $per_page);
                echo "<a href='index.php?page=1'>".'First Page'."</a> ";
                for ($i=1; $i<=$total_pages; $i++) {
                echo "<a href='index.php?page=".$i."'>".$i."</a> ";
                };
                echo "<a href='index.php?page=$total_pages'>".'Last Page'."</a>";
    }
    }
}


//GETTING THE PRODUCTS BY CATEGORIES
function getCatPro(){
    if(isset($_GET['cat'])){
        $cat_id = $_GET['cat'];
        global $con;
        $per_page=9;
        if (isset($_GET["page"])) {
        $page = $_GET["page"];
        }
        else {
        $page=1;
        }
        $start_from = ($page-1) * $per_page;
        $query = "SELECT * FROM products where product_cat='$cat_id' order by product_id DESC LIMIT $start_from, $per_page";
        $result = mysqli_query ($con, $query);
        while ($row_cat_pro=mysqli_fetch_array($result)){
            $pro_title = $row_cat_pro['product_title'];
            $pro_image = $row_cat_pro['product_image'];

            echo "<div id='product'>
                    <h3>$pro_title</h3>
                    <img src='admin_area/product_images/$pro_image' width='135' height='145'/>
                  </div>";
        }
        $query = "select * from products where product_cat='$cat_id'";
        $result = mysqli_query($con, $query);
        $total_records = mysqli_num_rows($result);
        $total_pages = ceil($total_records / $per_page);
            echo "<a href='index.php?page=1'>".'First Page'."</a> ";
            for ($i=1; $i<=$total_pages; $i++) {
            echo "<a href='index.php?page=".$i."'>".$i."</a> ";
            };
            echo "<a href='index.php?page=$total_pages'>".'Last Page'."</a>";
    }
}
?>

1 个答案:

答案 0 :(得分:1)

您必须修改getCatPro()并将MILLISECONDS_PER_SECOND参数连接到链接。例如:

cat

希望它有所帮助。