Jump to content

Pagination problem


kvnirvana

Recommended Posts

Can't seem to get the pagination to work. The problem is that if there are more than 2 comments a link to the next results appears. But if I click on the 'next' link it says ”no comments”. I know I have to pass some values, but don't know how to do it and which values to pass?

 

This is the full code

 

<?php



//connect to your database 
$dbhost = '';
$dbuser = '';
$dbpass = '';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');

$dbname = '';
mysql_select_db($dbname);
echo "<tr> 
<td>
</td></tr><br>
";









$records_per_page = 2;
//query comments for this page of this article 
$inf = "SELECT * FROM `comments` WHERE page = '".stripslashes($_SERVER['REQUEST_URI'])."' ORDER BY time desc"; 
//-run  the query against the mysql query function
$info = mysql_query($inf); 
     if(!$info) die(mysql_error()); 
//-count  results
   $info_rows = mysql_num_rows($info); 


$num_pages =  ceil($info_rows/$records_per_page);
//Determine the page to display
$current_page = (isset($_GET['pg']))?$_GET['pg']:1;
if($current_page<1 || $current_page>$num_pages) { $current_page = 1; }
//Define the limit start position for the current page of records (for current filter)
$limitstart = (($current_page-1)*$records_per_page);

$inf1 = "SELECT * FROM `comments` WHERE page = '".stripslashes($_SERVER['REQUEST_URI'])."' ORDER BY time desc LIMIT $limitstart, $records_per_page"; 
$result1=mysql_query($inf1);

if($info_rows > 0) { 
   echo '<p  align="center"><strong>Comments '.$_GET['behandler'].' '.$_GET['navn'].'</strong></p><br><br>'; 


   
while($info2 = mysql_fetch_object($result1)) {     
echo '<tr>';    
echo '<td><p>Subject:'.stripslashes($info2->subject).' written by: '.stripslashes($info2->username).'</td> <td><div align="right"> '.date('h:i:s a', $info2->time).' on '.$info2->date.'</p></div></td>'; 
echo '</tr><tr>'; 
echo '<td colspan="2"> '.stripslashes($info2->comment).'<hr align="left" width="70%" noshade><a href="anmeldkomment.php?user_id='.stripslashes($info2->user_id).'"></a>  </td>'; 

echo '</tr>'; 


}//end while 


} else echo '<p>No comments '.$_GET['behandler'].' '.$_GET['navn'].'</p><br>'; 

if(isset($_POST['submit'])) { 
  if(!addslashes($_POST['username'])) die('<u>ERROR:<p></u> no name given.</p>'); 
  if(!addslashes($_POST['subject']))  die('<u>ERROR:<p></u> no subject given.</p>'); 
  if(!addslashes($_POST['comment']))  die('<u>ERROR:<p></u> no comments given!</p>'); 



//try to prevent multiple posts and flooding... 
$c = "SELECT * from `comments` WHERE ip = '".$_SERVER['REMOTE_ADDR']."'"; 
  $c2 = mysql_query($c); 
     while($c3 = mysql_fetch_object($c2)) { 
      $difference = time() - $c3->time; 
    
      } //end while 
$adresse   = mysql_real_escape_string($_GET['adresse']);      
$navn   = mysql_real_escape_string($_GET['navn']);
$userID   = mysql_real_escape_string($_POST['user_id']);
$page     = mysql_real_escape_string($_POST['page']);
$date     = mysql_real_escape_string($_POST['date']);
$time     = mysql_real_escape_string($_POST['time']);
$username = mysql_real_escape_string($_POST['username']);
$remtAddr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
$subject  = mysql_real_escape_string($_POST['subject']);
$comment  = mysql_real_escape_string($_POST['comment']);//add comment 
$q = "INSERT INTO `comments`      (adresse, navn, user_id, page, date, time, username, ip, subject, comment)      VALUES ('{$adresse}','{$navn}','{$userID}','{$page}', '{$date}', '{$_POST['time']}', '{$username}', '{$remtAddr}', '{$subject}', '{$comment}')";

$q2 = mysql_query($q); 
  if(!$q2) die(mysql_error()); 

//refresh page so they can see new comment 
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_POST['page'] . "#comments"); 

} 


?>

<tr><td>
<br>
<?php 
// end else 
if($num_pages>1)
{
    $thisroot = $_SERVER['PHP_SELF'];
    echo "<strong>Sider:</strong>    ";
    
    //Create link to navigate to last page
    if($current_page > 1)
    {
        echo "<a href='javascript:void()' onclick='window.location.href=\"{$thisroot}?pg=" . ($current_page-1) ."&behandler=". $_GET['behandler'] . "&navn=". $_GET['navn'] . "\"; parent.scrollTo(0,0); return false'><< Forrige</a>  \n";
    }
    
    //Create individual page links
    for($page=1; $page<=$num_pages; $page++)
    {
        echo ($page!= $current_page) ? "<a href='javascript:void()' onclick='window.location.href=\"{$thisroot}?pg={$page}&behandler=$_GET[behandler]&navn=$_GET[navn]\"; parent.scrollTo(0,0); return false'>$page</a>" : $page;
        echo "  ";
    }
    
    //Create link to next page
    if($current_page < $num_pages)
    {
        echo "<a href='javascript:void()' onclick='window.location.href=\"{$thisroot}?pg=" . ($current_page+1) ."&behandler=". $_GET['behandler'] . "&navn=". $_GET['navn'] . "\"; parent.scrollTo(0,0); return false'> Næste >></a>";
    }
}
/////////////////

?> 

 

appriciate any help :=)

Link to comment
Share on other sites

Watched the clip, but that didn't help me with the problem. I really tried to solve this problem, but can't understand why I can't get it to work. Wish I could be more specific, but that's difficult when I don't know what's causing the problem :-(

The problem is this.  If there are more than 2 results ther will be a link to see the next results. If I click to see the next results, I get the message "No comments", even though I know there are more comments.

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.