Next and Previous Statements
Moderator: General Moderators
-
kkurkowski
- Forum Commoner
- Posts: 53
- Joined: Mon Dec 09, 2002 4:44 pm
- Location: Michigan
Next and Previous Statements
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?
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?
-
kkurkowski
- Forum Commoner
- Posts: 53
- Joined: Mon Dec 09, 2002 4:44 pm
- Location: Michigan
-
stickman373
- Forum Commoner
- Posts: 30
- Joined: Mon Jul 22, 2002 10:26 am
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
You have to query the db to find out how many records there are in total to begin with, something like
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
Now we get the rows required
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
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 pageCode: 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 requiredYou 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 queryCode: Select all
$sql_products = "SELECT * FROM table WHERE something=somethingelse LIMIT ${offset}, ${per_page}";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
-
stickman373
- Forum Commoner
- Posts: 30
- Joined: Mon Jul 22, 2002 10:26 am
-
kkurkowski
- Forum Commoner
- Posts: 53
- Joined: Mon Dec 09, 2002 4:44 pm
- Location: Michigan
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.
Then this is at the bottom of the 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))
{
?>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
try this as ur second sql statement instead
and u cant use this statement then:
if u want use this:
Code: Select all
$sql_products = "SELECT * FROM news_news ORDER BY timestamp LIMIT $offset, $per_page";Code: Select all
$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news");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
try this as ur second sql statement instead
and u cant use this statement then:
if u want use this:
Code: Select all
$sql_products = "SELECT * FROM news_news ORDER BY timestamp LIMIT $offset, $per_page";Code: Select all
$result = mysql_query ("SELECT headline,news,link,user,timestamp FROM news_news");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
-
stickman373
- Forum Commoner
- Posts: 30
- Joined: Mon Jul 22, 2002 10:26 am
-
sirTemplar
- Forum Commoner
- Posts: 65
- Joined: Wed Dec 18, 2002 1:57 am
help: mulitple pages
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 instrucif ($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>
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