Next and Previous Statements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Next and Previous Statements

Post by kkurkowski »

Hello all again,
In that news script I was making I can't figure out how to use the next statements like say I have 5 per page and then the next 5 go on the next page but then it puts a link on the bottom of each page with a link eaither saying next 5 and previous 5. Is there any code that you guys know of that can do that?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

search this forum for next and previous links and you will find loads of posts on it.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

The stuff that I am talking about do you guys know what it is called? I searched for it on the site you sent me and nothing showed up in it.
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

Post by stickman373 »

Try this:

First you should do this somewhere near the top of the script so if no page number is passed to the script it will default to page 1. You should also specify how many rows to show per page

Code: Select all

if ($page == '' || !is_numeric($page)) {
    $page = 1;
}

$per_page = 10; // shows 10 rows per page
You have to query the db to find out how many records there are in total to begin with, something like

Code: Select all

$sql_max = "SELECT * FROM table WHERE something=something else";
$max_result = mysql_query($sql_max); // execute query
            $max_rows = mysql_numrows($max_result); // get no. of rows
            $pages = ceil($max_rows / $per_page); // divide by number per page to get pages required
We now know how many "pages" are needed to show all the rows.

You should then calculate the offset for the sql query that will actually get the rows we want. The offset is the row to start at which is done like so

Code: Select all

$offset = ($page - 1) * $per_page; // get offset for SQL query
Now we get the rows required

Code: Select all

$sql_products = "SELECT * FROM table WHERE something=somethingelse LIMIT ${offset}, ${per_page}";
All you have to do now is loop through those records returned printing the contents of each as normal.

You'll need to put hyperlinks in so users can select other pages, which can be done like this

Code: Select all

if ($pages > 1) {
    // more than 1 page required
    for ($count_page = 1; $count_page <= $pages; $count_page++) { // loop through pages required printing links
        print "<a href="$PHP_SELF?page=$count_page">Page $count_page</a><br>";
    }
}
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

I have never seen where it says something=something else. I don't know if that is why it is erroring on me, but it errors out on me.
Warning: Supplied argument is not a valid MySQL result resource in view_news.php on line 13
Here is line 13

$max_rows = mysql_numrows($max_result); // get no. of rows
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

Post by stickman373 »

take WHERE something=somethingelse out of both sql statements or change it so it applies to your database :)
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Ok, I got most of that working. I made it so when 5 are added then the next 5 are added to the next page. It is kind of working and then another way kind of not. When I add more than 5 it doesn't want to put just 5 on the page. But the Page 1 Page 2 is adding on the bottom of the page it just doesn't want to show 5 per page.

Code: Select all

<?php
global $page;
require ("news_connect.php");

if ($page == '' || !is_numeric($page)) { 
    $page = 1; 
} 

$per_page = 5; // shows 5 rows per page

$sql_max = "SELECT * FROM news_news"; 
$max_result = mysql_query($sql_max); // execute query 
            $max_rows = mysql_numrows($max_result); // get no. of rows 
            $pages = ceil($max_rows / $per_page); // divide by number per page to get pages required
$offset = ($page - 1) * $per_page; // get offset for SQL query
$sql_products = "SELECT * FROM news_news ORDER BY timestamp LIMIT ${offset}, ${per_page}";

?>

<?
global $data;
require ("news_connect.php");

$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news");

while ($data = mysql_fetch_array ($result))
{
?>
Then this is at the bottom of the page.

Code: Select all

<?php
}
?>
<?
if ($pages > 1) { 
    // more than 1 page required 
    for ($count_page = 1; $count_page <= $pages; $count_page++) { // loop through pages required printing links 
        print "<a href="$PHP_SELF?page=$count_page">Page $count_page</a><br>"; 
    } 
} 
?>
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

Post by stickman373 »

try this as ur second sql statement instead

Code: Select all

$sql_products = "SELECT * FROM news_news ORDER BY timestamp LIMIT $offset, $per_page";
and u cant use this statement then:

Code: Select all

$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news");
if u want use this:

Code: Select all

$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news  LIMIT $offset, $per_page");
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

Post by stickman373 »

try this as ur second sql statement instead

Code: Select all

$sql_products = "SELECT * FROM news_news ORDER BY timestamp LIMIT $offset, $per_page";
and u cant use this statement then:

Code: Select all

$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news");
if u want use this:

Code: Select all

$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news  LIMIT $offset, $per_page");
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Kewl, thanks. That is working now. I am just trying to get rid of all my PHP errors on my site that I haven't had anytime to work on but now I am adding different stuff to each of the scripts. Few more errors on the site and I will be done.
stickman373
Forum Commoner
Posts: 30
Joined: Mon Jul 22, 2002 10:26 am

Post by stickman373 »

glad it works, good luck with your site :wink:
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

help: mulitple pages

Post by sirTemplar »

if ($MORTDAT1 == "")
{$MORTDAT1 = '%';}

// Change this to fit your database

$result = mysql_query ("SELECT * FROM friarsofmconv
WHERE CNOMEN LIKE '%$CNOMEN%'
AND NOMRL LIKE '%$NOMRL%'
AND PROVRELCD LIKE '%$PROVRELCD%'
AND NATDAT1 LIKE '%$NATDAT1%'
AND PROFTDAT1 LIKE '%$PROFTDAT1%'
AND PROFSDAT1 LIKE '%$PROFSDAT1%'
AND SACDAT1 LIKE '%$SACDAT1%'
AND MORTDAT1 LIKE '%$MORTDAT1%'
AND SDATFIN1 = ' '
ORDER BY CNOMEN ASC, NOMRL
",$conn);

if ($row = mysql_fetch_array($result)) {

do {
echo "<table border=0 cellpadding=1 cellspacing=0 style=border-collapse: collapse bordercolor=#111111 width=100%>";
echo "<tr><td bgcolor=#E1FFFF align=left width=20% valign=top><b><font face=Verdana size=2 color=#0000FF>Fullname: </font></b></td>
<td bgcolor=#E1FFFF align=left width=80% valign=top><b><font face=Verdana size=2 color=#800000>{$row['CNOMEN']},</b> {$row['NOMRL']} ({$row['NOMBL']})
<b><font face=Verdana size=2 color=#0000FF></font></b>
<font face=Verdana size=2 color=#000000></td>
this is a part of the code i have. where do i put exactly the above codes to have it work? i am new to php but i can follow instruc :) . any help?

by the way here is the end of the code:
}
echo ("<p>");
echo ("<p>");
} while($row = mysql_fetch_array($result));
} else {print "Sorry, no records were found!";}
?>
<hr color="#445BA1" size="1">
</td>
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Post by sirTemplar »

sorry. i may be unclear. what i mean when i said "above code to work" is the code by stickman373, not mine :wink:
Post Reply