Page 1 of 1

making multiple pages

Posted: Mon Jan 31, 2005 4:17 pm
by shiznatix
what i want to do is this:

i set the max items per page to 10
a query outputs 50 items
i want it to automatically create a list at the bottom that says page 1 page 2 page 3 until the number of pages that are needed are shown.

(this is were i have the biggest problem)-
on page one it should show items 1-10, page 2 should show items 11-20, and so on.

and sugestions on how i could do this please post. thanks

Posted: Mon Jan 31, 2005 4:40 pm
by Wldrumstcs
I did something just like that a little while ago. Here's my code:

Code: Select all

<?
$row_count = 0; 

mysql_connect("localhost","$username","$password") or die ("Unable to connect to MySQL server."); 
$db = mysql_select_db("$database") or die ("Unable to select requested database.");

$limit = 10;     
$page = $_GET&#1111;'page'];

$query_count = "SELECT * FROM tablename"; 
$result_count = mysql_query($query_count); 

$totalrows = mysql_num_rows($result_count); 

    if(empty($page))&#123; 
        $page = 1; 
    &#125; 
         
    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM tablename LIMIT $limitvalue, $limit";         
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

while($row = mysql_fetch_array($result))&#123; 
$var1=$row&#1111;"var1"];
$var2=$row&#1111;"var2"];
$var3=$row&#1111;"var3"];
$var4=$row&#1111;"var4"];

	echo "
		<tr>
				<td align='center' width='25%'>
				$var1</td>
				<td align='center' width='25%'>
				$var2</td>
				<td align='center' width='25%'>
				$var3</td>
				<td align='center' width='25%'>
				$var4</td>
</tr>
					
			";
			    $row_count++; 

&#125;



?>			
</table>
<br><br><p align="center">
<?
if($page != 1)&#123; 
        $pageprev = $page-1; 
         
        echo("<a href="PHP_SELF?page=$pageprev">&#1111;PREV&nbsp;".$limit."]</a>&nbsp;&nbsp;&nbsp;"); 
    &#125;else&#123; 
        echo("&#1111;PREV&nbsp;".$limit); 
        echo("]&nbsp&nbsp");
    &#125; 

    $numofpages = $totalrows / $limit; 
     
    for($i = 1; $i <= $numofpages; $i++)&#123; 
        if($i == $page)&#123; 
            echo($i."&nbsp;"); 
        &#125;else&#123; 
            echo("<a href="PHP_SELF.php?page=$i">$i</a>&nbsp;"); 
        &#125; 
    &#125; 


    if(($totalrows % $limit) != 0)&#123; 
        if($i == $page)&#123; 
            echo($i."&nbsp;"); 
        &#125;else&#123; 
            echo("<a href="PHP_SELF?page=$i">$i</a>&nbsp;"); 
        &#125; 
    &#125; 

    if(($totalrows - ($limit * $page)) > 0)&#123; 
 
$pagenext   = $page+1; 
          
       echo("&nbsp;&nbsp;<a href="$PHP_SELF?page=$pagenext">&#1111;NEXT&nbsp;".$limit."]</a>"); 
&#125;else&#123;
        echo("&nbsp&nbsp&#1111;NEXT&nbsp;".$limit); 
echo("]");
    &#125; 
     
    mysql_free_result($result); 
?></p>

Posted: Mon Jan 31, 2005 5:55 pm
by feyd
search for topics involving "pagination"

Posted: Mon Jan 31, 2005 6:50 pm
by shiznatix
the LIMIT $limitvalue, $limit was all i needed, someone explain what that does

Posted: Mon Jan 31, 2005 6:55 pm
by feyd
read the MySQL documentation.