Jump to content

pagination script


ted_chou12

Recommended Posts

[code]
<?php $diary = $username.'diary';
// connect to mysql below
require("../mysqlconnection2.php");

// Set how many rows to display on each page
$limit = 10;
// Query the database to get the total number of rows
$query_count = "SELECT * FROM $diary";
  $result_count = mysql_query($query_count) or die (mysql_error());
$totalrows = mysql_num_rows($result_count);
if(isset($_GET['page'])){    // Checks if the $page variable is empty (not set)
$page = $_GET['page'];      // If it is empty, we're on page 1
} else {
$page = 1;
}
// Set the start value
$startvalue = $page * $limit - ($limit);
// Query the database and set the start row and limit
$sql = "SELECT * FROM $diary LIMIT $startvalue, $limit";
  $res = mysql_query($sql) or die (mysql_error());
// Do a quick check to see if there are any records to display
if(mysql_num_rows($res) == 0){
echo "<p>There are no entries the blog right now!</p>";
}
// Start loop through records
while ($r = mysql_fetch_array($res)){

$entrydate = strtotime($r['entrydate']) + $row['timeoffset'];
$entrydate = date('M d, Y h:i A',$entrydate);

// Echo out the records with wordwrap, remove if you like
echo "<p><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr>
<td align=center width=\"10%\" height=\"50%\" bgcolor=\"#CCCCCC\">
<img src=\"images/". $r['entryicon'] .".gif\"></td>
<td align=left width=\"90%\" bgcolor=\"#CCCCCC\"><b>". $r['entrytitle'] ."</b></td>
<td align=center width=\"100%\" bgcolor=\"#CCCCCC\"><a href=\"blogedit.php?id=". $r['id']
."\">Edit</a></td></tr></table><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
<td align=left width=\"100%\" bgcolor=\"#f2f2f2\">Posted on: ".
$entrydate ."</td></table><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">
<tr><td align=left width=100% bgcolor=\"#CCCCCC\"><p>". $r['entrycontent']
."</p></td></tr></table></p><br />";}
// Start links for pages
echo "<p align=center>";

// Sets link for previous 25 and return to page 1
if($page != 1){
$pageprev = ($page - 1);
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\"><<</a>&nbsp;&nbsp;";
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV&nbsp;</a> ";
}else{
echo "PREV&nbsp;";
}
// Find out the total number of pages depending on the limit set
$numofpages = $totalrows / $limit;//##############################here, the total num rows is divided by the limit.
$totalpages = round($numofpages);
// Loop thru all the pages and echo out the links
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo "[".$i."] ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}

// Check for straglers after the limit blocks
if(($totalrows % $limit) != 0){
if($i == $page){
echo "[".$i."] ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
}
}
// Print out the Next 25 and Goto Last page links
if(($totalrows - ($limit * $page)) > 0){
$pagenext = ($page + 1);
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT&nbsp;</a>&nbsp;&nbsp;";
  echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a>&nbsp;&nbsp;";
}else{
echo("NEXT");
}
echo "</p>";
// Free results
mysql_free_result($res);
// Close mysql connection
mysql_close($mysql_conn);
?>
[/code]
i had 12 results in my database, and it appears this:

PREV [1] 2 NEXT   >> 

it looks right but this ">>" doesnt link right, it links to the first page, so I was thinking. see the orange text:
$numofpages = $totalrows / $limit; <<<
here is not right at all, say if we have 12 results, the totalrows will be 12 limit is 10 after the divions, it would be 1.2 and round... that doesnt go to 2 at all. How does that work????
Thanks
Ted
Link to comment
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.