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.
TTFN d:-)
*/
// Set defaults
$currentPosition = ($_GET['currentPosition']) ? $_GET['currentPosition'] : 0;
$recordsPerPage = ($_GET['recordsPerPage']) ? $_GET['recordsPerPage'] : 10;
// Assisting functions
function MakeNextLink($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) {
return ($currentPosition < ($numOfRecords-$recordsPerPage)) ?
'<a href="JavaScript:document.location.href=\'' . $searchPage . '?recordsPerPage=' . $recordsPerPage . '¤tPosition=' . ($currentPosition+$recordsPerPage) . '\'">Next</a>'
:
'Next';
}
function MakePreviousLink($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) {
return ($currentPosition > 0) ?
'<a href="JavaScript:document.location.href=\'' . $searchPage . '?recordsPerPage=' . $recordsPerPage . '¤tPosition=' . ($currentPosition-$recordsPerPage) . '\'">Previous</a>'
:
'Previous';
}
function MakeSelectOption($text, $value, $selectedValue) {
return "\n\t<option value=\"$value\"" . (($value == $selectedValue) ? " selected" : "") . ">$text</option>";
}
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;
default :
$toRecord = ((($currentPosition+1) + $recordsPerPage) -1);
return "Viewing records " . ($currentPosition+1) . " to " . (($toRecord > $numOfRecords) ? $numOfRecords : $toRecord). " of $numOfRecords records.";
}
}
// Main function
function SearchNavigation($dbConnection, $sql, $searchPage, $recordsPerPage, $currentPosition) {
// Do query
$result = mysql_query($sql, $GLOBALS[$dbConnection]) or die(mysql_error() . '<br><br>' . $sql);
$i = 0;
$numOfRecords = mysql_num_rows($result);
$resultSet = array();
// 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);
// Make page options
$pageID = 0;
$recordCount = $numOfRecords;
while ($recordCount > 0) {
$pageOptions .= MakeSelectOption("Page " . ++$pageID, ($pageID-1)*$recordsPerPage, $currentPosition);
$recordCount -= $recordsPerPage;
}
// Make size options
$sizeOptionsArray = array(5, 10, 25, 50, 100);
for ($i = 0; $i < count($sizeOptionsArray); $i++) {
$sizeOptions .= MakeSelectOption($sizeOptionsArray[$i], $sizeOptionsArray[$i], $recordsPerPage);
}
// Make form
$return['form'] = '
<table align="center">
<form name="searchNavigation" id="searchNavigation">
<tr align="center">
<td>' . MakeViewingMessage($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) . '</td>
</tr>
<tr align="center">
<td>' . MakePreviousLink($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) . '
<select name="whichPage" id="whichPage" onChange="document.location.href=\'' . $searchPage . '?recordsPerPage=' . $recordsPerPage . '¤tPosition=\' + this.value">
' . $pageOptions . '
</select>
<select name="howMany" id="howMany" onChange="document.location.href=\'' . $searchPage . '?recordsPerPage=\' + this.value + \'¤tPosition=' . $currentPosition . '\'">
' . $sizeOptions . '
</select>
' . MakeNextLink($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) . '</td>
</tr>
</form>
</table>
';
$return['records'] = $resultSet;
return $return;
}
/*
##########
EXAMPLE
##########
*/
$connection = mysql_connect("localhost", "username", "password");
$db = mysql_select_db("games", $connection);
$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>';
}
?>
Click to
Download File| 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. |
More Site Navigation Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
<a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference, featuring Paul Boustany and Mark Krasovich, to speak to the experts about becoming a Rational ClearCase power user. Get a chance to ask your questions and learn tips and tricks for using Rational ClearCase in Agile development FREE! Go There Now!
|
|
|
|
Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base. FREE! Go There Now!
|
|
|
|
Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise. FREE! Go There Now!
|
|
|
|
Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
|
|
|
|
This webcast outlines the best practices that must be instituted to gain the maximum benefit from SOA while maintaining high quality of service. Whether you are deploying new applications or managing and monitoring your existing infrastructure, learn how you can ensure high quality of services with SOA based solutions from IBM. All registrants who attend this live Web Seminar will receive complimentary access to a white paper titled “Maintaining QoS in an SOA Environment”. FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |