Jump to content

Recommended Posts

I am using the code in the tutorial for pagination.  I nearly have it working, except the anchors are not working.  The PREVIOUS 123 NEXT are appearing, but they are not linked to anything, just plain text.  Can anyone see what I need to change.  Thanks for the time

[code]
if ($count == $girlsPerRaw) { $rows++; ?></td></tr><?php }
}
if($page != 1){ 
        $pageprev = $page--;
       
        echo("<a href=\"$PHP_SELF?page=$pageprev\">PREV".$limit."</a> "); 
    }else{
        echo("PREV".$limit." ");
    }

    $numofpages = $totalrows / $limit; 
   
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>"); 
    }else{
        echo("NEXT".$limit); 
    }
   
    mysql_free_result($result);

?>
   

[/code]

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

Here's what I use:

[code]

function paginate($numMatches, $display, $pg, $total) {
  /* make sure pagination doesn't interfere with other query
string variables */
  if(isset($_SERVER['QUERY_STRING']) && trim(
    $_SERVER['QUERY_STRING']) != '') {
    if(stristr($_SERVER['QUERY_STRING'], 'pg='))
      $query_str = '?'.preg_replace('/pg=\d+/', 'pg=',
        $_SERVER['QUERY_STRING']);
    else
      $query_str = '?'.$_SERVER['QUERY_STRING'].'&pg=';
  } else
    $query_str = '?pg=';
   
  /* find out how many pages we have */
  $pages = ($total <= $display) ? 1 : ceil($total / $display);
   
  /* create the links */
  $first = '<a href="'.$_SERVER['PHP_SELF'].$query_str.'1">First
</a>';
  $prev = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg - 1).'">
Previous</a>';
  $next = '<a href="'.$_SERVER['PHP_SELF'].$query_str.($pg + 1).'">
Next</a>';
  $last = '<a href="'.$_SERVER['PHP_SELF'].$query_str.$pages.'">
Last</a>';
 
  /* display opening navigation */
  echo "<div id=\"searchLinks\" style=\"background:#131aa0;a:link:color:#FFFFFF;\">
<center>
<font color=\"white\">
<font size=\"4\"><br>Search Results:</font><br /><br /></font></center>
<div style=\"background:#0057FC; border:2px solid #000000;font-size:11px;\">
<table border=\"1\"><tr><td align=\"center\" width=\"10%\"><font size=\"1\"

color=\"white\"><b>$numMatches Matches Found.</b></font></td><td align=\"center\"

width=\"10%\"><font

size=\"1\" color=\"white\"><b>";
  echo ($pg > 1) ? "$first &nbsp; $prev  &nbsp;" : 'First &nbsp; Previous &nbsp;';
 
  /* limit the number of page links displayed */
  $begin = $pg - 4;
  while($begin < 1)
    $begin++;
  $end = $pg + 4;
  while($end > $pages)
    $end--;
  for($i=$begin; $i<=$end; $i++)
    echo ($i == $pg) ? ' ['.$i.'] ' : ' <a href="'.
      $_SERVER['PHP_SELF'].$query_str.$i.'">'.$i.'</a> ';
   
  /* display ending navigation */
  echo ($pg < $pages) ? "  &nbsp; $next &nbsp; $last" : ' &nbsp; Next &nbsp; Last';
  echo "</b></font></td></tr></table></div>";
}



/* set pagination variables */
$display = 5;
$pg = (isset($_REQUEST['pg']) && ctype_digit($_REQUEST['pg'])) ?
  $_REQUEST['pg'] : 1;
$start = $display * $pg - $display;

/* paginating from a database */
mysql_connect("localhost","root","");
mysql_select_db("books");

$result = mysql_query("SELECT count(*) FROM main WHERE ISBN='$ISBN'");
$total = mysql_result($result, 0);
$query = "SELECT * from main WHERE ISBN='$ISBN' ORDER BY 'ID'
ASC LIMIT $start, $display";
$news = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($news);

if($rows >= 1) {

paginate($total, $display, $pg, $total);

}[/code]

keep in mind i am cutting out some from my code that i believe u don't need
Link to comment
https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155312
Share on other sites

$PHP_SELF is an environment var that holds the current script's path/name. You really should be using $_SERVER['PHP_SELF'] instead of $PHP_SELF, though.  However, this would not be what is causing your PREV 123 NEXT text to not become links.


Are you sure that $page variable is getting passed right?  somewhere before you use this $page variable, you should have something like this:

[code=php:0]
$page = $_GET['page'];
[/code]

right?
Link to comment
https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155590
Share on other sites

This is the easiest pagination code i've ever worked with. First an example of how it's laid out in a script:

[code]<?php
// product display page
session_start();
include 'db-config.inc.php';
include 'header.inc.php';
include 'db_cart_functions.php';

db_connect();

// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 20;

// Figure out the limit for the query based on the current page number.
$from = (($page * $max_results) - $max_results);

$sql = "SELECT * FROM products ORDER BY id LIMIT $from, $max_results";
$results = mysql_query($sql) or die(mysql_error());
$num_rows = mysql_num_rows($results);

// output our display of the products
echo "<table width='600' border='0' align='center'>";
echo "<tr><td align='center'>There are $num_rows product available.\n</td></tr>";
echo "<tr><td>";
// Build Page Number Hyperlinks
echo "<center>Select a Page<br /><br />";

for($i = 1; $i <= $total_pages; $i++){
    echo " | ";
    if(($page) == $i){
        echo "Page $i ";
        } else {
            echo "<a class=\"body\" href=\"".$_SERVER['PHP_SELF']."?page=$i\">Page $i</a> ";
    }
}

echo "</center><p>\n";
echo "</td></tr>";

// loop through our results
while ($row = mysql_fetch_array($results))
{
  echo "<tr>
  <td>".$row['image']."</td><td>".$row['product_name']."<br>".$row['product_desc']."</td>
<td><form action='addtocart.php?product_id=$product_id' method='POST'><input type='text' name='quantity' size='4'> <input type='submit' name='addtocart' value='Add To Cart'></form></td>
</tr>";
}
echo "</table>\n";
include 'footer.php';
?>[/code]

The way it's set up is to display at the top of the results, centered. And now just the pagination code so you can have it separate:

[code]// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 75;

// Figure out the limit for the query based on the current page number.
$from = (($page * $max_results) - $max_results);

//SQL clause
ORDER BY id LIMIT $from, $max_results

// Build Page Number Hyperlinks
echo "<center>Select a Page<br /><br />";

for($i = 1; $i <= $total_pages; $i++){
    echo " | ";
    if(($page) == $i){
        echo "Page $i ";
        } else {
            echo "<a class=\"body\" href=\"".$_SERVER['PHP_SELF']."?page=$i\">Page $i</a> ";
    }
}

echo "</center><p>\n";[/code]
Link to comment
https://forums.phpfreaks.com/topic/33231-pagination/#findComment-155649
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.