Jump to content

Recommended Posts

hi guys, well i decided at 6 am to try and implement pagination into my forum... as you can probably guess it didnt go well...

it semi works on topics that already have posts in them...

but it seems that a topic will have 30 posts, so it should say x of 3 pages, bit instead it says x of 4 pages but the moment you click on any of the page links it seems to fix itself and it says x of 3 pages,

 

problem 2 is that on topics which only have the original post it shows the original post and then it gives: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-10,10' at line 1

 

well heres the code...

 

<?php
$root = "../";
$pagenum=$_GET['pagenum'];
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$tbl_name="forum_question"; // Table name
include("bbcode.php");

// get value of id that sent from address bar
$id=$_GET['id'];
$qid=$_GET['id'];
$answer_id = $_GET['answer_id'];
$query="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($query);

$rows=mysql_fetch_array($result);

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

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

<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>
<?php
if($pagenum == 1)
{ ?>
<tr>
<td bgcolor="#111111" style="padding:5px 5px 5px 5px;" width="14%">

<a href="?action=viewprofile&pruname=<? echo $rows['name']; ?>"><? echo $rows['name']; ?></a><br />
<strong>Posted on:</strong><br><? echo $rows['datetime']; ?><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>
<?php } ?>
</table>

<?php
$tbl_name2="forum_answer"; // Switch to table "forum_answer"

//PAGINATION STUFF
//This checks to see if there is a page number. If not, it will set it to page 1

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM $tbl_name2 WHERE question_id='$id' ORDER BY a_id ASC") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page if it is first page because op is there display 1 less
$page_rows = 10;


//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//This is your query again, the same one... the only difference is we add $max into it
$data_p = mysql_query("SELECT * FROM $tbl_name2 WHERE question_id='$id' ORDER BY a_id ASC $max") or die(mysql_error());

//This is where you display your query results
while($info = mysql_fetch_array( $data_p ))
{ 
//PAGINATION STUFF END


$queryx="SELECT avatar FROM users WHERE username='$info[a_name]'";
$resultx=mysql_query($queryx);

$rowx=mysql_fetch_array($resultx);

$a= $info['a_answer'];
$a = preg_replace($patterns,$replacements, $a);
?>
<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 $info['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 $info['a_name']; ?>"><? echo $info['a_name']; ?></a><br />
<strong>Posted on:</strong><br><? echo $info['a_datetime']; ?><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'] == $info['a_name'] | $_SESSION['level'] == 3){?><tr align="right"><td></td><td><?php
if($_GET['confirm'] == 1 && $info['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\"><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=<?=$info['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=<?=$info['a_id']?>" title="Delete your post"><img border="0"src="<?=$root?>images/Delete.png"></a>
</td></tr><?php } } ?>
</table>

<?php

$q_title=$rows['a_title']; ?> 
<?
}

$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);
}
echo " --Page $pagenum of $last--";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == "1")
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?action=view&id=3&pagenum=1'>First</a> ";
echo " | ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?action=view&id=3&pagenum=$previous'>Previous</a> ";
}

if ($pagenum != "1" && $pagenum != $last)
{
//just a spacer
echo " | ";
}

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?action=view&id=3&pagenum=$next'>Next</a> ";
echo " | ";
echo " <a href='{$_SERVER['PHP_SELF']}?action=view&id=3&pagenum=$last'>Last</a> ";
} 
// count more value
$addview=$view+1;
$query5="update $tbl_name set view='$addview' WHERE id='$id'";
$result5=mysql_query($query5);

 

 

fixes/ improvments welcome :)

Link to comment
https://forums.phpfreaks.com/topic/97197-pagination-screwed/
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.