Mortgage Loans - New Vauxhall Cars - Mortgages - Buy PSP - Debt Consolidation

PDA

View Full Version : Pagination help please


crazyryan
Jul 31st 2007, 5:41 am
Hey, having some problems with this, I followed the phpfreaks pagination tutorial.

My result:
http://www.phpmediascript.com/news/index.php

As you can see it isn't really functioning right (the links).

My code:

<?php
include("config.php");

$limit = 1;
// Sets how many results shown per page

$query_count = "SELECT count(*) FROM articles";
// Sets what we want to pull from the database
// count(*) is better for large databases (thanks Greg!)

$result_count = mysql_query($query_count);
// Pulls what we want from the database

$totalrows = mysql_num_rows($result_count);
// This counts the number of users

if(empty($page)){ // Checks if the $page variable is empty (not set)
$page = 1; // If it is empty, we're on page 1
}

$limitvalue = $page * $limit - ($limit);

$article = mysql_query("SELECT * FROM `articles` LIMIT $limitvalue, $limit");

$check = mysql_num_rows($article);

if ($check < 1) {

echo "no rows";

}

else {

while($row = mysql_fetch_array($article)) {

$title = $row['title'];
$short = $row['short'];
$full = $row['full'];
$comments = $row['comments'];

echo "\n\n<div class=\"container\">\n<div class=\"title\"><a href=\"full.php?id=".$row['id']."\">" . $title . "</a></div>\n";
echo $short;
if($short != $full) { echo "..<br /><a href=\"full.php?id=".$row['id']."\">Read More</a>";
}
if ($comments != 0) {
echo "<br /><a href=\"full.php?id=".$row['id']."\">$comments comments</a></strong>";
}
echo "</div><br /><br />\n\n";


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

echo("<a href=\"$PHP_SELF&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=\"$PHP_SELF?page=$i\">$i</a> ");
}
}


if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}

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

echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}

}
}
?>

ecentricNick
Jul 31st 2007, 6:56 am
I think your problem lies here...

$query_count = "SELECT count(*) FROM articles";
$result_count = mysql_query($query_count);
$totalrows = mysql_num_rows($result_count);

$totalrows will ALWAYS contain 1, because the select count query will always return just 1 row, containing your count. Instead of counting the rows returned by that query, you need to get the value out of the result set.

All of which is redundant anyway because you query it all again later! So you're doing 2 queries when you could be doing one.

Try inserting...
$totalrows = $check;

immediately after
$check = mysql_num_rows($article);

which should get you the correct value in totalrows before it's needed. If that works, you can remove all the top part.

crazyryan
Jul 31st 2007, 7:06 am
Hey,
Thanks, but unfortunately that didn't work.
I didn't see any difference.

ecentricNick
Jul 31st 2007, 7:50 am
interestingly, if you type in...

http://www.phpmediascript.com/news/index.php?page=4

or

http://www.phpmediascript.com/news/index.php?page=3

or any number for that matter, it displays the right result but...

the previous link is one number out and the next link doesn't get highlighted at all.

crazyryan
Jul 31st 2007, 10:56 am
Fixed with a new pagination script.

nagasharmi
Aug 2nd 2007, 1:55 am
are you using count(*)
count(*) means we need some condition like where group by field name
otherwise give
select count(field name) from tablanme where condition