...

View Full Version : A Next Page Function ?



quinnrd
02-06-2003, 04:18 PM
I am using PHP to read data from a mysql database and format it . I am
displaying a picture along with a descrption. No problem here, but what
I would like to do is stop after say 5
entries, then display a link for "next page" . Right now I display every entry that is in the database and the page is too long(height).

Example:
10 entries in DB.
display 5 at a time,stop
next page,
display next 5,etc

sounds easy but I dont know how to do it. Anybody have a easy way to do this ?

Thanks in aadvance

wap3
02-06-2003, 09:55 PM
Hi there,

Basically you use the limit value in your msql query.
If you do a search there are loads of posts concerning this.

:thumbsup:

p.s oh most likely to find something in the msql forum

quinnrd
02-06-2003, 10:18 PM
Thanks, I try there..............

mordred
02-07-2003, 12:50 AM
FYI: If you plan to include "next" and "previous" links, and probably also some information which part of the result set is currently displayed, then have a look at the PEAR::Pager class.
It's insanely practical and yesterday it saved me three hours of coding.
http://pear.php.net/package-info.php?pacid=32

Nightfire
02-08-2003, 05:57 PM
Thanks for the link mordred :) I was just looking for something like that

quinnrd
02-10-2003, 04:49 PM
OK, I looked at PAGER and it looks like what I want to do. Problem though, I'm
a bit confused on how to implement this,
not sure how to pass it my data. Perhaps someone could get me started. Thanks in advance....
mycode :

<?php

$linkID=mysql_connect ("localhost", "", );

mysql_select_db ("southern_newreleases",$linkID);

$resultID = mysql_query("SELECT iloc,dloc,movie_name,director,actor,date,rated,num_stars FROM movies",$linkID);

while (list($iloc,$dloc,$movie_name,$director,$actor,$date,$rated,$num_stars) = mysql_fetch_row($resultID))

{
print "<tr><td valign=middle align=left colspan=7>";
print "<img align=left valign=middle src=$num_stars>";

print "<strong>Directed by $director</strong>";
print "<strong><font color=#ffff00>$movie_name&nbsp($date)&nbsp&nbsp&nbsp</strong></font>";
print "<strong>Starring $actor </strong>";

print "<img align=right valign=top src=$rated>";
print "<hr align=right size=1 color=#ffff00 width=100%></td></tr>";

print "<tr bgcolor=#c4b79d>";
print "<td>";
print "<img align=center valign=middle src=$iloc width=75 height=100></td>";
print "<td valign=top align=center>";
print "<strong>";
readfile("$dloc");
print "</strong>";
print "</td></tr>";

print "<tr><td height=1 colspan=4><img align=left valign=bottom src=rent.gif width=38 height= 18>";

print "<hr align=right size=1 color=#ffff00 width=100%>";

print "</td></tr>";
}

mysql_close($linkID);
?>

</table>
</body>
</htm

quinnrd
02-12-2003, 05:47 PM
Can Anyone get me started on getting
"PAGER" or any other means of only
displaying a certain amount of rows
per page...... THanks in advance

ez4me2c3d
02-12-2003, 08:24 PM
i faced this problem when i wrote my own guest book. i just use a flat file to store a number indicating the amount of entries i have.

Let's say you have 10 pictures then you just asign a variable to the amount of pictures your have (10).
$pic_count = 10now you want to use a query string in the address bar to make it seem like you have more than one page. kinda like this:

<a href="thispage.php?page=1">page 1|Next Page</a>

Then based on the page number and the $pic_count variable you can deduce how many pages there are and which images to display with the LIMIT attribute in your mySQL query.

I wont write out all the code right here, but i hope you get where i was going with this.

to test my guest book : http://www.ez4me2c3d.com/guest_book.php?s=1&mode=sign

Feyd
02-12-2003, 08:27 PM
Note that I'm using echoes, when you really shouldn't, as it can slow down your pages, it is best to exit out of the PHP block and simply echo PHP variables in the HTML (but that would be harder for you to read and know what you were looking at, if I were to post it); but the even better option would be to use a template for this and separate all your code from your content...but that is far beyond the scope of this question.


