Connecting Tech Pros Worldwide Help | Site Map

Issue Implementing Paging Code with my Own

tharden3's Avatar
Site Addict
 
Join Date: Jul 2008
Location: Ocala, FL (United States)
Posts: 817
#1: Feb 1 '09
Hey all, I need some help with PHP code for paging my products on my site. I posted questions asking for help in the past, and was directed to this tutorial for help. The code that the tutorial gives is great, but I'm having a hard time synchronizing it with my own code to make the whole thing work.

My goal is to take four basic product details:
$thumb (a thumbnail that will go along with its corresponding info)
$type (the 'type' of product)
$number (the serial number that identifies the product)
$desc (a short description of the product)

$thumb is the only non-varchar type. I haven't quite figured out how to implement pictures yet, but I'm receiving help for that in another thread. For right now I'm focusing on taking those four basic product details, listing 15 or 20 per page, and then adding a links at the bottom for any page after the first one fills up. My code to list the products works fine, now I just need to take those products and implement pagination.

Here's the code I wrote for querying the products on my page:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include('books_login.php');
  3. $connection=mysql_connect($mysql_host,$mysql_user,$mysql_password);
  4. if(!$connection){
  5.    die("Could not connect to the database: <br/>". mysql_error());
  6. }
  7. $db_select=mysql_select_db($mysql_database);
  8. if(!$db_select){
  9.    die("Could not select the database: <br/>".mysql_error());
  10. }
  11. $query="select * from air_registers";
  12. $result=mysql_query($query);
  13. if(!$result){
  14.    die("Could not query the database: <br/>". mysql_error());
  15. }
  16. while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
  17.    $thumb=$row["Thumbnail"];
  18.    $type=$row["Type"];
  19.    $number=$row["Number"];
  20.    $desc=$row["Description"];
  21.    echo "$thumb";
  22.    echo "<div id='type'><strong>Model Type: </strong>$type</div>";
  23.    echo "<div id='number'><strong>Model #: </strong>$number</div>";
  24.    echo "<br />";
  25.    echo "<div id='desc'><strong>Description: </strong>$desc</div>";
  26.    echo "<br />";
  27.    echo "<hr>";
  28.  
  29. }
  30.  
  31. mysql_close($connection);
  32. ?>
Now here is that paging code.

I've tried taking the paging code, and using bits and pieces to help my code, but I seem to mess it up everytime I tweak it. I can't get it to come out right. Any help is greatly appreciated. Thanks Bytes.
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#2: Feb 1 '09

re: Issue Implementing Paging Code with my Own


Show us what you've tried and let us know where you've gone wrong.

Thanks,
Markus.
tharden3's Avatar
Site Addict
 
Join Date: Jul 2008
Location: Ocala, FL (United States)
Posts: 817
#3: Feb 1 '09

re: Issue Implementing Paging Code with my Own


How do I include the config.php and the opendb.php files at the top of the tutorial? Do I have to include those in my file manager?
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#4: Feb 1 '09

re: Issue Implementing Paging Code with my Own


Quote:

Originally Posted by tharden3 View Post

@ Markus:
How do I include the config.php and the opendb.php files at the top of the tutorial? Do I have to include those in my file manager?

No, no. Those are just some files used in that tutorial (if you read further back into the tutorial, you will see them be created). Basically all they do is connect to the database with the given configuration.

You just need to make that connection to the database on your paging ... page.
tharden3's Avatar
Site Addict
 
Join Date: Jul 2008
Location: Ocala, FL (United States)
Posts: 817
#5: Feb 1 '09

re: Issue Implementing Paging Code with my Own


Quote:

Originally Posted by Markus View Post

No, no. Those are just some files used in that tutorial (if you read further back into the tutorial, you will see them be created). Basically all they do is connect to the database with the given configuration.

You just need to make that connection to the database on your paging ... page.

oh, ok. That confused me a bit.

