Jump to content

Pagination problem


dhimok

Recommended Posts

Hello everyone.

 

I have this function below which consist of looping thru product table. Now I have 2 tables:

categories which keeps category name and products which keeps the items. Product table keeps the category_id.

What I want to to is to make a pagination. It will actually work fine if i dont have to loop thru categories which are only parent to some subcategories. Anyway i am stuck and I would appriciate some help or ideas. Tell me if i wasnt specific enough. thanks in advance

 


function get_products($cat = 0) {

$rowsPerPage = 10; 
$pageNum = 1;
f(isset($_GET['page'])) $pageNum = $_GET['page'];
$offset = ($pageNum - 1) * $rowsPerPage;

if ($cat != 0) {
$result = mysql_query("SELECT * FROM products WHERE category_id = '$cat' LIMIT $offset, $rowsPerPage");

$total = mysql_result(mysql_query("SELECT count(*) FROM products category_id = '$cat'), 0, 0);
$maxPage = ceil($total/$rowsPerPage);

while {
... loop ...
} 

}

//get children
  $result = mysql_query("SELECT category_id FROM categories WHERE parent_id=$cat");
  while ($row = mysql_fetch_assoc($result)) {
    get_products($row['category_id']);
}

}

 

heres the pagination code I am planing to use

 

if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = " <a style=\"font-family: arial; font-size: 10px; font-weight: bold;\" href=\"?page=$page\">Prev</a> |";
    
    $first = " <a style=\"font-family: arial; font-size: 10px; font-weight: bold;\" href=\"?page=$page\">First</a> |";
} 
else
{
    $prev  = ' Prev | ';	// we're on page one, don't enable 'previous' link
    $first = ' First | ';	// nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNum < $maxPage)
{
    $page = $pageNum + 1;
    $next = "| <a style=\"font-family: arial; font-size: 10px; font-weight: bold;\" href=\"?page=$page\">Next</a> ";
    
    $last = "| <a style=\"font-family: arial; font-size: 10px; font-weight: bold;\" href=\"?page=$page\">Last</a> ";
} 
else
{
    $next = ' | Next ';		// we're on the last page, don't enable 'next' link
    $last = ' | Last '; 	// nor 'last page' link
}

echo $first . $prev . " <strong style=\"font-family: tahoma; color: #333333;\">PAGE ".$pageNum." / ".$maxPage."</strong> " . $next . $last;

Link to comment
https://forums.phpfreaks.com/topic/50117-pagination-problem/
Share on other sites

this my be very usefull to you, its very similar coding, and it work 100%

 


$rowsPerPage = 10;
$pageNum = 1;

if(isset($_GET['page']))
{
    $pageNum = $_GET['page'];
}
$offset = ($pageNum - 1) * $rowsPerPage;

$cat=$_GET['cat'];
	$result=mysql_query("SELECT * FROM movies WHERE cat='$cat' ORDER BY name LIMIT $offset, $rowsPerPage") or die(mysql_error());
	while($row=mysql_fetch_array($result))
	{
include "display_rec.php";
}

$query   = "SELECT COUNT(name) AS numrows FROM movies WHERE cat='$cat'";
$result  = mysql_query($query) or die('Error, query failed');
$row1     = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row1['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
$self = $_SERVER['PHP_SELF'];

if ($pageNum > 1)
{
$cat = $_GET['cat'];
    $page = $pageNum - 1;
    $prev = " <a href=\"$self?cat=$cat&page=$page\">[Prev]</a> ";
    
    $first = " <a href=\"$self?cat=$cat&page=1\">[First Page]</a> ";
} 
else
{
    $prev  = '';
    $first = '';
}

if ($pageNum < $maxPage)
{
    $cat = $_GET['cat'];
$page = $pageNum + 1;
    $next = " <a href=\"$self?cat=$cat&page=$page\">[Next]</a> ";
    
    $last = " <a href=\"$self?cat=$cat&page=$maxPage\">[Last Page]</a> ";
} 
else
{
    $next = '';
    $last = '';
}   
echo $first . $prev . " Page $pageNum of $maxPage " . $next . $last;

 

take a look.....

 

nita

Link to comment
https://forums.phpfreaks.com/topic/50117-pagination-problem/#findComment-246074
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.