Jump to content


Photo

getting error when trying to get row count for pagination


  • Please log in to reply
24 replies to this topic

#21 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 08:13 AM

Unfortunately it's not paginating correctly. It just wants to display 5 results when there's 14 in the category. :(


I might be completely loosing the plot here, and if I am then somebody please shoot me as this is driving me insane, but the whole point of the pagination is that it only shows 5 results on the page, not 14!

If you click on your link up at the top of this page, it works fine, then when you click page 2 it doesn't work, but the link to page 2 is incorrect, as it doesn't contain the 'categoryid' in the URL.  Try this link to page 2 and then this link to page 3.

Have you made the modifications to the links that I suggested so that they include the 'categoryid' in the URL?

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#22 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 18 October 2006 - 01:58 PM

I'm emailing you a Valium. :)

It's not that complicated. Here's the whole thing in a nutshell:

1) Yes, it should show 5 per page and not 14. It's set to show 5 per page.
2) But, it's showing the first 5 only and the subsequent pages show 0. Meaning it's only showing a total of 5 results and not the 14 in 3 successive pages ( Page1 | Page 2| Page 3) as it should
3) I don't want it to show just 5 total results. I want it to show 14 total results but on 3 pages (hence the pagination)
4) Category ID 4 has 14 members in it. Not 5. Therefore it should show 3 pages of results based upon the fact we have it set for 5 results per page. Period.
5) I HAVE inserted your category id code into the script and uploaded it to the server and that's what is currently located at this link:

http://www.plateaupr...hp?categoryid=4

Note the fact that it shows 5 results and NO page link to go to results 6-10 (page 2) or 11-14 (page 3). THAT is the problem.

The code is EXACTLY as you posted it. I copied and pasted your post directly into my page. No deviations...no edits...exactly.


#23 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 18 October 2006 - 02:08 PM

ok... That makes sense, disregard what I posted in the early hours of this morning, I thinks it's only serving to confuse matters further.  ;D

Take the code that you have on page category-page2a.php as that code is almost right, and then just change the link at the bottom from this:

echo "<a class=\"body\" href=\"".$_SERVER['PHP_SELF']."?page=$i\">Page $i</a> ";

To this:

echo "<a class=\"body\" href=\"".$_SERVER['PHP_SELF']."?categoryid=$cat_id&page=$i\">Page $i</a> ";

and I think we'll finally be there.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#24 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 18 October 2006 - 02:11 PM

the total number of results found by the query is being run wrong.  you're using mysql_num_rows() which is a bit silly, because a COUNT() will only ever return one "row."  use mysql_result() to extract the correct result:

$sql = "SELECT COUNT(*) FROM members_cat WHERE categoryid='$cat_id'";
$result = mysql_query($sql) or die('count query failed for some reason.');
$total_results = mysql_result($result, 0, 0);

hope this at least amends the total results found.  this line is also a little faulty:

echo "<font class='bodytext'>There are <b>$num_rows</b> professionals in this category</font><br><br>\n";

$num_rows is just how many rows that page itself pulled.  $total_results is what you should be echoing in that line.

#25 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 18 October 2006 - 02:30 PM

He shoots...he SCORRRRRRRRRES! :)

Ok, akitchin...that was the code 'tweak' needed for that part. Huggie, adding the category id to the link was the other part of the puzzle. Independently they wouldn't work. Collectively they do. Here's what I did:

* changed the COUNT code to what akitchin provided with 'mysql_result' instead of 'mysql_num_rows'
* changed the display of the total results from $num_rows (which actually returned the correct amount, by the way) to $total_results
* added Huggie's categoryid=$cat_id& to the URL string

Now I get 14 results, 5 per page, and the pagination displays properly and each page has the specified number of results (5 or less)

Take a look: http://www.plateaupr...hp?categoryid=4

Whew! Time for a beer...even though it's 7:30 am :)  Thanks for the help, gentlemen!

Just a note of interest... pagination is  a BIG topic in the forums. This code works and is easy to implement. I'm going to modify the title a bit to reflect 'pagination' so it can possibly help others.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users