<?
//require/include db connection data from protected dir
$db = mysql_select_db($dbName,$dbConnect) or die('Unable to SELECT Database : '.mysql_error());
$per_page = 10; //amount to display on each page

$sql_text = ('SELECT * FROM name ORDER BY date DESC');

$result_count = @mysql_query($sql_text);
$totalrows = @mysql_num_rows($result_count); //get counts prior to pagination

if (empty($page)) @$page = 1;
$limitvalue = @$page * $per_page - ($per_page); // Ex: (2 * 25) - 25 = 25 <- data starts at 25

$sql_text = $sql_text . ' LIMIT ' . $limitvalue.', ' . $per_page; //now break into paged sections

$query = mysql_query($sql_text) or die('Error: ' . mysql_error());

//content code display here

//actual pagination build of links
//previous
if ($page != 1) {
$pageprev = $page - 1;
echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pageprev.'">&lt;&lt;</a>';
} else //no previous or add unavailable option, or remove else completely

//active pages
$numofpages = $totalrows / $per_page;
for($i = 1; $i <= $numofpages; $i++) {
if ($i == $page) echo '&nbsp;'.$i; //inactive page number
else echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a>'; //active page, linked
}
//check for remaining rows, if no page available after prev block
if (($totalrows % $per_page) != 0) {
if ($i == $page) echo '&nbsp;'.$i; //inactive page number
else echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a>'; //active page, linked
}

//next
if( ($totalrows - ($per_page * $page)) > 0) {
$pagenext = $page + 1;
echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$pagenext.'">&gt;&gt;</a>';
} else //no next or add unavailable option, or remove else completely

mysql_free_result($query); //paranoid cleaner
?>

quinnrd
02-13-2003, 01:50 AM
ez4m22c3d and feyd, Thanks for the reply and help. I took your code feyd and played around with it a little and believe I can get it to work for my application. At
least it is doing something for me.

I had to comment out 2 lines of code because I was getting parse/syntax errors. With the time I had I did not spot the problem? On the lines:

//else echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a>');

But anyway thanks again for the help and I am sure I will have a few more questions for you guys......

Feyd
02-13-2003, 01:58 AM
Ooops...my fault, I was hacking it together while I was replying...

