| | |||||||
| |||||||
| |||||||
|
|
|
|
|
|
|
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
More Site Navigation Code Articles |
| |
| |