Well, everything works fine, but there were a few things that worried me. I'll show you my code and then address the issues. Maybe you can give me a few tips:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include('books_login.php');
  3. $connection=mysql_connect($mysql_host,$mysql_user,$mysql_password);
  4. if(!$connection){
  5.    die("Could not connect to the database: <br/>". mysql_error());
  6. }
  7. $db_select=mysql_select_db($mysql_database);
  8. if(!$db_select){
  9.    die("Could not select the database: <br/>".mysql_error());
  10. }
  11.  
  12. // how many rows to show per page
  13. $rowsPerPage = 4;
  14.  
  15. // by default we show first page
  16. $pageNum = 1;
  17.  
  18. // if $_GET['page'] defined, use it as page number
  19. if(isset($_GET['page']))
  20. {
  21.     $pageNum = $_GET['page'];
  22. }
  23.  
  24. // counting the offset
  25. $offset = ($pageNum - 1) * $rowsPerPage;
  26.  
  27. $query = " SELECT * FROM air_registers" .
  28.          " LIMIT $offset, $rowsPerPage";
  29. $result = mysql_query($query) or die('Error, query failed');
  30.  
  31.  
  32. while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
  33.    $thumb=$row["Thumbnail"];
  34.    $type=$row["Type"];
  35.    $number=$row["Number"];
  36.    $desc=$row["Description"];
  37.    echo "$thumb";
  38.    echo "<div id='type'><strong>Model Type: </strong>$type</div>";
  39.    echo "<div id='number'><strong>Model #: </strong>$number</div>";
  40.    echo "<br />";
  41.    echo "<div id='desc'><strong>Description: </strong>$desc</div>";
  42.    echo "<br />";
  43.    echo "<hr>";
  44. }
  45. // how many rows we have in database
  46. $query   = "SELECT COUNT(Number) AS numrows FROM air_registers";
  47. $result  = mysql_query($query) or die('Error, query failed');
  48. $row     = mysql_fetch_array($result, MYSQL_ASSOC);
  49. $numrows = $row['numrows'];
  50.  
  51. // how many pages we have when using paging?
  52. $maxPage = ceil($numrows/$rowsPerPage);
  53.  
  54. // print the link to access each page
  55. $self = $_SERVER['PHP_SELF'];
  56. $nav  = '';
  57.  
  58. for($page = 1; $page <= $maxPage; $page++)
  59. {
  60.    if ($page == $pageNum)
  61.    {
  62.       $nav .= " $page "; // no need to create a link to current page
  63.    }
  64.    else
  65.    {
  66.       $nav .= " <a href=\"$self?page=$page\">$page</a> ";
  67.    }
  68. }
  69. // ... the previous code
  70.  
  71. // creating previous and next link
  72. // plus the link to go straight to
  73. // the first and last page
  74.  
  75. if ($pageNum > 1)
  76. {
  77.    $page  = $pageNum - 1;
  78.    $prev  = " <a href=\"$self?page=$page\">[Prev]</a> ";
  79.  
  80.    $first = " <a href=\"$self?page=1\">[First Page]</a> ";
  81. }
  82. else
  83. {
  84.    $prev  = '&nbsp;'; // we're on page one, don't print previous link
  85.    $first = '&nbsp;'; // nor the first page link
  86. }
  87.  
  88. if ($pageNum < $maxPage)
  89. {
  90.    $page = $pageNum + 1;
  91.    $next = " <a href=\"$self?page=$page\">[Next]</a> ";
  92.  
  93.    $last = " <a href=\"$self?page=$maxPage\">[Last Page]</a> ";
  94. }
  95. else
  96. {
  97.    $next = '&nbsp;'; // we're on the last page, don't print next link
  98.    $last = '&nbsp;'; // nor the last page link
  99. }
  100.  
  101. // print the navigation link
  102. echo $first . $prev . $nav . $next . $last;
  103.  
  104. mysql_close($connection);
  105. ?>
Line 46 uses the COUNT() function, and I passed Number as the value. What if Number is NULL for one of my table entries though? Will it skip over that product?

Another little issue that isn't really causing me problems now, but might in the future, is when PHP runs into an error querying, it does not render the rest of the code for that website page. Not even the HTML. I know this is what it's supposed to do, and is a very normal thing. But how do get it to render at least all of the HTML if there is a problem with the PHP? I have HTML that follows my PHP code, and in one instance, the PHP could not query my database, and the rest of the site looked funky.
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,635
#6: Feb 1 '09

re: Issue Implementing Paging Code with my Own


Quote:

Originally Posted by tharden3 View Post

Another little issue that isn't really causing me problems now, but might in the future, is when PHP runs into an error querying, it does not render the rest of the code for that website page. Not even the HTML. I know this is what it's supposed to do, and is a very normal thing. But how do get it to render at least all of the HTML if there is a problem with the PHP?

one thing to consider is using Exceptions (as of PHP 5). the cool thing is, that you have the possibility to apply some emergency plan if your script/function/method fails.

see PHP: Exceptions
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#7: Feb 1 '09

re: Issue Implementing Paging Code with my Own


I'm not too sure about the COUNT(); I'll let some mysql savvy person answer that.

But to your second question: the logical (and best programming practice) thing to do is it completely seperate the HTML and the PHP. This is 'separating business logic (PHP - serverside) from your presentation logic (HTML - clientside). You should also be handling every possible error that may arise from your code.

http://www.paragoncorporation.com/Ar...x?ArticleID=21
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,745
#8: Feb 1 '09

re: Issue Implementing Paging Code with my Own


Quote:

Originally Posted by tharden3 View Post

Line 46 uses the COUNT() function, and I passed Number as the value. What if Number is NULL for one of my table entries though? Will it skip over that product?

Yep. Count only counts non-NULL fields.
You are usually better of using COUNT(*). It's usually faster to.
tharden3's Avatar
Site Addict
 
Join Date: Jul 2008
Location: Ocala, FL (United States)
Posts: 817
#9: Feb 1 '09

re: Issue Implementing Paging Code with my Own


Quote:

Originally Posted by Atli View Post

Yep. Count only counts non-NULL fields.
You are usually better of using COUNT(*). It's usually faster to.

Cool. I assumed there was something I could do about that. Thanks.
Reply


Similar PHP bytes