PHP
  Home arrow PHP arrow Page 4 - PHP and JavaScript Interaction: Storing Data in the Client, part 3
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 3
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    2005-05-18


    Table of Contents:
  • PHP and JavaScript Interaction: Storing Data in the Client, part 3
  • Records wanted! Fetching data from tables
  • Defining data containers: the "createDivs()" function
  • Showing database records: the "displayRecords()" function
  • 1 - 2 - 3 Next
  • Building paging links: the sequel
  • Building client-based result sets: making PHP and JavaScript interact

  • 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 3 - Showing database records: the "displayRecords()" function
    ( Page 4 of 7 )

    When it comes to displaying database records, we need to define a specific function to perform that task. Before we list the function code, let's explain how it will behave. Doing so, its logic will be much easier to understand. Take my word for it.

    Since the records are already stored in the "rows" JavaScript array, the only assignment for this function is to extract the corresponding elements from it, and then insert their values into paragraph elements. Next, the whole set will be wrapped up by a regular <div> container and appended to the document tree. It's that simple.

    I know that explaining this in words without showing any code is pretty pointless, so here's the listing for our "displayRecords()" function:

    displayRecords=function(page){
      var recsDiv=document.getElementById('records');
      if(!recsDiv){return;}
      var div=document.createElement('div');
      div.id='records';
      if(!page){page=1;}

      // extract records from rows array
      for(var i=(page-1)*recsPage;i<((page-1)*recsPage)+recsPage;i++){
        if(rows[i]){

          // insert records as paragraph elements
          var p=document.createElement('p');
          p.appendChild(document.createTextNode(rows[i]));
          div.appendChild(p);
        }
      }

      // replace old records node with new records node
      recsDiv.parentNode.replaceChild(div,recsDiv);
    }

    If the above code seems to be a bit messy, don't worry. We'll explain some sections of code.

    As I said previously, the function really extracts the records from the "rows" array, using a record pointer called "page," which is passed as the unique parameter. This pointer is used internally to calculate the position within the array and display the exact number of records. The lines listed below perform that operation:

    for(var i=(page-1)*recsPage;i<((page-1)*recsPage)+recsPage;i++){
      if(rows[i]){

        // insert records as paragraph elements
        var p=document.createElement('p');
        p.appendChild(document.createTextNode(rows[i]));
        div.appendChild(p);
      }
    }

    Notice that we're putting each record inside <p> elements, and wrapping them with a <div> tag. The only thing left is to insert the complete updated structure into the document. To do so, I've opted to replace the existing "records" <div> element with a new one, using the expression:

    recsDiv.parentNode.replaceChild(div,recsDiv);

    Oops! I think that the code was pretty hard to explain, but it's worthwhile. Thus, having defined the functions to contain records and paging links, as well as for displaying paginated data, what's our next step? We need to create the paging links. That's what we'll learn to do in the next section.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT