PHP
  Home arrow PHP arrow Page 3 - PHP and JavaScript, Pooling Your Resources (continued)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP and JavaScript, Pooling Your Resources (continued)
By: Brian Vaughn
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2005-09-27


    Table of Contents:
  • PHP and JavaScript, Pooling Your Resources (continued)
  • Separating the Logic
  • What Does PHP Do?
  • Main Layout
  • Is it PHP? Or Javascript?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP and JavaScript, Pooling Your Resources (continued) - What Does PHP Do?
    ( Page 3 of 5 )

    The reasons for using HTML in our project are obvious, as are the reasons for using JavaScript (hopefully) – but why do we need PHP? PHP provides us with an enormous range of programming options, one of which is an easy mechanism through which to query a MySQL database and receive a result set. This is primarily what we will be using our helper function to do. Let’s take a look now at the code:

    <?php
          function get_parent_menu() {
                $select_menu      = NULL;
                # establish connection to the MySQL server
                mysql_connect( 'localhost', 'root', '' );
                mysql_select_db( 'devshed' );           

                # append empty option (to be default)
                $select_menu      .= "\t" . "<OPTION
    onMouseOver=\"show_hint( 'Please select an option from the list
    below.' );\" onMouseOut=\"set_timeout();\" value=\"\" SELECTED>-
    - select - -</OPTION>" . "\r\n";     

                # retrieve a list of all parent values
                $db_query   = 'SELECT * FROM php_js_parent_menu;';
                $db_result  = mysql_query( $db_query );         

                # output a SELECT menu option for each row returned
                if ( $db_result ) {
                      while ( $l_db_row = mysql_fetch_assoc( $db_result ) ) {                 

                            # retrieve and format (if necessary) SQL
    results
                            $l_description    = $l_db_row
    ['description'];
                            $l_id       = stripslashes( $l_db_row
    ['id'] );
                            $l_name           = stripslashes
    ( $l_db_row['name'] );                 

                            # output SELECT menu with JS attributes
                            $select_menu      .= "\t" . "<OPTION
    onMouseOver=\"show_hint( '$l_description' );\"
    onMouseOut=\"set_timeout();\" value=\"$l_id\">$l_name</OPTION>" .
    "\r\n";
               

                      } # END while rows
                } # END if rows           

                # return SELECT menu
                return $select_menu;           

          } # END get_parent_menu()
    ?>


    As you can see above, the ‘get_parent_menu’ function does two basic things. First, it queries the SQL database for all values stored in our ‘parent_menu’ table. It then takes those values, and outputs them in the form of <OPTION> tags for our ‘parent’ SELECT menu.

    The only thing that may need additional explanation here is the use of the onMouseOver and onMouseOut events. The application we are developing is pretty self-explanatory to an end-user, but it never hurts to make our systems a little easier to use and understand to someone who may not be adept with computers. Because of that, we have chosen to implement a “hint box” to display helpful tips to the user in real-time. When the user hovers over a menu option, a brief explanation of that option will be displayed in our hint box. When the user moves on a timeout is set to erase the hint after a couple of seconds. This is all done through the use of the JavaScript functions we discussed in the previous section.

    We’re now ready to move on to our application’s stylesheet.

    How’s it Looking?

    Our stylesheet, “stylesheet.css”, is very simple. One class is used to control the appearance and formatting of our SELECT menus, and another is used to control the previously mentioned “hint box”. Those classes are as follows:

    select {
          width: 300px;
          font-family: Tahoma, Arial, Helvetica, sans-serif;
          font-size: 11px;
    }
    #hint_box {
          width: 290px;
          background-color: #DDDDDD;
          border: 1px solid #000000;
          padding: 5px;
          font-family: Tahoma, Arial, Helvetica, sans-serif;
          font-size: 11px;
    }

    There is nothing too complex here, so let’s move on now to our index file.




     
     
    >>> More PHP Articles          >>> More By Brian Vaughn
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT