Clean Previous Next Script for MySQL results

Reply

Join Date: Aug 2003
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Clean Previous Next Script for MySQL results

 
0
  #1
Nov 16th, 2003
This script will display a previous next script that will look like this:
<< prev 1 2 3 4 5 next >>
It is clean, and avoids large numbers of pages in the prev/next links

  1. $server = "localhost";
  2. $user = "username";
  3. $pass = "password";
  4. $databasename = "yourDBname";
  5. $db = mysql_connect($server, $user, $pass);
  6. mysql_select_db($databasename,$db);
  7.  
  8. $sql = "SELECT * FROM yourtable WHERE yourconditions ORDER BY your_order ";
  9. $query = mysql_query($sql,$db);
  10. $total_results = mysql_num_rows($query);
  11. $limit = "15"; //limit of archived results per page.
  12. $total_pages = ceil($total_results / $limit); //total number of pages
  13. if (empty($page))
  14. {
  15. $page = "1"; //default page if none is selected
  16. }
  17. $offset = ($page - 1) * $limit; //starting number for displaying results out of DB
  18.  
  19. $query = "SELECT * FROM yourtable WHERE yourconditions ORDER BY your_order LIMIT $offset, $limit";
  20. $result = mysql_query($query);
  21. //This is the start of the normal results...
  22.  
  23. while ($row = mysql_fetch_array($result))
  24. {
  25. // display your results as you see fit here.
  26. }
  27. mysql_close();
  28.  
  29.  
  30. // This is the Previous/Next Navigation
  31. echo "<font face=Verdana size=1>";
  32. echo "Pages:($total_pages)&nbsp;&nbsp;"; // total pages
  33. if ($page != 1)
  34. {
  35. echo "<a href=$PHP_SELF?page=1><< First</a>&nbsp;&nbsp;&nbsp;"; // First Page Link
  36. $prevpage = $page - 1;
  37. echo "&nbsp;<a href=$PHP_SELF?page=$prevpage><<</a>&nbsp;"; // Previous Page Link
  38. }
  39. if ($page == $total_pages)
  40. {
  41. $to = $total_pages;
  42. }
  43. elseif ($page == $total_pages-1)
  44. {
  45. $to = $page+1;
  46. }
  47. elseif ($page == $total_pages-2)
  48. {
  49. $to = $page+2;
  50. }
  51. else
  52. {
  53. $to = $page+3;
  54. }
  55. if ($page == 1 || $page == 2 || $page == 3)
  56. {
  57. $from = 1;
  58. }
  59. else
  60. {
  61. $from = $page-3;
  62. }
  63.  
  64. for ($i = $from; $i <= $to; $i++)
  65.  
  66. {
  67. if ($i == $total_results) $to=$total_results;
  68. if ($i != $page)
  69. {
  70. echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>";
  71. }
  72. else
  73. {
  74. echo "<b><font face=Verdana size=2>[$i]</font></b>";
  75. }
  76. if ($i != $total_pages)
  77. echo "&nbsp;";
  78. }
  79. if ($page != $total_pages)
  80. {
  81. $nextpage = $page + 1;
  82. echo "&nbsp;<a href=$PHP_SELF?page=$nextpage>>></a>&nbsp;"; // Next Page Link
  83. echo "&nbsp;&nbsp;&nbsp;<a href=$PHP_SELF?page=$total_pages>Last >></a>"; // Last Page Link
  84. }
  85. echo "</font>";
  86.  
  87. // This is the end of the Previous/Next Navigation

I'll definitely redo this one soon, and comment the code a little better
Last edited by cscgal; Nov 1st, 2007 at 6:08 am. Reason: Fixed code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 127
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Clean Previous Next Script for MySQL results

 
0
  #2
Feb 1st, 2004
I'm still waiting on your redo The only reason I'm asking is because inscissor was over at my house the other day and we were talking about how he just set up something like this on a php site he was working on ... so it reminded me.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 6
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Clean Previous Next Script for MySQL results

 
0
  #3
Feb 1st, 2004
Yeah I remember this. It's a good thing that he pasted his code because I don't think one site exists that shows how to do this. Especially this clean. It's rare.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

 
0
  #4
Feb 3rd, 2004
hi all, first of all, thank you! this is a very nice script..

I am no coder, i basically do a little bit of php, i try hehe

My question is, how to display the results correctly? Let me tell you the situation:

I have a table named "screenshots" with names/date of files in it and i want to "echo" the names of the files correctly. I know my english is terrible, im sorry.

