Home arrow PHP arrow Page 4 - PHP and JavaScript Interaction: Storing Data in the Client, part 1

Dealing with result sets - PHP

Modern websites demand heavy interaction between server-side and client-side programming. In the first part of this article series, we will implement a simple mechanism to make PHP and JavaScript interact, creating a function which can build an array structure and store information in it. It will allow for programmatic data manipulation without server interaction.

  1. PHP and JavaScript Interaction: Storing Data in the Client, part 1
  2. Lord of the Arrays
  3. Building JavaScript Arrays with PHP: the "createJavascript()" function
  4. Dealing with result sets
  5. Getting practical: putting the "createJavaScript()" function into action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 55
May 04, 2005

print this article



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
  die('Invalid result set parameter');
  // build JavaScript array from result set
  foreach($row=mysql_fetch_array($dataSource,MYSQL_ASSOC) as $column){
    tempOutput.=$column.' ';

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:

// 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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: