PHP
  Home arrow PHP arrow Page 2 - PHP and JavaScript Interaction: Storing Data in the Client - Part 2
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 Interaction: Storing Data in the Client - Part 2
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2005-05-09


    Table of Contents:
  • PHP and JavaScript Interaction: Storing Data in the Client - Part 2
  • A brief look at the "createJavaScript()" function
  • Working in the client side: the JavaScript news ticker
  • Defining the JavaScript functions
  • A practical approximation: showing the news ticker in action

  • 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 Interaction: Storing Data in the Client - Part 2 - A brief look at the "createJavaScript()" function
    ( Page 2 of 5 )

    Before we get our hands dirty writing some code, let's remind ourselves how our PHP "createJavaScript()" function looked originally, in order to establish a starting point to build the JavaScript ticker. The function source code was defined as following:

    function createJavaScript($dataSource,$arrayName='rows'){

    // validate variable name

    if(!is_string($arrayName)){

    die('Invalid variable name');

    }

    // initialize javascript string

    $javascript='<script>var '.$arrayName.'=[];';

    // check if $dataSource is a file or a result set

    if(is_file($dataSource)){

    // read data from file

    $row=file($dataSource);

    // build javascript array

    for($i=0;$i<count($row);$i++){

    $javascript.=$arrayName.'['.$i.']="'.trim($row[$i]).'";';

    }

    }

    // read data from result set

    else{

    // check if it's 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).'";';

    }

    }

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

    // return javascript code

    return $javascript;

    }

    We're not going to offer in-depth coverage about how the function works, because that was already done in the first part of this series. Instead, we'll cover briefly its functionality, just to give you enough knowledge for quick implementation.

    The function accepts two parameters, $dataSource and $arrayName. The first one means the source from which data are extracted, and directly fills the JavaScript array. Possible valid sources are plain text files or database result sets. The second argument determines the name of the array to be dynamically generated.

    Considering these parameters, we might show an example where we're passing an imaginary "customers.dat" text file, whose contents will populate a "customers" JavaScript array, like this:

    echo createJavaScript('customers.dat','customers');

    That's all we need. Assuming that the customers file has valid information, after executing the function, an array named "customers" will be created for us, available for processing. Simple and effective.

    At this point, we're armed with our PHP function to create arrays in the client side. Now, it's time to build our first client application: the JavaScript news ticker. Do you think that JavaScript won't fit your needs? You might be surprised. It's quite powerful.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    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 6 hosted by Hostway
    Stay green...Green IT