heres what ive tried:

  1. <?php
  2. include('config.php');
  3. $db = mysql_connect($dbserver, $dbuser, $dbpass);
  4. mysql_select_db($dbname,$db);
  5. $sql = "SELECT id,big,thumb,postdate, DATE_FORMAT(postdate, '%d/%m/%Y @ %T ') AS pdate FROM screenshots ORDER BY id DESC";
  6. $query = mysql_query($sql,$db);
  7. $total_results = mysql_num_rows($query);
  8. $limit = "12"; //limit of archived results per page.
  9. $total_pages = ceil($total_results / $limit); //total number of pages
  10.  
  11. if (empty($page))
  12. {
  13. $page = "1"; //default page if none is selected
  14. }
  15. $offset = ($page - 1) * $limit; //starting number for displaying results out of DB
  16. $query = "SELECT id,big,thumb,postdate, DATE_FORMAT(postdate, '%d/%m/%Y @ %T ') AS pdate FROM screenshots ORDER BY id DESC LIMIT $offset, $limit";
  17. $result = mysql_query($query);
  18. //This is the start of the normal results...
  19. $screen_id = mysql_result($result,$row,"id");
  20. $screen_b = mysql_result($result,$row,"big");
  21. $screen_t = mysql_result($result,$row,"thumb");
  22. $screen_date = mysql_result($result,$row,"pdate");
  23.  
  24. while ($row = mysql_fetch_array($result))
  25. {
  26. echo "$screen_t"; // display your results as you see fit here.
  27. }
  28. mysql_close();
  29.  
  30. ***CUT***

i simply want to display the 12 latest images and so on for the other pages, i only need to print the filenames correctly, can someone help me out

thanks alot for your time

fred
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

 
0
  #5
Feb 3rd, 2004
well, i have found the solution, was quite simple..i told you i am not a coder hehe

echo $row['title']."<br />";
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

 
0
  #6
Feb 3rd, 2004
  1.  
  2. ***CUT***
  3.  
  4. while ($row = mysql_fetch_array($result))
  5. {
  6. echo "<a HREF=images/ingame/".$row['big']." TARGET=_blank><img src=images/ingame/thumbs/".$row['thumb']." border=0 alt=".$row['big']."></a>&nbsp;&nbsp;"; // display your results as you see fit here.
  7. }
  8.  
  9. ***CUT***

This code will display the pictures i want, but i have another problem , How do i create a nice 3x4 table to display the images ? please..


fred
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 127
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Clean Previous Next Script for MySQL results

 
0
  #7
Feb 3rd, 2004
I don't know PHP for anything but the following is code for one row ...

  1. <tr>
  2. <td> code for picture in row 1</td>
  3. </tr>
  4.  
  5. <tr>
  6. <td> code for picture in row 2 </td>
  7. </tr>

So now, perhaps use code like the following? Keep in mind I'm NOT a php programmer.

  1. echo "<table><tr>";
  2. while ($row = mysql_fetch_array($result))
  3. {
  4. echo "<td><a HREF=images/ingame/".$row['big']." TARGET=_blank><img src=images/ingame/thumbs/".$row['thumb']." border=0 alt=".$row['big']."></a></td>"; // display your results as you see fit here.
  5. if (($row % 3) == 0)
  6. { echo "</tr><tr>"; }
  7. }
  8. </tr></table>
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

 
0
  #8
Feb 3rd, 2004
wow...you so fast! i ll let you know if if works, thanks alot cscgal

fred
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,035
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 127
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Clean Previous Next Script for MySQL results

 
0
  #9
Feb 3rd, 2004
I doubt it was right. In fact, I just found an error ... I don't want to see if $row % 3 ... I want to see what we're on ... change that to $counter % 3, and directly above, add counter++; Then, above the while loop, add $counter=0; Sorry
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

 
0
  #10
Feb 3rd, 2004
  1. $counter=0;
  2. echo "<table><tr>";
  3. while ($row = mysql_fetch_array($result))
  4. {
  5. echo "<td><a HREF=images/ingame/".$row['big']." TARGET=_blank><img src=images/ingame/thumbs/".$row['thumb']." border=0 alt=".$row['big']."></a></td>"; // display your results as you see fit here.
  6. $counter++;
  7. if (($counter % 3) == 0){
  8. echo "</tr><tr>";
  9. }
  10. }
  11. echo "</tr></table>";

awesome!!!!!!


thanks !!!

fred
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC