Home arrow PHP arrow Page 2 - PHP and JavaScript Interaction: Storing Data in the Client - Part 2

A brief look at the "createJavaScript()" function - PHP

In the first article in this series, we developed a simple PHP function that dynamically generates a JavaScript array and populates it with external incoming data. In this article, we will develop a real world application using this method, in the form of a news ticker.

TABLE OF CONTENTS:
  1. PHP and JavaScript Interaction: Storing Data in the Client - Part 2
  2. A brief look at the "createJavaScript()" function
  3. Working in the client side: the JavaScript news ticker
  4. Defining the JavaScript functions
  5. A practical approximation: showing the news ticker in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
May 09, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: