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!
|
|
|
|
As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper. FREE! Go There Now!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Discover how IBM Rational AppScan Standard Edition can help you detext vulnerabilities in your web applications in the Web Application Security eKit. IBM Rational AppScan is a leading suite of automated web application security solutions that scan and test for common Web application vulnerabilities. The new Web Application Security eKit provides you with valuable resources, including white papers, demos, and additional information on the benefits of testing your Web applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
|
|
|
|
Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase. FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
|
|
|
|
This paper is about the critical role that a discipline called integrated requirements management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrating, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way. FREE! Go There Now!
|
|
|
|
As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |