Jump to content

Question with Pagination


gfX

Recommended Posts

Okay, So I have a photogallery with pagination.  And I set the limit to 3 photos per page.  And it has pagination for different pages. So, Say I have 7 Images in the database.  I SHOULD have 3 pages, and the third page should only have one image.  Well, My problem is:  If I had 7 images, It would only make 2 pages, and not make that third page, until I reached the limit (3 images).

Here is the code:
[code]
<? include 'header.php';
  include 'db.php';
?>


<div class="greentext">Select the photo you would like to view:<br /><br />

<?             
 
  $num = $_GET['num'];
if(empty($num)){
$num = 1;
};
$limit = 3;

$start = ($num-1)*$limit;
$start = round($start,0);

$sql = "SELECT * FROM `gallery` ORDER by `id` LIMIT $start, $limit";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "
<a href='viewgallery.php?id=$row[id]'><img src='galleryimages/$row[picture]' width='222' height='167' style='border: 2px solid #d4cccc;'>&nbsp;&nbsp;&nbsp;&nbsp;</a>";

}

?>

<br /><br />

<?
$totalpages = mysql_num_rows(mysql_query("SELECT * from `gallery`"));
$totalpages = $totalpages / $limit;
$totalpages = round($totalpages,0);
$c = 0;

while($c<$totalpages){
$page = $c + 1;
if($_GET['num']==$page){
echo "&nbsp;&nbsp;[$page]";
}else{
echo "&nbsp;&nbsp;<a href=?num=$page>[$page]</a>";
}
$c = $c+1;
}
echo "<br>".$totalpages." Pages in total.";

   
?>


</div>


<? include 'footer.php'; ?>
[/code]

Thanks!
Link to comment
https://forums.phpfreaks.com/topic/16606-question-with-pagination/
Share on other sites

try this mate ok
[code]
<?
include 'header.php';
include 'db.php';
?>

<div class="greentext">Select the photo you would like to view:<br /><br />

<?php
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$max_results = 3;

$from = (($page * $max_results) - $max_results);

$query="select * FROM `gallery` ORDER by `id` LIMIT $from, $max_results";


$result=mysql_query($query);
while($row=mysql_fetch_assoc($result)){



<a href='viewgallery.php?id=$row[id]'><img src='galleryimages/$row[picture]' width='222' height='167' style='border: 2px solid #d4cccc;'>&nbsp;&nbsp;&nbsp;&nbsp;</a>";




$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM FROM gallery "),0);


$total_pages = ceil($total_results / $max_results);


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"></a>";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"></a>";

}


}

?>

[/code]
hundred of users use it daily look here the full code i use ok.


[code]

<?php

$db=mysql_connect("localhost" , "xxxx" , "xxxx");
mysql_select_db("promotor",$db);

?>
<html>
<head>
<title>Send Message</title>
<body bgcolor="#A0C0F0">
<br><br><br><br><br>

<?php
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$max_results = 1;

$from = (($page * $max_results) - $max_results);

$query="select * from messages_copied  LIMIT $from, $max_results";


$result=mysql_query($query);
while($record=mysql_fetch_assoc($result)){


echo"<table align='center' width='300'border='4' bordercolor='black'><td align='left'><b>Members Id:<font color='red'><br>".$record["sent_id"]."</font><br> User Name: <font color='red'><br>".$record["members_name"]."</font><br>
Sent Time: <font color='red'><br>".$record["time"]."</font> <br> Sent Date <font color='red'><br>".$record["date"]."</font> </b></td><td align='center' valign='top'><b>Members Message <font color='red'>$page</font><b>
<br><br><b><div align='left'><textarea col='7' rows='7'style='color: white; background-color: #A0C0F0'>".$record["message"]."</textarea></td></b></div><table>";

echo "<table align='center' width='300'border='4' bordercolor='black'><td align='center'><b>";


$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM messages_copied "),0);


$total_pages = ceil($total_results / $max_results);


echo "<center><b>Select A Message!</b><br>";


if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"></a>";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}


if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\"></a>";

}


echo "</center>";
echo'</td></table>';

echo "<table align='center' width='300'border='4' bordercolor='black'><td align='center'><b>Send A Reply    <font color='red'><a href='reply_message.php?&id=".$record['sent_id']."'>Reply</a></b></font>
<font color='red'><a href='delete_message.php?&cmd=delete&time=".$record['time']."&id=".$record['sent_id']."'>Delete</a></b></font>
</td></table><br>";
}

?>
[/code]
Let's say, for example, that you have 16 results, at 3 results per page.

If you use this code:

[code]
$totalpages = $totalpages / $limit;
$totalpages = round($totalpages,0);
[/code]

You will get a result of [b]5.33[/b] pages, which will get rounded down to [b]5[/b] with [b]round()[/b].

Change this line in your code:

[code]$totalpages = $totalpages / $limit;[/code]

to this:

[code]$totalpages = ceil($totalpages / $limit);[/code]

and remove this line:

[code]$totalpages = round($totalpages,0);[/code]

That will give you [b]6[/b] pages as your result in my example, which should fix your issue.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.