GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Nov-2005, 18:55
Zurke Zurke is offline
New Member
 
Join Date: Dec 2004
Posts: 12
Zurke is on a distinguished road

Pagnation


I have read and reread a number of pagnation scripts and tutorials. I finally found one that mostly worked. The only problem seems to be is, that when the link to say, page (2), (or further) I can presume, is clicked and the page reloads I get an error of not being able to make the query.

Its the same query I made on the first page and with page (2) as $_get it should retrieve the remainder.

I am stuck, here is my code, can anyone figure it out?

PHP Code:

####### Pagnation script #########
// Sets how many results shown per page.
    $limit = 5; 

// Sets what we want to pull from the database.                        
    $query_count = "SELECT * FROM threads WHERE thread_id = '$_POST[th_id]'";         
    $result_count = mysqli_query($connection,$query_count);
        if(!$result_count){
    echo"unable to get result_count @ this time";
      exit();
      }    
    $totalrows= mysqli_num_rows($result_count); 
    
// Checks if the $page variable is empty (not set). If it is empty, we're on page 1 
   if(empty($_GET['page'])){ 
    $page = 1; 
    } 


####### ### ### ### ## #########

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>threads</title>
</head>
<body background='/pslocal131/graphics/back8.jpg' bgcolor='' topmargin='50' text='#ffffff'>
<?php

echo"<table border='1' bordercolor='#083194' background='' bgcolor='#3152a5' cellpadding='0' cellspacing='0' width='900' height='' align='center'><tr><td>";

# This file loads the header to the document.
include_once("header.inc");
   
     # This section creates navigation and New topic button for new threads.
     echo"<table border='0' background='/pslocal131/graphics/gray.gif' bgcolor='' cellpadding='2' cellspacing='0' width='850' height='25' align='center'><tr><td width='785' height='25'>";
     nav_pslocal131();
     echo"</td><td width='65' height='25' align='right'>";
     $new='new_thread';
   if(@$_SESSION['logname'] != "admin" && $_POST[th_id] == 1){ $new = "<font size='2' color='#000000' face=''>Admin only</font>";}
   else {$new('test');}
     echo"</td></tr></table>"; 
     
     
     # This table gives a description of the threads being displayed.
   echo"<table border='1' background='/pslocal131/graphics/gray.gif' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='30' align='center'><tr><td>"; 
   echo"<td background='' bgcolor='' width='400' height='30' align='center'><font size='3' color='#29006b' face''><b>subject</b></font></td>";
   echo"<td background='' bgcolor='' width='150' height='30' align='center'><font size='3' color='#29006b' face''><b>author</b></font></td>";
   echo"<td background='' bgcolor='' width='100' height='30' align='center'><font size='3' color='#29006b' face''>last post</font></td>";
   echo"<td background='' bgcolor='' width='100' height='30' align='center'><font size='3' color='#29006b' face''>replies</font></td>";
   echo"<td background='' bgcolor='' width='100' height='30' align='center'><font size='3' color='#29006b' face''>views</font></td>";
   echo"</td></tr></table> $nothing"; 
     
     
    # This section retrieves results for display and also for pagnation.
     // Ex: (1 * 5) - 5 = 0 <- data starts at 0 
   $limitvalue = $page * $limit - ($limit); 
     // Selects all the data from table. 
     // LIMIT $row_to_start_at, $how_many_rows_to_return
   $query  = "SELECT * FROM threads WHERE thread_id='$_POST[th_id]' ORDER BY id DESC LIMIT $limitvalue,$limit";         
   $result = mysqli_query($connection,$query);                                                
     if(!$result){
   echo"unable to get result limitvalue, limit @ this time";
     exit();
      } 

   if(mysqli_num_rows($result) == 0){ 
        $nothing = "<center><font size='3' color='' face=''>There are no threads in this forum.</font></center>"; 
    }   
     
     
   while ($row = mysqli_fetch_array($result)){
   
     $tp_id=$row[id];
   $author=$row[author];
   $th_subject=$row[subject];
   $body=$row[body];
     $views=$row[views];
      
     # This section is the product of the above query.
     echo"<table border='1' background='' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='80' align='center'><tr>";
   echo"<td background='' bgcolor='' width='400' align='left'>";view_post();
   echo"</td><td background='' bgcolor='' width='150' height='80' align='center'><font size='3' color='ffffff' face''><b>$author &nbsp;</b></font></td>";
   echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$date<br>$time &nbsp;</font></td>";
   echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$total &nbsp;</font></td>";
   echo"<td background='' bgcolor='' width='100' height='80' align='center'><font size='3' color='ffffff' face''>$views &nbsp;</font></td>";
   echo"</tr></table>"; 
 }
 
