Site Navigation Code

  Home arrow Site Navigation Code arrow PHP Search Navigator 1.0
SITE NAVIGATION CODE

PHP Search Navigator 1.0
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2003-03-27

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    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 . '&currentPosition=' . ($currentPosition+$recordsPerPage) . '\'">Next</a>'
    :
    'Next';
    }

    function MakePreviousLink($searchPage, $numOfRecords, $recordsPerPage, $currentPosition) {
    return ($currentPosition > 0) ?
    '<a href="JavaScript:document.location.href=\'' . $searchPage . '?recordsPerPage=' . $recordsPerPage . '&currentPosition=' . ($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 . '&currentPosition=\' + this.value">
    ' . $pageOptions . '
    </select>
    &nbsp;&nbsp;
    <select name="howMany" id="howMany" onChange="document.location.href=\'' . $searchPage . '?recordsPerPage=\' + this.value + \'&currentPosition=' . $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

    blog comments powered by Disqus

    SITE NAVIGATION CODE ARTICLES

    - Simple Menu System
    - Simply image viewer script
    - Simple File Lister
    - Dynamic Error Pages
    - BSoftEditor
    - Yahoo Status
    - Page numbers
    - PHP Search Navigator 1.0
    - Simple Page Navigation
    - An easy page browser ( prev 6 7 8 9 10 next )
    - AutoIndex PHP Script (Directory Indexer)
    - Bs_HtmlNavigation (Navigation and Sitemap cl...
    - Another Paging with Stage
    - Website Navigation via PHP
    - MySQL Paging Class

    Developer Shed Affiliates

     



    © 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap