This will return your MySQL query as an array and a search map made up of previous/next buttons, select sub-page combo-box and a records per page combo box.
By : sir_tripod
<?
/*
Name of Script: Search navigation Version: 1.0 Author: Matthew Lindley E-mail: sir_tripod@hotmail.com
Notes: I've created this simple search navigation script so you can search through your database with using only one SQL query. The form offers a previous/next buttons and combo-boxes to jump to a sub-page as well as set number of records to show per page.
There's a commented example of how to use this script at the bottom.
The best way (I think) to add this to a page is to include() it.
Please send me an e-mail to say you've downloaded it: sir_tripod@hotmail.com That's all. No registration. No costs. No headaches in getting code to work. Just one email.
Also, I cannot and will not be held resposible for any (mis)use of this script. It's tested safe but please test it yourself first before running it properly.
If the script doesn't work, you may need to alter the REGISTER_GLOBALS setting in your php.ini file.
function MakeViewingMessage($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) { switch($numOfRecords) { case 0 : return "No records to view"; break;
case 1 : return ($recordsPerPage > 1) ? "Viewing the only record." : "Viewing record " . ($currentPosition+1) . " of $numOfRecords records."; break;
// Run though query while ($row = mysql_fetch_array($result)) {
// If $i (index) is within spec of current position or end of required number of results... if (($i >= $currentPosition) && ($i <= ($currentPosition+$recordsPerPage))) {
// For each field returned in $result while (list($k, $v) = each($row)) {
// if the key ($k) isn't an integer, add to array with value ($v) if (!is_int($k)) $resultSet[$i][$k] = $v;
} } $i++; }
// Reset the array's keys. $resultSet = array_values($resultSet);
$sql = ' SELECT first_name, last_name, telephone FROM contacts ORDER BY first_name, last_name ';
// Call function with handler. You should only need to change the connection link name, $sql and maybe the search page. $test = SearchNavigation("connection", $sql, $PHP_SELF, $recordsPerPage, $currentPosition);
// handler: show form echo $test['form'];
// loop through records for ($i = 0; $i < count($test['records']); $i++) { echo $test['records'][$i]['first_name'] . ' ' . $test['records'][$i]['last_name'] . ': ' . $test['records'][$i]['telephone'] . '<br>'; }
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.