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 ?
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
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))
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.
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.
PHP Code:
<?
//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');
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:
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());
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.....