PHP
  Home arrow PHP arrow Page 3 - PHP and JavaScript, Pooling Your Resou...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 19
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · The app works great, except that the hint box doesn't update dynamically in IE6, but...
       · Glad to hear it :)
       · I have a PHP page, which references a 'javascript/PHP' file. (The file uses PHP to...
       · This scripts work with Opera8.5Very nice. It will help me a lot.
       · you should be able to include the file either way, but I normally use the "<script...
       · thank you :) i'm glad to hear it!
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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