### Pagnation ########

 // Fancy way of subtracting 1 from $page 
 if($page != 1){#1 
   $pageprev = $page--; 
 // If we are not on the first page this outputs PREV in link form.
     $prev=("<a href='/pslocal131/threads.phppage=$pageprev'>$limit&nbsp;PREV</a>"); 
    }#1
        else{#2 
 // If we're on page 1, PREV is not a link 
   $prev=("$limit&nbsp;PREV"); 
    }#2 
        
 // We divide our total amount of rows (for example 12) by the limit (5). 
  $numofpages = $totalrows / $limit; 
 // This will yield 2.4, which can be rounded down to 2. The next few lines create 2 pages, 
 // and then check to see if we have extra rows remaining for a 3rd page. 
 
 
 // This for loop will add 1 to $i at the end of each pass until $i is greater than $numofpages (2.4).    
  for($i = 1; $i <= $numofpages; $i++){#1 
    
 //    This if statement will not make the current page number available in link form. It will, however, make all other pages available in link form. 
  if($i == $page){#2 
   $doc=("&nbsp;($i)&nbsp;"); 
   }#2
      else{#3 
     $doc=("<a href='/pslocal131/threads.php?page=$i'>&nbsp;($i)&nbsp;</a>"); 
   }#3 
  }#1 
 
 // The following decides if there are any remaning pages to create. It returns the remainder after dividing two numbers. 
 // If there is no remainder, it returns zero.
 if(($totalrows % $limit) != 0){#1 
 
 // This is the  statement that turns pages into link form that is used above. 
   if($i == $page){#2 
     $doc=("&nbsp;($i)&nbsp;"); 
    }#2
        else{#3 
     $doc=("<a href='/pslocal131/threads.php?page=$i'>&nbsp;($i)&nbsp;</a>"); 
      }#3 
    }#1 
 
 // This statement checks to see if there are more rows remaining, meaning there are pages in front of the current one. 
    if(($totalrows - ($limit * $page)) > 0){#1 
 // Fancy way of adding 1 to page 
     $pagenext = $page + 1; 
 // If there are pages remaining, this outputs NEXT in link form.          
       $next=("<a href='/pslocal131/threads.php?page=$pagenext'>NEXT&nbsp;$limit</a>"); 
    }#1
        else{#2 
 // If we're on the last page possible, NEXT will NOT be displayed in link form. 
        $next=("NEXT&nbsp;$limit"); 
      }#2 

 
 
 ### ### ### ### ### ##
 
 
 mysqli_close($connection);

include_once("footer.inc");
echo"<table border='0' background='' bgcolor='' cellpadding='0' cellspacing='0' width='850' height='30' align='center'>";
echo"<tr><td align='left'>total threads: $totalrows</td><td align='right'>$prev $doc $next </td></tr></table>";

echo"<br><br>"; 

  echo"</td></tr></table>"; 


Last edited by LuciWiz : 19-Nov-2005 at 07:00. Reason: Please insert your Php code between [php] & [/php] tags
  #2  
Old 18-Nov-2005, 20:48
Zurke Zurke is offline
New Member
 
Join Date: Dec 2004
Posts: 12
Zurke is on a distinguished road

Re: Pagnation


Also when it advances to the second page, the previous link is set up for 1 and when the previous link is clicked the page loads but there is nothing displayed. .....page=1 is shown in the address bar as it should be.
  #3  
Old 22-Nov-2005, 09:01
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Pagnation


Quote:
Originally Posted by Zurke
I have read and reread a number of pagnation scripts and tutorials. I finally found one that mostly worked.

and what is wrong with the example in this thread? What was complicated about it?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.