| | |
Clean Previous Next Script for MySQL results
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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
I'll definitely redo this one soon, and comment the code a little better
<< 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)
$server = "localhost"; $user = "username"; $pass = "password"; $databasename = "yourDBname"; $db = mysql_connect($server, $user, $pass); mysql_select_db($databasename,$db); $sql = "SELECT * FROM yourtable WHERE yourconditions ORDER BY your_order "; $query = mysql_query($sql,$db); $total_results = mysql_num_rows($query); $limit = "15"; //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 * FROM yourtable WHERE yourconditions ORDER BY your_order LIMIT $offset, $limit"; $result = mysql_query($query); //This is the start of the normal results... while ($row = mysql_fetch_array($result)) { // display your results as you see fit here. } mysql_close(); // This is the Previous/Next Navigation echo "<font face=Verdana size=1>"; echo "Pages:($total_pages) "; // total pages if ($page != 1) { echo "<a href=$PHP_SELF?page=1><< First</a> "; // First Page Link $prevpage = $page - 1; echo " <a href=$PHP_SELF?page=$prevpage><<</a> "; // Previous Page Link } if ($page == $total_pages) { $to = $total_pages; } elseif ($page == $total_pages-1) { $to = $page+1; } elseif ($page == $total_pages-2) { $to = $page+2; } else { $to = $page+3; } if ($page == 1 || $page == 2 || $page == 3) { $from = 1; } else { $from = $page-3; } for ($i = $from; $i <= $to; $i++) { if ($i == $total_results) $to=$total_results; if ($i != $page) { echo "<a href=$PHP_SELF?showold=yes&page=$i>$i</a>"; } else { echo "<b><font face=Verdana size=2>[$i]</font></b>"; } if ($i != $total_pages) echo " "; } if ($page != $total_pages) { $nextpage = $page + 1; echo " <a href=$PHP_SELF?page=$nextpage>>></a> "; // Next Page Link echo " <a href=$PHP_SELF?page=$total_pages>Last >></a>"; // Last Page Link } echo "</font>"; // 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
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.
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. 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.
•
•
Join Date: Feb 2004
Posts: 11
Reputation:
Solved Threads: 0
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:
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
I am no coder, i basically do a little bit of php, i try
heheMy 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)
<?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
•
•
Join Date: Feb 2004
Posts: 11
Reputation:
Solved Threads: 0
PHP Syntax (Toggle Plain Text)
***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> "; // 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
I don't know PHP for anything but the following is code for one row ...
So now, perhaps use code like the following? Keep in mind I'm NOT a php programmer.
PHP Syntax (Toggle Plain Text)
<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.
PHP Syntax (Toggle Plain Text)
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>
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
•
•
Join Date: Feb 2004
Posts: 11
Reputation:
Solved Threads: 0
PHP Syntax (Toggle Plain Text)
$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
![]() |
Other Threads in the PHP Forum
- Previous Thread: NEW to PHP
- Next Thread: Regarding display of mac address of a system
| Thread Tools | Search this Thread |
# address apache api array autoincrement beginner binary broken cakephp checkbox class clean cms code countingeverycharactersfromastring crack cron curl database date decode dehasher directory display dissertation dynamic echo email error fairness file files folder form forms function functions google href htaccess html image include incode insert ip javascript joomla legislation limit link login mail masterthesis match menu method mlm multiple mysql newsletters oop pagerank paypal pdf persist php play protocol query question radio random remote root script search server sessions simple sms soap source space spam sql support! syntax system table tutorial update upload url validator variable video web youtube






