Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2003, 03:18 PM   PM User | #1
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
A Next Page Function ?

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
quinnrd is offline   Reply With Quote
Old 02-06-2003, 08:55 PM   PM User | #2
wap3
Regular Coder

 
Join Date: Aug 2002
Location: UK
Posts: 324
Thanks: 0
Thanked 0 Times in 0 Posts
wap3 is an unknown quantity at this point
Hi there,

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



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

Last edited by wap3; 02-06-2003 at 09:01 PM..
wap3 is offline   Reply With Quote
Old 02-06-2003, 09:18 PM   PM User | #3
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
Thanks, I try there..............
quinnrd is offline   Reply With Quote
Old 02-06-2003, 11:50 PM   PM User | #4
mordred
Senior Coder


 
Join Date: Jun 2002
Location: frankfurt, german banana republic
Posts: 1,848
Thanks: 0
Thanked 0 Times in 0 Posts
mordred is an unknown quantity at this point
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
mordred is offline   Reply With Quote
Old 02-08-2003, 04:57 PM   PM User | #5
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Thanks for the link mordred I was just looking for something like that
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 02-10-2003, 03:49 PM   PM User | #6
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
Pager

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 is offline   Reply With Quote
Old 02-12-2003, 04:47 PM   PM User | #7
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
PLease HELP !

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
quinnrd is offline   Reply With Quote
Old 02-12-2003, 07:24 PM   PM User | #8
ez4me2c3d
Regular Coder

 
Join Date: Dec 2002
Location: Minneapolis, MN
Posts: 208
Thanks: 0
Thanked 1 Time in 1 Post
ez4me2c3d is an unknown quantity at this point
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).
PHP Code:
$pic_count 10 
now 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
__________________
anthony
ez4me2c3d is offline   Reply With Quote
Old 02-12-2003, 07:27 PM   PM User | #9
Feyd
Regular Coder


 
Feyd's Avatar
 
Join Date: May 2002
Location: Los Angeles, CA Maxim: Subvert Society
Posts: 404
Thanks: 0
Thanked 0 Times in 0 Posts
Feyd is an unknown quantity at this point
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');

    
$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
?>
__________________
Moderator, Perl/CGI Forum
shadowstorm.net - subvert society

Last edited by Feyd; 02-13-2003 at 12:58 AM..
Feyd is offline   Reply With Quote
Old 02-13-2003, 12:50 AM   PM User | #10
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
THanks

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......
quinnrd is offline   Reply With Quote
Old 02-13-2003, 12:58 AM   PM User | #11
Feyd
Regular Coder


 
Feyd's Avatar
 
Join Date: May 2002
Location: Los Angeles, CA Maxim: Subvert Society
Posts: 404
Thanks: 0
Thanked 0 Times in 0 Posts
Feyd is an unknown quantity at this point
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.
__________________
Moderator, Perl/CGI Forum
shadowstorm.net - subvert society
Feyd is offline   Reply With Quote
Old 02-13-2003, 01:31 AM   PM User | #12
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
Feryd, once again thanks, I see it now.....
quinnrd is offline   Reply With Quote
Old 02-28-2003, 06:58 PM   PM User | #13
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
Variables not visible on next page ?

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);
?>
quinnrd is offline   Reply With Quote
Old 07-09-2003, 12:04 AM   PM User | #14
Feyd
Regular Coder


 
Feyd's Avatar
 
Join Date: May 2002
Location: Los Angeles, CA Maxim: Subvert Society
Posts: 404
Thanks: 0
Thanked 0 Times in 0 Posts
Feyd is an unknown quantity at this point
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...

PHP Code:
        $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.
__________________
Moderator, Perl/CGI Forum
shadowstorm.net - subvert society
Feyd is offline   Reply With Quote
Old 07-09-2003, 12:20 AM   PM User | #15
quinnrd
New Coder

 
Join Date: Jan 2003
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
quinnrd is an unknown quantity at this point
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.....
quinnrd is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:14 AM.


Advertisement
Log in to turn off these ads.