problem with php_pagination

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
izlik
Forum Newbie
Posts: 3
Joined: Sun Apr 01, 2007 10:20 am

problem with php_pagination

Post by izlik »

Hello.

i have been trying to make a tag system for my page, and sofar it has been working with a few kinks. i have also been trying to make it page based 30 images per page and so on. I managed to get it to work but the the images shown has now become messed up, and when you press "next page" on the bottom everyting becomes messed up and i dont know how to solve it and hope that someone can help me, the code in the notpaste link bellow and the example link is showing the problem for one of my tags on the site.

Code: http://nopaste.php-quake.net/9687 (Everah | Code snagged from link and posted below)
Everah | It's ok, you can post code here. We don't mind:

Code: Select all

<?
include "includes/inc.php";

require_once("header.php");

$template->set_filenames(array(
    'body' => 'tags.html')
    );
    
?>

<?php

$con = mysql_connect("localhost","asd","das") OR die('Could not connect: ' . mysql_error());
mysql_select_db("asd", $con);

//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Edit $result to be your query 
$result = mysql_query("
    SELECT *
    FROM `images` 
    WHERE `tags`
    LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
    ORDER BY views
    $max
")
OR die(mysql_error());
$rows = mysql_num_rows($result); 

//This is the number of results displayed per page
$page_rows = 30; 

$last = ceil($rows/$page_rows); 

pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
} 

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

while($row = mysql_fetch_array($result))
{
    
    echo '<div style="float:left;width:25%"><a href="http://mydomain.net/show.php/' .$row['id'].'_' .$row['name'].'"><img src="http://www.mydomain.net/out.php/t' .$row['id'].'_' .$row['name'].'"></a></div>'; 
    echo "<br>\n";
 
}    

?>
<div style="clear:both"></div>
<?    
// This shows the user what page they are on, and the total number of pages
    echo " --Page $pagenum of $last-- <p>";
   
    if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo " ---- ";

if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
<?
include "footer.php";
?>
example: http://filefrog.net/tags.php?games
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Pagination can get complicated fast because you are dealing with many related values. Typically in PHP you would ether use the session or pass all the values in the query string.

Usually you would initialize everything the first time in. This would be determined because no parameters were passed. You will want to get the total number of rows using something like:

Code: Select all

$result = mysql_query("
    SELECT COUNT(*) AS total_rows
    FROM `images`
    WHERE `tags`LIKE '%" . mysql_real_escape_string($_GET['tag']) . "%'
")
Then you just need to pass the page number and somehow maintain the page size and total rows. All of those together give you what you need to query just the records for the requested page and deal with being at the beginning or end of the list. Finally use the LIMIT clause in MySQL to fetch only the records for the current page.
(#10850)
Post Reply