(there was an exta ')' or two when there shouldn't have been)

The original code I posted has been updated.

quinnrd
02-13-2003, 02:31 AM
Feryd, once again thanks, I see it now.....

quinnrd
02-28-2003, 07:58 PM
Feyd, I have been using your code for the next page function for sometime now, worked great, UNTIL now....
Perhaps you can help me.

From my main page if you were to detect on this link, the variable type is passed to mypager.php.
<a href="http://host37.ipowerweb.com/~southern/mypager.php?type=comedy"><img align="center"
src="comedy.gif" alt="Comedy Flicks" border="0"></a>

Now mypager.php will display the proper number of things on the first page(limit set to 4, 6 items in the database, should have 2 pages of stuff to display and I do).

The problem is when I ask it for the next page(should be 2 more) I get nothing displayed.

I can get it to work by hardcoding the variable to ($type=comedy)
in mypager.php. Is the variable getting lost on the second page?
My simple mind cant figure this out.
Any ideas........ Thanks in advance

code:

mysql_select_db ("southern_newreleases",$linkID);


//$db = mysql_select_db($dbName,$dbConnect) or die('Unable to SELECT Database : '.mysql_error());

$per_page = 4; //amount to display on each page

//$lookup_type=$_GET['type'];
//$_PUT['lookup_type'];
//$ID=$_GET['ID'];

//print "$type";
//$type = comedy;
//switch ($lookup_type)
// {
// case "comedy":
// $type = "$lookup_type";
// print "$type";
// break;
// }

$sql_text = ("SELECT iloc,dloc,movie_name,director,actor,date,rated,num_stars FROM movies
WHERE type='$type'");


$result_count = @mysql_query($sql_text);

$totalrows = @mysql_num_rows($result_count); //get counts prior to pagination

if (empty($page)) @$page = 1;

$limitvalue = @$page * $per_page - ($per_page); // Ex: (2 * 25) - 25 = 25 <- data starts at 25

$sql_text = $sql_text . ' LIMIT ' . $limitvalue.', ' . $per_page; //break into paged sections

$query = mysql_query($sql_text) or die('Error: ' . mysql_error());

//content code display here

// print "limit valve = $limitvalue";
// print "per_page = $per_page";
// print "totalrows = $totalrows";

while (list($iloc,$dloc,$movie_name,$director,$actor,$date,$rated,$num_stars) = mysql_fetch_row($query))
{
print "<tr><td valign=middle align=left colspan=7>";

print "<img align=left valign=middle src=$num_stars>";

print "<strong>&nbsp&nbsp&nbsp&nbsp&nbspDirected by
$director</strong>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp";
print "<strong><font color=#ffff00>$movie_name&nbsp($date)&nbsp&nbsp&nbsp</strong></font>";
print "<strong>&nbsp&nbsp&nbsp&nbspStarring $actor </strong>";
print "<img align=right valign=top src=$rated>";
print "<hr align=right size=1 color=#ffff00 width=100%></td></tr>";

print "<tr bgcolor=#c4b79d>";
print "<td height=110>";
print "<img align=center valign=middle src=$iloc width=93 height=130></td>";
print "<td valign=middle align=center>";
print "<strong>";
readfile("$dloc");
print "</strong>";
print "</td></tr>";

print "<tr><td height=1 colspan=4><img align=left valign=bottom src=rent.gif width=38 height=
18>";
print "<hr align=right size=1 color=#ffff00 width=100%>";
print "</td></tr>";
}


print "<table width=760 align=center border=0>";
print "<tr>";
print "<td align=center>";
print "<h3>";
print "<strong>";

//actual pagination build of links

//previous

if ($page != 1) {

$pageprev = $page - 1;

echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pageprev.'">&lt;&lt;Previous</a>';


} else //no previous or add unavailable option, or remove else completely



//active pages

$numofpages = $totalrows / $per_page;
// print "Active number of pages=$numofpages";

for($i = 1; $i <= $numofpages; $i++) {


if ($i == $page)
{

echo '&nbsp;'.$i;
//
}
else echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a>';

}

//check for remaining rows, if no page available after prev block

if (($totalrows % $per_page) != 0) {

if ($i == $page) echo '&nbsp;.$i';
else
echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'">'.$i.'</a>';

}


//next

if( ($totalrows - ($per_page * $page)) > 0) {

$pagenext = $page + 1;

echo '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$pagenext.'">&gt;&gt;Next</a>';

}

else //print "in the damm else";
//} else no next or add unavailable option, or remove else completely
mysql_free_result($query); //paranoid cleaner
print "</h3>";
print "</strong>";
print "</td>";
print "</tr>";
print "</table>";

mysql_close($linkID);
?>

Feyd
07-09-2003, 01:04 AM
Wow...can't believe I missed this reply for so long...sorry Quinn, hope this will help anyways:

Since I can't see your code, you probably are losing the type specification in your query string.

You simply need to echo it back into the navigation links for your pagination for the script to pick up the category selection...



$category = '';
if (isset($_GET['type'])) $category = '&type='.$_GET['type'];
$href = $_SERVER['SCRIPT_NAME'].'?page='.$i.$category;
echo ' <a href="'.$href.'">&gt;&gt;Next</a>';


You may need to tweak it a bit in your application of it, but that is basically it.

quinnrd
07-09-2003, 01:20 AM
I guess better late than never applies here. Hey thanks, Ive
been away from my coding for a while but I will sure give
this a try since I never did get it working. Thanks once
again for the help.....



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum