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ї'page'];
$query_count = "SELECT * FROM tablename";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);
if(empty($page)){
$page = 1;
}
$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)){
$var1=$rowї"var1"];
$var2=$rowї"var2"];
$var3=$rowї"var3"];
$var4=$rowї"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++;
}
?>
</table>
<br><br><p align="center">
<?
if($page != 1){
$pageprev = $page-1;
echo("<a href="PHP_SELF?page=$pageprev">їPREV ".$limit."]</a> ");
}else{
echo("їPREV ".$limit);
echo("]  ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href="PHP_SELF.php?page=$i">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href="PHP_SELF?page=$i">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page+1;
echo(" <a href="$PHP_SELF?page=$pagenext">їNEXT ".$limit."]</a>");
}else{
echo("  їNEXT ".$limit);
echo("]");
}
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.