...

View Full Version : categories pagination + for loop issues



n4te02
10-12-2009, 06:53 AM
Hey all,
I am hoping there is a simple solution behind this, but I've been trying for awhile now and cannot figure it out.

I have a website, where there are categories, which people will be able to click on a category. It then goes to: pages.php?action=get_entries&cat_id=categorynamegoeshere - This all works fine. But I cannot for the life of me figure out why the pagination isn't working properly on the category page. I have it to where it works to a point, not 100%. The page #'s are not being displayed for some reason. When I click "Next" it does go to the next page fine, but "First" does not become a link to go back to the first page.

If I take off the " WHERE `category` = '$cat_id'" part in the $sql, it displays pages, except it displays all of the pages, not just for that one category lol. I believe that WHERE statement needs to be there.. but again, the #'s will not display properly..

This pagination script is working fine, except on categories... from my testing it seems that the problem is the FOR LOOP (you'll see that alittle further down where $x variable is). Any help would be appreciated!



function pagination_catid()
{
$connection = db_connect();

/****** pagination ******/
// find out how many rows are in the table
$cat_id = $_REQUEST['cat_id'];
$sql = "SELECT COUNT(*) FROM `posts` WHERE `category` = '$cat_id'";
$result = mysql_query($sql, $connection);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 10;
//find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage']))
{
// cast var as int
$currentpage = $_GET['currentpage'];
}
else
{
// default page num
$currentpage = 1;
}

// if current page is greater than total pages...
if ($currentpage > $totalpages)
{
// set current page to last page
$currentpage = $totalpages;
}

// if current page is less than first page...
if ($currentpage < 1)
{
// set current page to first page
$currentpage = 1;
}

// the offset of the list, based on current page
//$offset = ($currentpage - 1) * $rowsperpage;

/****** build the pagination links ******/
// if not on page 1, don't show back links
if ($currentpage > 1)
{
// show << link to go back to page 1
echo "<div class='paginleft'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=1'>First</a></div>";
}
else
{
echo "<div class='paginleft'>First</div>";
}

// if not on last page, show forward and last page links
if ($currentpage != $totalpages)
{
// get next page
$nextpage = $currentpage + 1;
// echo forward link for next page
echo "<div class='paginright'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$nextpage'>Next</a></div>";
}
else
{
echo "<div class='paginright'>Next</div>";
}

// if not on page 1, don't show back links
if ($currentpage > 1)
{
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo "<div class='paginright'>|</div>";
echo "<div class='paginright'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$prevpage'>Previous</a></div>";
}
else
{
echo "<div class='paginright'>|</div>";
echo "<div class='paginright'>Previous</div>";
}

// range of num links to show
$range = 5;
$range2 = 4;

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range2) + 1); $x++)
{
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages))
{
// if we're on current page...
if ($x == $currentpage)
{
// highlight it but don't make a link
echo "<div class='paginx'><b>$x</b></div>";
}
// if not current page
else
{
// make it a link
echo "<div class='paginx'><a href='pages.php?action=get_entries&cat_id=$cat_id&currentpage=$x'>$x</a></div>";
}
}
}

if ($currentpage != $totalpages)
{
if ($totalpages > 5)
{
if ($totalpages < (($currentpage + $range2) + 1))
{
echo "";
}
else
{
echo "<div class='paginx'> ... </div> ";
echo "<div class='paginx'><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>$totalpages</a></div> ";
}
}
// echo forward link for lastpage
echo "<div class='paginlast'><a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a></div> ";
}
else
{
echo "<div class='paginlast'>Last</div>";
}
}

n4te02
10-12-2009, 08:41 PM
bump ^^

anyone? :(

n4te02
10-13-2009, 06:11 PM
I could really use some help on this ;x
anyone have any insight at all?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum