SunQuest
 
       PHP
  Home arrow PHP arrow Page 2 - PHP and JavaScript Interaction: Storin...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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 Interaction: Storing Data in the Client, part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 37
    2005-05-04

    Table of Contents:
  • PHP and JavaScript Interaction: Storing Data in the Client, part 1
  • Lord of the Arrays
  • Building JavaScript Arrays with PHP: the "createJavascript()" function
  • Dealing with result sets
  • Getting practical: putting the "createJavaScript()" function into action

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    PHP and JavaScript Interaction: Storing Data in the Client, part 1 - Lord of the Arrays


    (Page 2 of 5 )

    Historically, one of the most common methods employed by developers to store data in the client for faster processing, is the use of JavaScript arrays. This aproach has been used for years in diverse aplications, where PHP generates the necessary code to create a JavaScript array structure and stores data either from files or, more infrequently, from database records. Let's consider, for instance, the following example, which shows a rough approximation to populate a JavaScript array with database information:

    <script>
    var rows=[];
    <?php
      $db=mysql_connect('dbhost','username','password') or die('Error connecting to the server');
      mysql_select_db('mydatabase') or die('Error selecting database');
      $result=mysql_query('SELECT * FROM mytable') or die ('Error performing query');
      while($row=mysql_fetch_array($result, MYSQL_ASSOC)){
    ?>
    rows[rows.length]=<?php echo '"'.$row['name'].'"'?>;
    <?php
      }
    ?>
    </script>

    In the above example, we're declaring a JavaScript "rows" array. We then use interspersed, procedural PHP code to connect to the MySQL server, extract some records from a sample table, and finally store the contents of a hypothetical "name" table field in the array. Despite the fact that the aproach is quick and dirty, it shows in a nutshell that storing server data using a PHP - JavaScript interaction is really simple.

    Before I hear the loud and well-intended complaints resounding in my ears, let me say that there are serious drawbacks with this technique. First, we're mixing up PHP code with JavaScript, which may lead to undesirable bad coding habits. The second pitfall is rather obvious: what if our harmless, simple database table contains thousands of records to be displayed? Well, there may be a few upset visitors out there, watching their browsers crash. That's definitely not a very good situation!

    However, the topic is not as unfavorable as it seems. There are cases where we can take advantage of JavaScript arrays to build up a less demanding application, which hopefully will run smoothly in the client's computer, avoiding unnecessary requests to the server. That sounds fairly good, doesn't it?

    But before working directly with arrays, we need to address a key issue. Since we don't want to mess up our code by mixing PHP and JavaScript, we're going to keep them in separate layers, creating a PHP function that will take care of generating the corresponding JavaScript arrays, but maintaining the rest of the code pretty untouched. Also, this introduces an extra benefit. Having a generic function that creates JavaScript code and returns the proper arrays is quite portable for use in applications with this kind of client requirement. The subject is really promising. So, let's start defining the PHP function to generate the JavaScript arrays.

    More PHP Articles
    More By Alejandro Gervasio


       · The article shows a simple and effective way to create and populate a JavaScript...
       · The article is very understanding. I hope the 2nd part is coming soon, because I do...
       · Hi friend,This first part implements a JavaScript array built with server-side...
       · Hi...This article is really very nice. It's also written in simple to understand...
       · Hi Sukhi,Thanks a lot for the compliments about my PHP-JavaScript related...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway