WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 23079
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  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>
}

?>





Dynamic WHERE CLAUSE depending on number of FORM FIELDS
Categories : ODBC, General SQL, PHP, Complete Programs, Databases
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
google like search function with bolded search terms
Categories : PHP, Search, Databases, General SQL
Multiple Search using PHP and Mysql
Categories : PHP, Databases, General SQL, MySQL
phpYellow Pages Standard
Categories : PHP, Complete Programs, Databases, Directories, Search
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
How to group by only by the date when you are really using a datetime type of field.
Categories : Databases, MySQL, General SQL
generic sql insert non-literal values
Categories : General SQL, 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
ADODB Database Wrapper Abstraction Library for PHP: MySQL, MSSQL, Oracle, Interbase,ODBC, Microsoft Access and FoxPro.
Categories : PHP Classes, Databases, PHP, General SQL, ODBC
TSQL INDENTER
Categories : MS SQL Server, Debugging, Databases, General SQL
Safe Authorization SELECT query
Categories : MySQL, Databases, General SQL
This function will populate the options in a drop down HTML select list in a form from a database query.
Categories : MySQL, General SQL, PHP, HTML and PHP, Databases
Logs hits to any page which includes it. Automatically utilises page access information left behind by PHP/FI2.0.
Categories : Databases, PHP, mSQL, Databases
phpEasySQL - Easily connect to your MySQL database with just 1 php file and 3 easy steps!
Categories : Databases, PHP, MySQL, General SQL
 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