WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A simple chunk of code to take a SQL query and display the results xx per page. Also displays next, previous, and direct links to each page number.
Categories : Databases, Directories, General SQL Update Picture
Matt Whitted
Date : Feb 03rd 2000
Grade : 2 of 5 (graded 8 times)
Viewed : 21015
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Matt Whitted
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<? php
// Simple chunk of code to take a db query and display it xx entries
// per page, by Matt Whitted (admin@virtuaweb.net).
// Not the cleanest code, but it works.

// Number of entries per page
$per_page = 25;

// You will need to change this. This should be your normal SQL query that
// you use to get the info to be displayed from the db.
$sql_text = ("SELECT * from table");


// Set page #, if no page isspecified, assume page 1
if (!$page) {
$page = 1;
}
$prev_page = $page - 1;
$next_page = $page + 1;

$query = mysql_query($sql_text);

// Set up specified page
$page_start = ($per_page * $page) - $per_page;
$num_rows = mysql_num_rows($query);

if ($num_rows <= $per_page) {
$num_pages = 1;
} else if (($num_rows % $per_page) == 0) {
$num_pages = ($num_rows / $per_page);
} else {
$num_pages = ($num_rows / $per_page) + 1;
}
$num_pages = (int) $num_pages;

if (($page > $num_pages) || ($page < 0)) {
error("You have specified an invalid page number");
}

//
// Now the pages are set right, we can
// perform the actual displaying...
$sql_text = $sql_text . " LIMIT $page_start, $per_page";
$query = mysql_query($sql_text);

while ($result = mysql_fetch_array($query)) {
// This stuff is obviously just an example. This is where you want
// to layout your HTML to display the queries. This loop will run
// once for every entry to be displayed on the current page.
echo $result[email];
echo "<br>";
}

// You will probably want to modify this stuff too. This displays
// the previous, next, and direct links to each page. It is laid out
// VERY plain below, so you will likely want to change it to fit the
// layour of your site.

// Previous
if ($prev_page) {
echo "<a href=\"$PHP_SELF page=$prev_page\">Prev</a>
}

// Page # direct links
// If you don't want direct links to each page, you should be able to
// safely remove this chunk.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $page) {
echo " <a href=\"$PHP_SELF?page=$i\">$i</a> ";
} else {
echo " $i ";
}
}

// Next
if ($page != $num_pages) {
echo "<a href=\"$PHP_SELF page=$next_page\">Next</a>
}

?>





Persistent connections - General information.
Categories : General SQL, PHP, Databases
Is there some possibility to link a database to an htaccess file, so that instead of having a passwd file you would have a database with DES-crypted password and username fields?
Categories : Authentication, PHP, General SQL, Databases
google like search function with bolded search terms
Categories : PHP, Search, Databases, General SQL
I`d like to use the mysql_fetch_row function along with a "randomizer" function that would give me a random result from a mySQL table.
Categories : General SQL, MySQL, PHP, Databases
You have a set of many resources , and You need to know wich of these resources are available for a given use during a given period but MySql does not allow SUB-Selection :( here is the right way to do that in a single query
Categories : MySQL, General SQL, Databases, Algorithms
Multiple Search using PHP and Mysql
Categories : PHP, Databases, General SQL, MySQL
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
Compare NTEXT fields - a function (tested in SQL Server 2000) that can compare two NTEXT values for equality.
Categories : MS SQL Server, Databases, General SQL
TSQL INDENTER
Categories : MS SQL Server, Debugging, Databases, General SQL
Dynamic WHERE CLAUSE depending on number of FORM FIELDS
Categories : ODBC, General SQL, PHP, Complete Programs, Databases
Safe Authorization SELECT query
Categories : MySQL, Databases, General SQL
This is a class with functions that are taken from simple SQL statements. I made it to have an easier connection between PHP3 and DBase files.
Categories : General SQL, PHP, PHP Classes, Databases
phpYellow Pages Standard
Categories : PHP, Complete Programs, Databases, Directories, Search
How to "group by" only by the date when you are really using a datetime type of field.
Categories : Databases, MySQL, General SQL
Sql Builder
Categories : PHP, HTML and PHP, Databases, General SQL, Form Processing
 brett jungblut wrote : 212
wouldn`t a "SELECT COUNT(column) as num_rows FROM table" be faster & less ram intensive just to get the 
number of rows in the table?

yabel.com - yet another BeOS link
 
 SWiTCH2 wrote : 214
I`m a newbie, and was looking for a script like this.... THANKS!!

Is it possible so that the display of number of pages with the NEXT and PREV
is th same as LIMIT of records? 
(ie) if LIMIT is 10, the total number of page number shown 
should be 10 , 1 2 3 4 5 6 7 8 9 10 NEXT 20 pages ...etc
 
 norhafiz mohamad wrote : 222
This example help me alots to understand limit and offset, also very nice example
its works with me
 
 zhenheng wu wrote : 286
Thank you  for sample!
 
 zhenheng wu wrote : 287
Thank you  !
 
 Roger West wrote : 290
Thanks, this is just what I was looking for. It`s a 
beautiful day out. Now I can go Play! Or mow the 
grass...
 
 Stephen Cameron wrote : 338
Thanks, this is a great example!
With a few small mods it could be perfect :)

Stephen
 
 Jordan Elver wrote : 407
Thank You. This is the only piece of easy to use 
code that I have found. I modified it a bit and it 
worked. Cheers mate. My next/prev links nightmare 
is over!
 
 Marduk de la Rocha wrote : 413
Thanks, but I need to formatting results of any search...
Anybody know to make this???


Contact me: trencito@gmx.net
 
 A. Gianotto wrote : 476
It`s the strangest thing!  I have used this piece of code many times (THANKS!) and now all of a sudden it`s not working.  :-(  

http://www.snipe.net/tech/news/news.php

Any thoughts?
 
 Trevor Garrett wrote : 536
Could someone help me.  I`m having a problem going to the second page of results.  It says the page cannot be found.  the location shows   www.mydomain.com/search.php3%20page=2
 
 Boaz Yahav wrote : 537
Did you call your file "search.php3" ?
 
 army sierra wrote : 552
this code doesn`t work when i used variables in my query (ie. [$query = mysql_query("SELECT * FROM &variabletable WHERE  LNAME=`$lname` and FNAME=`$fname` and POSITION=`$position` ORDER BY LNAME, FNAME");]

would appreciate ur help...

army
army@idess.com
 
 Renato Preti wrote : 700
Great stuff! Thanx by this code
 
 Alexei Selivanov wrote : 794
Using variables with this example (in reply to sierra army`s comment).

Try to send these variables like this:
echo " &lt;a href="$PHP_SELF?page=$i&lname=`$lname&fname=`$fname&position=`$position`\"&gt;$i&lt;/a&gt; ";  
 
 Mark Ellis wrote :1205
Can anyone help me - I am having problems using the following variables:
WHERE $category LIKE `y` AND town LIKE `%$category2%` AND checked LIKE `%1%` ORDER BY name ASC

When clicking on the page links I get an error - how do I send these variables using echo?

Many thanks

Mark