I have been working on this all day and this is my second post today here. I have managed to create pagination and display the page numbers on certain pages. When I have introduced it to my products page I have struggled to add the variable ID cat_id
. If I manually add a category ID it displays the first page but when I select the second it loses the list as the query doesn't contain the variable.
I have tried "SELECT COUNT(*) As Total FROM products WHERE prod_id = {$cat_id}"
but do I then need to reference that in showproduct.php? It displays the paginated data but page list does not display and has error Undefined variable: cat_id
. Whats the best way to define the variable here.
This is starting to frustrate me as it shouldn't have taken as long as it has and I still have jquery to add. Plus I know the mysql is outdated but I would like to get it working first.
So my question is how to add the {$cat_id}
to the COUNT query.
showproduct.php
.......
<p class="pagination">
<?php
if(isset($page_num))
{
$result = mysql_query("SELECT COUNT(*) As Total FROM products WHERE prod_id = {$cat_id}");
$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="../anonymous/anonymous.master.php?page=showproduct.php&page_num=' . $j . '"> < </a></span>';
}
for($i=1; $i <= $totalPages; $i++)
{
if($i<>$page_num)
{
echo '<span><a href="../anonymous/anonymous.master.php?page=showproduct.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="../anonymous/anonymous.master.php?page=showproduct.php&page_num=' .$j. '" id="page_a_link"> > </a></span>';
}
}
?>
</p>
......
Get Product.php
.....
<?php
function get_posts($id = null, $cat_id = null) {
$posts = array();
$perpage = 8;
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;
$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;
}
....
Table View
CREATE TABLE `products` (
`id` int(100) NOT NULL AUTO_INCREMENT,
`prod_id` int(15) NOT NULL,
`prod_sub_id` int(15) DEFAULT NULL,
`description` varchar(3000) NOT NULL,
`sale` varchar(100) DEFAULT NULL,
`name` varchar(100) NOT NULL,
`price` varchar(100) NOT NULL,
`picture` varchar(100) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=60 DEFAULT CHARSET=latin1