Jump to content


Photo

Question with Pagination


  • Please log in to reply
7 replies to this topic

#1 gfX

gfX
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 05 August 2006 - 01:20 AM

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:
<? 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'; ?>

Thanks!

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 05 August 2006 - 01:46 AM

try this mate ok
<? 
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>";

} 


}

?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 gfX

gfX
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 05 August 2006 - 01:57 AM

Didn't work man, There were a few errors which I fixed up but that code isn't working :(

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 05 August 2006 - 01:59 AM

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



<?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>";
}

?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 gfX

gfX
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 05 August 2006 - 03:55 AM

I don't get what that has to do with it.

#6 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 05 August 2006 - 05:13 AM

Let's say, for example, that you have 16 results, at 3 results per page.

If you use this code:

$totalpages = $totalpages / $limit;
$totalpages = round($totalpages,0);

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

Change this line in your code:

$totalpages = $totalpages / $limit;

to this:

$totalpages = ceil($totalpages / $limit);

and remove this line:

$totalpages = round($totalpages,0);

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

#7 gfX

gfX
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts

Posted 05 August 2006 - 12:24 PM

Thank you very much!

This is random but how do you remove a file from FTP using PHP?

#8 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,429 posts
  • LocationBelgium

Posted 05 August 2006 - 12:41 PM

ftp what do you mean? when it is already uploaded or still has to be uploaded? When it is already uploaded you will need unlink(); to delete the file.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users