Jump to content

pagination... Just math problems i think


blueman378

Recommended Posts

hi guys, well heres the relevant code:

<?php
$root = "../";
$tbl_name="forum_question"; // Table name

// get value of id that sent from address bar
$page=$_GET['page'];
if (isset($page)) {
$page=$_GET['page'];
}
else{ $page=1;}
$answer_id = $_GET['answer_id'];

<?php
} 
//**EDIT TO YOUR TABLE NAME, ECT.
$tbl_name2="forum_answer"; // Switch to table "forum_answer"


$t = mysql_query("SELECT * FROM $tbl_name2 WHERE question_id='$id'"); 
  if(!$t) die(mysql_error()); 
    
$a                  = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = "5"; 
//if it is the first page set limit to 1 less than normal because of op being shown
if($page == 1) {
$limit--; }

//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { 
      $page = 1; //default 
} 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit          = $page * $limit - ($limit); 

     if($page >= 2)
     {
  echo("  <a href=?id=$id&action=view&page=1><< First </a> | ");
  }
//Display middle pages: **EDIT LINK PATH** 
     $pcount = $page - 2 ;
     if($pcount >= 1 && $pcount != 1)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | "); 
   }  
$pcount = $page - 1;
     if($pcount >= 2)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | ");
  }
  
  echo "<b>$page</b>";
  
  $pcount = $page + 1 ;
  if($pcount <= $total_pages -2)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | ");
}

  $pcount = $page + 2 ;
    if($pcount <= $total_pages -1)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | ");
}

if($page == $total_pages -2 )
     {
  echo("  <a href=?id=$id&action=view&page=$total_pages> </a> | ");
  }

     if($page != $total_pages)
     {
  echo("  <a href=?id=$id&action=view&page=$total_pages> Last >></a> | ");
  }

echo "</td></tr></table>";
$query3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($query3);

$rows=mysql_fetch_array($result3);
$view=$rows['view'];

// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$query4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($query4);
}

// count more value
$addview=$view+1;
$query5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($query5);

if ($_SESSION['loggedin'] == "1")
{
if (isset($_GET['advanced']))
{ ?>

 

i took out the stuff that doesnt matter,

but heres my problem,

there are 7 pages,

however when you are on page 1 the last >> button tries to take you to page 9 for some reason,

the second problem is that the rest works fine except for on page 5

when you are on page 5 everything is fine except that it shows

 

3 | 4 | 5 | Last >> |

when it should show

3 | 4 | 5 | 6 | Last >> |

 

im guessing its jsut cause my maths is bad

 

heres the enitre code incase it helps:

<?php
$root = "../";
$tbl_name="forum_question"; // Table name

// get value of id that sent from address bar
$id=$_GET['id'];
$qid=$_GET['id'];
$page=$_GET['page'];
if (isset($page)) {
$page=$_GET['page'];
}
else{ $page=1;}
$answer_id = $_GET['answer_id'];
// only show op if it is the first page
if($page == 1)
{
$query="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($query);

$rows=mysql_fetch_array($result);

$queryd="SELECT * FROM users WHERE username='$rows[name]'";
$resultd=mysql_query($queryd);

$rowd=mysql_fetch_array($resultd);
$q= $rows['detail'];
?>

<table width="920" border="0" align="left" cellpadding="0" cellspacing="1" class="forum">
<tr height="30" class="titles">
<td valign="top" colspan="2"><u><? echo $rows['topic']; ?></u></td>
</tr>
<tr>
<td bgcolor="#111111" style="padding:5px 5px 5px 5px;" width="14%" valign="top">

<a href="?action=viewprofile&pruname=<? echo $rows['name']; ?>"><? echo $rows['name']; ?></a><br />
<strong>Posted on:</strong><br><? echo $rows['datetime']; ?><br />
<strong>Posts:</strong> <? echo $rowd['posts']; ?><br />
<img border="1" src="avatars/<? echo $rowd['avatar']; ?>" width="60" height="60"></td>
<td colspan="3" valign="top" width="86%" style="padding:5px 5px 5px 5px;"><?=$q?></td>
</tr>
</table>


<?
} 
//**EDIT TO YOUR TABLE NAME, ECT.
$tbl_name2="forum_answer"; // Switch to table "forum_answer"


$t = mysql_query("SELECT * FROM $tbl_name2 WHERE question_id='$id'"); 
  if(!$t) die(mysql_error()); 
    
$a                  = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = "5"; 
//if it is the first page set limit to 1 less than normal because of op being shown
if($page == 1) {
$limit--; }

//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { 
      $page = 1; //default 
} 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit          = $page * $limit - ($limit); 

//query: **EDIT TO YOUR TABLE NAME, ECT. 

$q = mysql_query("SELECT * FROM $tbl_name2 WHERE question_id='$id' ORDER BY a_id ASC LIMIT $set_limit, $limit"); 
  if(!$q) die(mysql_error()); 
     $err = mysql_num_rows($q); 
       if($err == 0) die("No matches met your criteria.");


//show data matching query: 



while($rows=mysql_fetch_array($q)){
$queryx="SELECT * FROM users WHERE username='$rows[a_name]'";
$resultx=mysql_query($queryx);

$rowx=mysql_fetch_array($resultx);

$a= $rows['a_answer'];
?>
<table width="920" border="0" align="left" cellpadding="0" cellspacing="2">
<tr><td colspan="2" width="100%"><hr style="color:#666666" /></td></tr>
<tr><td colspan="2"><? echo $rows['a_title'];?></td></tr>
<tr>
</tr>
<tr>
<td bgcolor="#111111" valign="top" style="padding:5px 5px 5px 5px;" width="14%">
<a href="?action=viewprofile&pruname=<? echo $rows['a_name']; ?>"><? echo $rows['a_name']; ?></a><br />
<strong>Posted on:</strong><br><? echo $rows['a_datetime']; ?><br />
<strong>Posts:</strong> <? echo $rowx['posts']; ?><br />
<img border="1" src="avatars/<? echo $rowx['avatar']; ?>" width="60" height="60"></td>
<td colspan="3" valign="top" width="86%" style="padding:5px 5px 5px 5px;">
<?=$a?><p><br /><? echo $rows['edited']; ?></td>
</tr>
<?php if($_SESSION['WS_username'] == $rows['a_name'] || $_SESSION['level'] == 3){?><tr align="right"><td></td><td><?php
if($_GET['confirm'] == 1 && $rows['a_id'] == $_GET['answer_id'])
{echo "<font color='red'>Delete:</font>"; ?> 
<a href="delete_answer.php?answer_id=<?=$_GET['answer_id']?>&id=<?=$_GET['id']?>" title="Delete" id="delete"><img border="0"src="<?=$root?>images/yes.png"></a> |
<a href="?action=view&id=<?=$_GET['id']?>" title="Cancel"><img border="0"src="<?=$root?>images/Delete.png"></a> <?php }
else
{ ?>
<a href="index.php?action=change&id=<?=$qid?>&answer_id=<?=$rows['a_id']?>" title="Modify your post"><img border="0" src="<?=$root?>images/edit.png"></a>
| 
<a href="?action=view&confirm=1&id=<?=$qid?>&answer_id=<?=$rows['a_id']?>#delete" title="Delete your post"><img border="0"src="<?=$root?>images/Delete.png"></a>
</td></tr><?php } } ?>
</table>

<?php

}
echo "<table width='100%'>
<tr><td>";
     if($page >= 2)
     {
  echo("  <a href=?id=$id&action=view&page=1><< First </a> | ");
  }
//Display middle pages: **EDIT LINK PATH** 
     $pcount = $page - 2 ;
     if($pcount >= 1 && $pcount != 1)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | "); 
   }  
