Fanya Fanya - 3 months ago 14
MySQL Question

How to create a pagination by selecting categories?

I'm trying learn how to make a simple pagination.. for the first launch (index.php) it's success,
then i click the button CATEGORIES to show the products by category, it's still success..
But when i click the next page, let say page number 5, it's actually jump to the page number 5
but not in the CATEGORIES, it's back to the main page. I know it's because of this echo a href='index.php',
but i'm a beginner here, just start with php and javascript about a week ago,
so i need you "JUST CORRECT MY CODE", please don't give me a bunch of code like javascript that i'm just start to learning.

<!--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>";
}
}
?>

Answer

You have to modify getCatPro() and concatenate the cat parameter to the links. For example:

echo "<a href='index.php?page=".$i."&cat=".$cat_id."'>".$i."</a> ";

Hope it helps.