User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,865 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,568 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 14343 | Replies: 22
Reply
Join Date: Aug 2003
Location: Steamboat Springs, CO
Posts: 82
Reputation: himerus is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 0
himerus's Avatar
himerus himerus is offline Offline
Junior Poster in Training

Clean Previous Next Script for MySQL results

  #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 5:08 am. Reason: Fixed code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 107
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Clean Previous Next Script for MySQL results

  #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.
Reply With Quote  
Join Date: Feb 2002
Location: Long Island, NY
Posts: 1,134
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Rep Power: 12
Solved Threads: 2
Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: Clean Previous Next Script for MySQL results

  #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.
_.:: my websites ::._
blog @ www.samaru.net * engi No Jutsu @ www.narutorp.net * portfolio @ shinylight.com
deviantART: inscissor
Reply With Quote  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

  #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:

<?php
include('config.php');
$db = mysql_connect($dbserver, $dbuser, $dbpass); 
	mysql_select_db($dbname,$db); 
		$sql = "SELECT id,big,thumb,postdate, DATE_FORMAT(postdate, '%d/%m/%Y @ %T ') AS pdate FROM screenshots ORDER BY id DESC"; 
		$query = mysql_query($sql,$db); 
		$total_results = mysql_num_rows($query); 
		$limit = "12"; //limit of archived results per page. 
		$total_pages = ceil($total_results / $limit); //total number of pages
 
if (empty($page)) 
	{ 
		$page = "1"; //default page if none is selected 
	} 
$offset = ($page - 1) * $limit; //starting number for displaying results out of DB 
	$query = "SELECT id,big,thumb,postdate, DATE_FORMAT(postdate, '%d/%m/%Y @ %T ') AS pdate FROM screenshots ORDER BY id DESC LIMIT $offset, $limit"; 
	$result = mysql_query($query); 
//This is the start of the normal results... 
$screen_id = mysql_result($result,$row,"id"); 
$screen_b = mysql_result($result,$row,"big");
$screen_t = mysql_result($result,$row,"thumb");
$screen_date = mysql_result($result,$row,"pdate");
 
	while ($row = mysql_fetch_array($result)) 
		{ 
		echo "$screen_t"; // display your results as you see fit here.
		} 
		mysql_close();
 
***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  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

  #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  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

  #6  
Feb 3rd, 2004
 
***CUT***
 
while ($row = mysql_fetch_array($result)) 
 {
 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.
 }
 
***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  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 107
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Clean Previous Next Script for MySQL results

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

<tr>
  <td> code for picture in row 1</td>
</tr>
 
<tr>
  <td> code for picture in row 2 </td>
</tr>

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

echo "<table><tr>";
while ($row = mysql_fetch_array($result)) 
 {
 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.
if (($row % 3) == 0)
  { echo "</tr><tr>"; }
 }
</tr></table>
Reply With Quote  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

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

fred
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,879
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 107
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Clean Previous Next Script for MySQL results

  #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
Reply With Quote  
Join Date: Feb 2004
Posts: 11
Reputation: fred999 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
fred999 fred999 is offline Offline
Newbie Poster

Re: Clean Previous Next Script for MySQL results

  #10  
Feb 3rd, 2004
$counter=0;
echo "<table><tr>";
while ($row = mysql_fetch_array($result)) 
 {
  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.
 $counter++;
 if (($counter % 3) == 0){
  echo "</tr><tr>"; 
  }
}
echo "</tr></table>";


awesome!!!!!!


thanks !!!

fred
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 6:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC