Connecting Tech Pros Worldwide Help | Site Map

pagination error

  #1  
Old June 17th, 2009, 06:01 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340
hi i have made a simple pagination but it is creating problem , the problem is that if there are any products under the category that is clicked in the left menu then the page works fine but if it does not have any product listed under that category then the page halts no eror is diplayed and the page halts at the line
Expand|Select|Wrap|Line Numbers
  1. $total = mysql_result($result, 0, 0) or die(mysql_error());
  2.  
the working example can be seen here link
here is the full code for that pagination each time the page loads it checks if the variable $bingo is poted or not which contains a cat_id and against that category id it fetches the results and makes the pages
Expand|Select|Wrap|Line Numbers
  1. class Pager{
  2.        function getPagerData($numHits, $limit, $page){ 
  3.            $numHits  = (int) $numHits; 
  4.            $limit    = max((int) $limit, 1); 
  5.            $page     = (int) $page; 
  6.            $numPages = ceil($numHits / $limit);
  7.            $page = max($page, 1); 
  8.            $page = min($page, $numPages);
  9.            $offset = ($page - 1) * $limit;
  10.            $ret = new stdClass;
  11.            $ret->offset   = $offset; 
  12.            $ret->limit    = $limit; 
  13.            $ret->numPages = $numPages; 
  14.            $ret->page     = $page; 
  15.            return $ret; 
  16.        } 
  17.        }
  18.     if(!empty($_GET['bingo'])){$cat_id=$_GET['bingo'];}else{$cat_id='24';}
  19.     if(!empty($_REQUEST['page'])){$page = $_REQUEST['page'];}else{$page=1;}
  20.     $limit=6;
  21.     $result = mysql_query("SELECT count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
  22.  
  23.     $total = mysql_result($result, 0, 0) or die(mysql_error());
  24.     $pager = Pager::getPagerData($total, $limit, $page);
  25.     $offset= $pager->offset;
  26.     $limit= $pager->limit;
  27.     $page= $pager->page;
  28.     $error="";
  29.  
  #2  
Old June 17th, 2009, 07:13 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


Hi.

You don't actually check that the query was successful before you use the mysql_result function.

You should always check the result object from a mysql_query call, or any other function that returns a result, before you use it.

Try adding a if statement that simply checks if the $result variable is false, and print an error if it is.
  #3  
Old June 17th, 2009, 07:30 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


i added this statement
Expand|Select|Wrap|Line Numbers
  1. if($result!=false){
  2.         $total = mysql_result($result, 0, 0) or die(mysql_error());
  3.     }else{
  4. $total = 0;
  5. $error="No results matched your query";
  6. }
  7.  
but it still not working it still does not pass that line
  #4  
Old June 17th, 2009, 07:35 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


also that when i click any category which does not have any product listed against it the line
Expand|Select|Wrap|Line Numbers
  1. $result= mysql_query("select count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
  2.  
this line does not return false to $result infact it returns Resource id #5
regards,
Omer Aslam
  #5  
Old June 17th, 2009, 07:41 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


And you just get an empty page... odd.

Have you tried turning the error messages on?
Expand|Select|Wrap|Line Numbers
  1. ini_set('display_errors', true);
  2. error_reporting(E_ALL);

Also:
Expand|Select|Wrap|Line Numbers
  1. if($result!=false){
!=false is a double-negative. You can remove it and get the same results.
Unless you need to check against the actual boolean value FALSE, in which case you should do !==false
  #6  
Old June 17th, 2009, 07:43 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


Quote:
Originally Posted by omerbutt View Post
also that when i click any category which does not have any product listed against it the line
Expand|Select|Wrap|Line Numbers
  1. $result= mysql_query("select count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
  2.  
this line does not return false to $result infact it returns Resource id #5
regards,
Omer Aslam
It always returns a resource ID on success, even if it's an empty set.
  #7  
Old June 18th, 2009, 04:01 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


Quote:
Originally Posted by Atli View Post
And you just get an empty page... odd.

Have you tried turning the error messages on?
Expand|Select|Wrap|Line Numbers
  1. ini_set('display_errors', true);
  2. error_reporting(E_ALL);
YES i had already turned
error_reporting(E_ALL)
also i included
Expand|Select|Wrap|Line Numbers
  1. ini_set('display_errors', true);
but there were no errors displayed

Quote:
Also:
Expand|Select|Wrap|Line Numbers
  1. if($result!=false){
!=false is a double-negative. You can remove it and get the same results.
Unless you need to check against the actual boolean value FALSE, in which case you should do !==false
i didi this way but it is again entring the if part not the else and then it halts as usual
  #8  
Old June 18th, 2009, 05:08 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


Quote:
Originally Posted by Atli View Post
It always returns a resource ID on success, even if it's an empty set.
then what should i be comparing $result with ?
  #9  
Old June 18th, 2009, 05:10 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


Have you tried counting the rows?
Expand|Select|Wrap|Line Numbers
  1. if($result && mysql_num_rows($result) > 0) {
  2.     mysql_do_whatever_you_want($result);
  3. }
  4. else {
  5.     echo "Sorry. No results.";
  6. }
Like I say, even if there are no results in the set, $result will still return true.
If you want to know if there were any results just testing the $result won't do.
  #10  
Old June 18th, 2009, 06:11 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


Quote:
Originally Posted by Atli View Post
Have you tried counting the rows?
Expand|Select|Wrap|Line Numbers
  1. if($result && mysql_num_rows($result) > 0) {
  2.     mysql_do_whatever_you_want($result);
  3. }
  4. else {
  5.     echo "Sorry. No results.";
  6. }
Like I say, even if there are no results in the set, $result will still return true.
If you want to know if there were any results just testing the $result won't do.
no i checked it that way too it is still entring that if() part :(
  #11  
Old June 18th, 2009, 06:36 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


Quote:
Originally Posted by omerbutt View Post
no i checked it that way too it is still entring that if() part :(
Gahh. I just realised that you are using a COUNT query.
Those always return a row.
(I should have noticed earlier, but I was distracted :P)

You need to fetch the value in that row and test that.
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(*) FROM mytbl";
  2. $result = mysql_query($sql) or die(mysql_error());
  3. $count = mysql_result($result, 0, 0);
  4.  
  5. if($count > 0) {
  6.     // Do your thing here.
  7. }
  8. else {
  9.     echo "Sorry. No cookie for you.";
  10. }
Just make sure the code inside the if block prints something :)
  #12  
Old June 18th, 2009, 06:44 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


Quote:
Originally Posted by Atli View Post
Gahh. I just realised that you are using a COUNT query.
Those always return a row.
(I should have noticed earlier, but I was distracted :P)

You need to fetch the value in that row and test that.
Expand|Select|Wrap|Line Numbers
  1. $sql = "SELECT COUNT(*) FROM mytbl";
  2. $result = mysql_query($sql) or die(mysql_error());
  3. $count = mysql_result($result, 0, 0);
  4.  
  5. if($count > 0) {
  6.     // Do your thing here.
  7. }
  8. else {
  9.     echo "Sorry. No cookie for you.";
  10. }
Just make sure the code inside the if block prints something :)

yuo are not getting what i am trying to tell you the line mysql_result will cause every thing to halt if there is no record and i need to put it into the if check this wont work
  #13  
Old June 18th, 2009, 07:09 PM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


Hmmm...

Try to remove the die statement from your mysql_result call.
I'm not sure the mysql_error function would return anything if the mysql_result function returned false, which would halt the script without any explanation.

O, and P.S.
Your Pager::getPagerData function should be declared static if you intend to use it like that.
  #14  
Old June 19th, 2009, 06:03 AM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


Quote:
Originally Posted by Atli View Post
Hmmm...

Try to remove the die statement from your mysql_result call.
I'm not sure the mysql_error function would return anything if the mysql_result function returned false, which would halt the script without any explanation.

O, and P.S.
Your Pager::getPagerData function should be declared static if you intend to use it like that.
yes it is not returning any thing nor have i added it.
  #15  
Old June 19th, 2009, 09:39 AM
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,701
Provided Answers: 4

re: pagination error


Quote:
Originally Posted by omerbutt View Post
yes it is not returning any thing nor have i added it.
What do you mean?

In the code you posted, you do this:
Expand|Select|Wrap|Line Numbers
  1. $total = mysql_result($result, 0, 0) or die(mysql_error());
If the result set is empty (no first row to jump to) or the contents of the first field in the first row would evaluate as FALSE (0, null, etc..), then the die function would halt the execution of the code without any warning.
(The mysql_error function doesn't return an error if the mysql_result function fails, thus you get no error message.)

To see what I mean, try simply adding a custom error message in the die, rather than mysql_error.
  #16  
Old June 24th, 2009, 04:53 PM
omerbutt's Avatar
Needs Regular Fix
 
Join Date: Nov 2006
Location: Earth Obviously :P
Posts: 340

re: pagination error


i did it here see the example here
http://toylandpk.com/
here is what i did
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("SELECT count(*) FROM ".PRODUCT." p,".PRODUCT_DESCRIPTION." pd,".PRODUCT_TO_CATEGORY." ptc WHERE p.prod_id=pd.prod_id AND ptc.cat_id='$cat_id' AND ptc.prod_id=p.prod_id");
  2.     //$result = mysql_query("select count(*) from ".PRODUCT." where ".PRODUCT.".prod_status='1'");
  3.     if(mysql_num_rows($result)){
  4.         $query_data=mysql_fetch_array($result);
  5.         $total=$query_data[0];
  6.     }
  7.     if($total>0){
  8.         $pager = Pager::getPagerData($total, $limit, $page);
  9.         $offset= $pager->offset;
  10.         $limit= $pager->limit;
  11.         $page= $pager->page;
  12.     }
  13.  
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pagination Problem Chris H answers 2 March 25th, 2006 05:35 AM
phpBB Pagination Error - Help! Somerset Bob answers 2 July 17th, 2005 04:23 AM
Pagination error in phpbb - desperate for help! Somerset Bob answers 0 July 17th, 2005 03:45 AM
Pagination error in phpbb - desperate for help Somerset Bob answers 0 July 17th, 2005 03:44 AM