PHP
  Home arrow PHP arrow Page 5 - 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 - 1 - 2 - 3 Next
    ( Page 5 of 7 )

    In order to create the paging links, required to display paged records, we'll start by defining another useful JavaScript function called "generateLinks," which accepts only one incoming parameter: an array pointer that nicely determines our position inside the "rows" array.

    Using this pointer, the function is capable of verifying whether it's possible to generate the usual <previous> and <next> links, along with the regular numbered links. But, doesn't it sound like we've used this parameter before? Yes! The pointer is the same one that we utilized to display the records in our previous "displayRecords()" function. As we'll see in a moment, the same parameter is used to output both records and paging links. But, I'm getting ahead of myself! Let's get on with the step-by-step explanation process.

    For the sake of not feeling overwhelmed by the apparent complexity of the function, let's divide it into a few more digestible sections. The first one simply retrieves the <div> element for the paging links, creates a new <div> and assigns an "paginglinks" ID attribute to it, as listed below:

    var linksDiv=document.getElementById('paginglinks');
    if(!linksDiv){return;}
    var div=document.createElement('div');
    div.id='paginglinks';

    Why are we doing this? Since we're keeping this on the client side, with no requests to the server to update page contents, this is the basic way we're updating our paging links. We get the document node identified as "paginglinks" where the links are placed, update them accordingly, and finally replace the old node with a new one. Simply and straightforward, huh?

    The second section of the function checks to see whether it's feasible to generate a <previous> link, in a way similar to that used in server-side scripts. So, here's the code to generate our link:

    // create previous link
    if(page>1){
      var a=document.createElement('a');
      a.href='#';
      a.id=parseInt(page)-1;
      a.style.fontFamily='Verdana';
      a.style.fontSize='11px';
      a.style.fontWeight='bold';
      a.appendChild(document.createTextNode('<<Previous'));
      div.appendChild(a);
      a.onclick=function(){

        // update record list
        displayRecords(this.id);

        // update paging links
        generateLinks(this.id);
      }
    }

    Okay, that's not big deal! If you have ever created a server-based paging system, the code is fairly understandable. The function checks to see whether the "page" array pointer is placed at a position other than the first one (page>1). If this is true, then a <previous> link is generated, and receives some regular styles, such as font typefaces, font weight, size and color. Then, the classic <previous> label is inserted inside the link, and finally appended to the "paginglinks" <div> container.

    However, there are a few things to consider here. Notice that we've assigned an ID to the link:

    a.id=parseInt(page)-1;

    The reason for doing this should become clear. Whenever the link is clicked, we need to update the list of records and the paging links themselves. To do so, we first make a recursive call to the "generateLinks()" function, passing this ID to it, and finally calling our known displayRecords() function, with the same ID value, as illustrated below:

    a.onclick=function(){

      // update record list
      displayRecords(this.id);

      // update paging links
      generateLinks(this.id);
    }

    If you're feeling a bit confused, don't worry. It really works!

    Next, we'll focus our attention on the section aimed at generating the numbered links.



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