$pcount = $page - 1;
     if($pcount >= 2)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | ");
  }
  
  echo "<b>$page</b>";
  
  $pcount = $page + 1 ;
  if($pcount <= $total_pages -2)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | ");
}

  $pcount = $page + 2 ;
    if($pcount <= $total_pages -1)
     {
  echo("  <a href=?id=$id&action=view&page=$pcount> $pcount </a> | ");
}

if($page == $total_pages -2 )
     {
  echo("  <a href=?id=$id&action=view&page=$total_pages> </a> | ");
  }

     if($page != $total_pages)
     {
  echo("  <a href=?id=$id&action=view&page=$total_pages> Last >></a> | ");
  }

echo "</td></tr></table>";
$query3="SELECT view FROM $tbl_name WHERE id='$id'";
$result3=mysql_query($query3);

$rows=mysql_fetch_array($result3);
$view=$rows['view'];

// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$query4="INSERT INTO $tbl_name(view) VALUES('$view') WHERE id='$id'";
$result4=mysql_query($query4);
}

// count more value
$addview=$view+1;
$query5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($query5);

if ($_SESSION['loggedin'] == "1")
{
if (isset($_GET['advanced']))
{ ?>
<div style="float:left; width:920px;">
<div class="form">
<table border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td colspan="2">
<b>Advanced reply:</b>
</td>
</tr>
<tr><td colspan="10" align="center">
<a name="advancededitor"></a>
<form name="form_name" method="post" action="add_answer.php">
<textarea name="a_answer" cols="135" rows="5" id="a_answer" onkeydown="limitText(this.form.a_answer,this.form.countdown,5000);" 
onkeyup="limitText(this.form.a_answer,this.form.countdown,5000);"></textarea></td>
</tr>
<tr>
<td align="center" colspan="10">

<input name="id" type="hidden" value="<? echo $id; ?>"><input type="submit" name="Submit" value="Submit" class="button"> | <input type="text" name="countdown" size="1" value="5000" readonly="readonly"> | <input type="reset" name="Submit2" value="Reset" class="button">

</td>
</tr>
</form>
</tr>
</table>
</div>
</div>
<?php
}
else
{
?>
<div style="float:left; width:920px;">
<div class="form">
<table border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>
<b>Quick reply:</b>
</td>
</tr>
<tr>
<form name="form1" method="post" action="add_answer.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1">
<tr>
<td><textarea name="a_answer" cols="135" rows="5" id="a_answer" onkeydown="limitText(this.form.a_answer,this.form.countdown,5000);" 
onkeyup="limitText(this.form.a_answer,this.form.countdown,5000);"></textarea></td>
</tr>
<tr>
<td align="center"><input name="id" type="hidden" value="<? echo $id; ?>"><input type="submit" name="Submit" value="Submit" class="button"> | <input type="text" name="countdown" size="1" value="5000" readonly="readonly"> | <input type="reset" name="Submit2" value="Reset" class="button"> | <a href="?action=view&id=<?=$id?>&advanced=advanced#advancededitor">Advanced Editor</a></td>
<td></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</div>
</div>
<?php }} ?>

Link to comment
Share on other sites

$total_items = mysql_num_rows($t); 
$limit = 5;
$total_pages = ceil(($total_items + 1) / $limit); 
$set_limit = $page * $limit - ($limit) - 1; 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_pages)) { 
      $page = 1; //default 
} 
if($page == 1) {
$limit--;
$set_limit++; }
etc.

Link to comment
Share on other sites

well i figured it out, because on page 1 we were showing the original post we took 1 off the limit so it only showed 4 instead of 5, but the total pages was being calculated based on te limit so it was putting it off, so i simply made it that it ran 2 different queries based on the page

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.