PDA

View Full Version : How to do this: NEXT 1 2 3 4 LAST etc for multiple pages


dysfunctionGazz
05-06-2005, 12:28 AM
Ive got a simple news script but at the moment its just a list.

I didnt know how to search this one as I don't know what the technique is called.

I want to be able to display say 10 results, and then at the bottom it say eg. 1 of 5 and then a NEXT PAGE link, or as in this title, a link to each page number. Basically somewhat like a forum, just not threaded or with members!

I know how to return a limited number of records in MySQL but thats about it.

I'm not asking for a solution per se but if someone could point me in the right direction, as i dont even know what the technique is called in order to learn it!!!

Thanks
Gazz

Brandoe85
05-06-2005, 12:35 AM
Do a search for pagination...you'll find plenty of results...heres a google (http://www.google.com/search?hl=en&lr=&c2coff=1&q=php+pagination&btnG=Search)

SeeIT Solutions
05-06-2005, 02:04 AM
It really depends on how you've got your news set up. You need to have some way of getting the next few results and also determning how many results you have in total.

dysfunctionGazz
05-06-2005, 10:27 AM
cool, thanks, i shall check it out now i know what its called! :D

dysfunctionGazz
05-06-2005, 11:18 AM
OK so I found me a script and have started fooling around with/ruining it.

Ive populated a db table with a load of rubbish and changed the relevant parameters in the script that I had.

The data displays OK, but for some reason the links PREV, NEXT etc are inactive! I can't see why that is.

My code is live here, so you can see the problem: http://www.suicidenotesmusic.net/nicola/pagination1.php

And the code i'm using is:

<?php

require ('connect.php');

$limit = 3;
$query_count = "SELECT count(*) FROM news";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

if(empty($page)){
$page = 1;
}


$limitvalue = $page * $limit - ($limit);
$query = "SELECT * FROM news LIMIT $limitvalue, $limit";
$result = mysql_query($query) or die("Error: " . mysql_error());

if(mysql_num_rows($result) == 0){
echo("Nothing to Display!");
}

$bgcolor = "#E0E0E0"; // light gray

echo("<table>");

while($row = mysql_fetch_array($result)){
if ($bgcolor == "#E0E0E0"){
$bgcolor = "#FF0000";
}else{
$bgcolor = "#E0E0E0";
}

echo("<tr bgcolor=".$bgcolor."><td>");
echo($row["newstitle"] . $row["dateposted"]);
echo("</td><td>");
echo($row["body"]);
echo("</td></tr>");
}

echo("</table>");

if($page != 1){
$pageprev = $page--;

echo("<a href=\"http://www.suicidenotesmusic.net/nicola/pagination.php&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"http://www.suicidenotesmusic.net/nicola/pagination.php?page=$i\">$i</a> ");
}
}


if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"http://www.suicidenotesmusic.net/nicola/pagination1.php?page=$i\">$i</a> ");
}
}

if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;

echo("<a href=\"http://www.suicidenotesmusic.net/nicola/pagination1.php?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}

mysql_free_result($result);

?>


Any takers on what bit I have screwed up?

dysfunctionGazz
05-06-2005, 04:51 PM
Just edited above code to reflect changes I made (i removed all PHP_SELF references and used absolute URL instead as i thought that might have been the problem).

I still am none the wiser with this, anyone see why these links are dead?