pagination problem

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
esandra
Forum Newbie
Posts: 24
Joined: Sun Aug 30, 2009 11:11 pm

pagination problem

Post by esandra »

i really can't tell what's wrong with my pagination script..to say the least it isn't working.when i click a number that (the pagination) it wont show me the files that its supposed to. :shrug: ive been looking at this thing the whole week i cant find out what's wrong with this thing

if((isset($_POST['tcl'])) && (isset($_POST['date']))){ //main, disregard top 2 same posts
$tcl=$_POST['tcl'];
$date=$_POST['date'];

//pagination
$page_name="view.php";

$start=$_GET['start']; //global variable if OFF
if(!($start > 0)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start -0);
$limit = 1;

$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

$query2=" select * from arrastre where `tcl` = '" . $tcl. "' and `date` = '" . $date . "'";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);

$query = "select * from `arrastre` where `tcl` = '" . $tcl. "' and `date` = '" . $date . "' order by `tcl` asc";
$result = mysql_query($query) or die(mysql_error());

$orno=$row['orno'];

?>//this is my display
<tr>
<td height="20" align="center"><?php echo $orno;?>
</tr>
<?php }

?>
</table>
</center>
<div align="center">
<?php
$p_limit=5; // This should be more than $limit and set to a value for which links are to be breaked
$p_f=$_GET['p_f']; // To take care global variable if OFF
if(!($p_f > 0)) { // This variable is set to zero for the first page
$p_f = 0;
}
$p_fwd=$p_f+$p_limit;
$p_back=$p_f-$p_limit;
//////////// End of variables for advance paging ///////////////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'>PREV $p_limit</a>"; }
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
elseif($back >=0 and ($back >=$p_f)) {
print "<a href='$page_name?start=$back&p_f=$p_f'>PREV</a>";
}
//////////////// Let us display the page links at center. We will not display the current page as a link ///////////
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
if($i <> $eu){
$i2=$i+$p_f;
echo " <a href='$page_name?start=$i&p_f=$p_f'>$i</a> ";
}
else { echo "<font face='Verdana' size='2' color=red>$i</font>";} /// Current page is not displayed as link and given font color red
}
///////////// If not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($p_f+$p_limit)) {
print "<a href='$page_name?start=$next&p_f=$p_f'>NEXT</a>";}
elseif($p_fwd < $nume){
print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'>NEXT $p_limit</a>";
}}
?></div>
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: pagination problem

Post by mecha_godzilla »

I'm not sure whether this will be helpful or not but here's how I do my pagination:

Code: Select all

$rows_per_page = 10;

$sql = "SELECT * FROM products WHERE product_name LIKE '%$search_string%'";
$result = mysql_query($sql, $conn) or MySQL_query_failure(mysql_error());
$number_of_results_in_total = mysql_num_rows($result);
        
$maximum_number_of_pages = ceil($number_of_results_in_total / $rows_per_page);
        
if ($GLOBALS['query_page'] > $maximum_number_of_pages || $GLOBALS['query_page'] == NULL) {
    
    // This is a $_GET value taken from the URL and is used to stop someone messing about with the URL
    // URL looks like search.php when the first page of results is displayed for the first time
    // URL looks like search.php?query_page=1 when the first page of results is displayed for the second time
    $GLOBALS['query_page'] = 1;
        
}

$offset = ($GLOBALS['query_page'] - 1) * $rows_per_page;

$sql = "SELECT * FROM products WHERE product_name LIKE '%$search_string%' LIMIT $offset, $rows_per_page";
$result = mysql_query($sql, $conn) or MySQL_query_failure(mysql_error());
$number_of_results = mysql_num_rows($result);

if ($number_of_results_in_total <= $rows_per_page) {
            
    if ($number_of_results == 0) {
        $GLOBALS['pagination'] = 'No results found';
    } else if ($number_of_results == 1) {
        $GLOBALS['pagination'] = 'Now showing 1 of ' . $number_of_results_in_total . ' result in total';
    } else {
        $GLOBALS['pagination'] = 'Now showing 1-' . $number_of_results_in_total . ' of ' . $number_of_results_in_total . ' results in total';
    }
        
} else {
            
    for ($i = 1; $i <= $maximum_number_of_pages; $i++) {
            
        if ($i == $GLOBALS['query_page']) {
            $GLOBALS['search_links'] .= '<li>' . $i . '</li>';
        } else {
        $GLOBALS['search_links'] .= '<li><a href="search.php?query_page=' . $i . '">' . $i . '</a></li>';
        }
            
    }
            
    if ($offset == 0) {
        $GLOBALS['pagination'] = 'Now showing 1-' . $number_of_results . ' of ' . $number_of_results_in_total . ' results in total';
    } else if ($offset + $rows_per_page > $number_of_results_in_total) {
        $GLOBALS['pagination'] = 'Now showing ' . $offset . '-' . $number_of_results_in_total . ' of ' . $number_of_results_in_total . ' results in total';
    } else {
        $GLOBALS['pagination'] = 'Now showing ' . $offset . '-' . ($offset + $rows_per_page) . ' of ' . $number_of_results_in_total . ' results in total';
    }
            
}
Note that I display the actual search results later on in the script, and also that MySQL_query_failure() is a custom handler that I've written to replace the die() command that's usually used.

HTH,

Mecha Godzilla
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: pagination problem

Post by social_experiment »

esandra wrote:when i click a number that (the pagination) it wont show me the files that its supposed to.
The crucial thing to get pagination working is to use LIMIT in your sql query. Your query doesn't show it anywhere. Im guessing it works for the first page (let me know please) though?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply