Jump to content

pagination problem


Noskiw

Recommended Posts

here is my topic.php script

 

<?php

$id = $_GET['id'];

$page = (!$_GET['page'] || $_GET['page'] < 0) ? "1" : $_GET['page']; 
$page = ceil($page);
$limit = 10;

$start = $limit;
$end = $page*$limit-($limit); 

if(isset($id)){
$sql = "SELECT * FROM forum_topics WHERE id='".$id."'";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($res) == 0){
echo "This topic does not exist!\n";
}else{
$row = mysql_fetch_assoc($res);
$sql2 = "SELECT admin FROM forum_sub_cats WHERE id='".$row['cid']."'";
$res2 = mysql_query($sql2) or die(mysql_error());
$row2 = mysql_fetch_assoc($res2);
if($row2['admin'] == 1 && $admin_user_level == 0){
echo "You cannot view this topic because you are not an admin!\n";
}else{
$a = (isa($row['uid'])) ? "<font style=\"color:#800000;\">ADMIN</font>" : "";
echo "<table border=\"0\" width=\"100%\" cellspacing=\"3\" cellpadding=\"3\">\n";
echo "<tr><td colspan=\"2\" align=\"left\"  class=\"forum_header\"><b><font size=\"3\">".$row['title']."</font></b><font size=\"3\"> - Posted On: <em>".$row['date']."</em></font></td></tr>\n";
echo "<tr><td align=\"left\" width=\"15%\" valign=\"top\"  class=\"forum_header\">".uid($row['uid'], true)."<br>Posts: 0<br>".$a."</td>";
echo "<td align=\"left\" valign=\"top\" class=\"forum_header\">";
echo topic($row['message']);
echo "</td>\n";
echo "</tr>\n";

$amount_check = "SELECT * FROM `forum_replies` WHERE `tid`='".$tid."'";
$amount_check_res = mysql_query($amount_check) or die(mysql_error());
$amount_count = mysql_num_rows($amount_check_res); 
$pages = ceil($amount_count/$limit); 

$previous = ($page-1 <= 0) ? "« Prev" : "<a href=\"./forum-index.php?act=topic&id=".$id."&page=".($page-1)."\">« Prev</a>";
$nextpage = ($page+1 > $pages) ? "Next »" : "<a href=\"./forum-index.php?act=topic&id=".$id."&page=".($page+1)."\">Next »</a>";
echo "<tr><td align=\"right\" colspan=\"2\" class=\"forum_header\">\n";
echo "Pages: ";
echo $previous;
for($i=1;$i<=$pages;$i++){
    $href = ($page == $i) ? " ".$i." " : " <a href=\"./pagetest.php?page=".$i."\">".$i."</a> ";
    
    echo $href;
}
echo $nextpage; 
echo "</td></tr>\n";

$select_sql = "SELECT * FROM `forum_replies` ORDER BY id ASC LIMIT ".$end.",".$start."";
$select_res = mysql_query($select_sql) or die(mysql_error()); 

echo "<form method=\"post\" action=\"./forum-index.php?act=reply&id=".$row['id']."\">\n";
echo "<tr><td colspan=\"2\" align=\"center\"><textarea style=\"width:90%\" name=\"reply\"></textarea><br><input type=\"submit\" name=\"submit\" value=\"add reply!\" style=\"width:90%\" /></td></tr>\n";

echo "</table>\n";
}
}
}else{
echo "Please view a valid topic!\n";
}

?>

 

it comes out without the page number and no link to the next page.

Link to comment
Share on other sites

Okay, I'm looking at your code now, and am curious about some things.

SELECT * FROM `forum_replies` WHERE `tid`='".$tid."'

I'm going to guess tid is topic ID? But where do you set $tid?

 

$select_sql = "SELECT * FROM `forum_replies` ORDER BY id ASC LIMIT ".$end.",".$limit."";
	$select_res = mysql_query($select_sql) or die(mysql_error()); 

What is that being called for? Nothing after it is using it

Link to comment
Share on other sites

I've just grabbed this from my website...

<?php
  $intCounter=0;
  $intMax=30; //HOW MANY ENTRIES RETURNED ON EACH PAGE
  $intPage=intval($_GET['page']); //GET CURRENT PAGE NUMBER FROM URL
  $strSQL="SELECT * FROM table";
  $intTotalFound=mysql_num_rows(mysql_query($sql));
  $intTotalPages=ceil($intTotalFound/$intMax);
  $intStart=(($intPage*$intMax)-$intMax);
  if ($intPage>$intTotalPages) {$intPage=$intTotalPages;}
  $query=mysql_query($strSQL." LIMIT ".$intStart.",".$intMax);
  while ($fetch=mysql_fetch_assoc($query)) {
    print '<tr><td>'.$fetch['title'].'</td></tr>';
    $intCounter++;
  }
  if ($intCounter==0) {
    print '<tr><td align="center">Nothing found - please widen your search criteria</td></tr>';
  } else {
    print '<tr><td align="center">Found: '.number_format($intTotalFound).' (showing '.number_format($intStart+1).' to '.number_format($intStart+$intCounter).')</td></tr>';
  }
?>

All that does is generate each page depending on page= in the URL. For example, page=2 shows page 2.

 

Later, you can use something like this to generate the HTML to make your "previous", "next" and page number links:

<?php
  $strURL='&search='.(empty($strFind) ? '' : $strFind).'&os='.($intOS==0 ? 0 : $intOS).'&genre='.($intGenre==0 ? 0 : $intGenre).'&mode='.($intFindMode==0 ? 0 : $intFindMode);
  if ($intTotalMatched>$intMax) {
?>
        <br />
        <table width="870" cellspacing="1" cellpadding="0" class="tblmain">
          <tr>
            <td style="font: 12px arial; text-align: center" width="30"><?=($intPage>1 ? '<a href="demos.php?page='.($intPage-1).$strURL.'" class="link"><<<</a>' : '<<<')?></td>
            <td style="font: 12px arial; text-align: center"><?php for ($i=1;$i<=$intTotalFound;$i++) {echo ($i==$intPage ? '<strong>'.$i.'</strong> ' : '<a href="demos.php?page='.$i.$strURL.'" class="link">'.$i.'</a> ');} ?></td>
            <td style="font: 12px arial; text-align: center" width="30"><?=($intPage<$intTotalFound ? '<a href="demos.php?page='.($intPage+1).$strURL.'" class="link">>>></a>' : '>>>')?></td>
          </tr>
        </table>
<?php } ?>

If you want to see what that looks like: http://www.pictureinthesky.net

 

Click either "Demos", "Games" or "Applications in the menu to see the pages.

Link to comment
Share on other sites

i can't make a new db or table cause i only get a few on the server i'm working on. could you help me, say like edit bits of the code, just to help me a little, please?

 

You don't need to make a new database.  Just read the tutorial so you can get the idea of what you're supposed to do.  No I won't rewrite your script for you.  I'm here to teach people how to catch fish, not do their fishing for them.

Link to comment
Share on other sites

I've just grabbed this from my website...

<?php
  $intCounter=0;
  $intMax=30; //HOW MANY ENTRIES RETURNED ON EACH PAGE
  $intPage=intval($_GET['page']); //GET CURRENT PAGE NUMBER FROM URL
  $strSQL="SELECT * FROM table";
  $intTotalFound=mysql_num_rows(mysql_query($sql));
  $intTotalPages=ceil($intTotalFound/$intMax);
  $intStart=(($intPage*$intMax)-$intMax);
  if ($intPage>$intTotalPages) {$intPage=$intTotalPages;}
  $query=mysql_query($strSQL." LIMIT ".$intStart.",".$intMax);
  while ($fetch=mysql_fetch_assoc($query)) {
    print '<tr><td>'.$fetch['title'].'</td></tr>';
    $intCounter++;
  }
  if ($intCounter==0) {
    print '<tr><td align="center">Nothing found - please widen your search criteria</td></tr>';
  } else {
    print '<tr><td align="center">Found: '.number_format($intTotalFound).' (showing '.number_format($intStart+1).' to '.number_format($intStart+$intCounter).')</td></tr>';
  }
?>

All that does is generate each page depending on page= in the URL. For example, page=2 shows page 2.

 

Later, you can use something like this to generate the HTML to make your "previous", "next" and page number links:

<?php
  $strURL='&search='.(empty($strFind) ? '' : $strFind).'&os='.($intOS==0 ? 0 : $intOS).'&genre='.($intGenre==0 ? 0 : $intGenre).'&mode='.($intFindMode==0 ? 0 : $intFindMode);
  if ($intTotalMatched>$intMax) {
?>
        <br />
        <table width="870" cellspacing="1" cellpadding="0" class="tblmain">
          <tr>
            <td style="font: 12px arial; text-align: center" width="30"><?=($intPage>1 ? '<a href="demos.php?page='.($intPage-1).$strURL.'" class="link"><<<</a>' : '<<<')?></td>
            <td style="font: 12px arial; text-align: center"><?php for ($i=1;$i<=$intTotalFound;$i++) {echo ($i==$intPage ? '<strong>'.$i.'</strong> ' : '<a href="demos.php?page='.$i.$strURL.'" class="link">'.$i.'</a> ');} ?></td>
            <td style="font: 12px arial; text-align: center" width="30"><?=($intPage<$intTotalFound ? '<a href="demos.php?page='.($intPage+1).$strURL.'" class="link">>>></a>' : '>>>')?></td>
          </tr>
        </table>
<?php } ?>

If you want to see what that looks like: http://www.pictureinthesky.net

 

Click either "Demos", "Games" or "Applications in the menu to see the pages.

 

yea thats all helpful, but i don't know how to change it so it fits into my page. and i don't want 2 seperate files for one part.

Link to comment
Share on other sites

As Crayon's already said, I'm not going to write it for you and I very much doubt anyone else will either.

 

The code I posted above is from the same script - I only posted two snippets - the bits that you need. The rest of the script does other stuff not relevant to what you need or it is the HTML itself that I've used for the rest of the page.

 

Part of the clue is in the variable names. I strongly believe in not using variable names that mean nothing.

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.