PHP
  Home arrow PHP arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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 / 38
    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


    PHP and JavaScript Interaction: Storing Data in the Client, part 1 - Dealing with result sets


    (Page 4 of 5 )

    By now, we're halfway home. We've already seen how to build a JavaScript array from data files, right? Now, the function should be capable of working with database result sets, at least at a basic level. Fortunately, our function checks to see what type of data source has been provided. If the parameter $dataSource is a result set, then table records are extracted with a regular loop, and stored in the proper JavaScript array, following the same method aplicable in flat data files. The lines that extract table records and create the JavaScript array are listed below:

    // check if we have a valid result set
    if(!$numRows=mysql_num_rows($dataSource)){
      die('Invalid result set parameter');
    }
    for($i=0;$i<$numRows;$i++){
      // build JavaScript array from result set
      $javascript.=$arrayName.'['.$i.']="';
      $tempOutput='';
      foreach($row=mysql_fetch_array($dataSource,MYSQL_ASSOC) as $column){
        tempOutput.=$column.' ';
      }
      $javascript.=trim($tempOutput).'";';
    }

    Luckily, we've stored database records in the dynamic array. Before we move on, a little explanation is in order here. Notice that inside the loop process we've retrieved each table field, separating them with a single blank space. Why did we do it that way? The reason is quite simple: by doing so, we can access each table field value later, by simply splitting the string of data returned by the function.

    However, possible additions are probably valid too. We could have stored field values in a bi-dimensional JavaScript array, for further access. The flip side is that we'd be taking up even more client memory, making the overall storage a crash prone process. So, to keep things simple, we're using the first aproach.

    Still following the function explanation? Good, because we're almost done.

    Once the function has done its thing building the JavaScript array and populating it either with flat data files or database records, it must end up completing the code, adding a </script> closing tag, and returning the output, just like this:

    $javascript.='</script>'."\n";
    // return JavaScript code
    return $javascript;

    As I told you before, we're almost done. Our function takes a file or database result set, generates an array structure with the values obtained, and finally returns the complete JavaScript code, ready to be included in any application for further processing.

    Okay, it's time to test our function. Let's show a practical example to see how it can be used.

    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

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





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