Page 1 of 1
Next and Previous Statements
Posted: Wed Dec 11, 2002 2:34 pm
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?
Posted: Wed Dec 11, 2002 3:10 pm
by qads
search this forum for next and previous links and you will find loads of posts on it.
Posted: Wed Dec 11, 2002 6:41 pm
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.
Posted: Wed Dec 11, 2002 8:47 pm
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>";
}
}
Posted: Wed Dec 11, 2002 9:52 pm
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
Posted: Thu Dec 12, 2002 6:36 am
by stickman373
take WHERE something=somethingelse out of both sql statements or change it so it applies to your database

Posted: Thu Dec 12, 2002 2:02 pm
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>";
}
}
?>
Posted: Thu Dec 12, 2002 3:50 pm
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");
Posted: Thu Dec 12, 2002 3:50 pm
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");
Posted: Thu Dec 12, 2002 4:16 pm
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.
Posted: Thu Dec 12, 2002 6:15 pm
by stickman373
glad it works, good luck with your site

help: mulitple pages
Posted: Thu Dec 19, 2002 4:17 pm
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>
Posted: Thu Dec 19, 2002 4:23 pm
by sirTemplar
sorry. i may be unclear. what i mean when i said "above code to work" is the code by
stickman373, not mine
