943,718 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 26526
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 16th, 2003
0

Clean Previous Next Script for MySQL results

Expand Post »
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

php Syntax (Toggle Plain Text)
  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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
himerus is offline Offline
82 posts
since Aug 2003
Feb 1st, 2004
0

Re: Clean Previous Next Script for MySQL results

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 1st, 2004
0

Re: Clean Previous Next Script for MySQL results

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.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

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:

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fred999 is offline Offline
11 posts
since Feb 2004
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

well, i have found the solution, was quite simple..i told you i am not a coder hehe

echo $row['title']."<br />";
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fred999 is offline Offline
11 posts
since Feb 2004
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fred999 is offline Offline
11 posts
since Feb 2004
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

I don't know PHP for anything but the following is code for one row ...

PHP Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  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>
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

wow...you so fast! i ll let you know if if works, thanks alot cscgal

fred
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fred999 is offline Offline
11 posts
since Feb 2004
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

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
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Feb 3rd, 2004
0

Re: Clean Previous Next Script for MySQL results

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fred999 is offline Offline
11 posts
since Feb 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in PHP Forum Timeline: embedded web server for remote access
Next Thread in PHP Forum Timeline: Duplicate Wordpress Installation...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC