AJAX - Grid: Creating Server Side Task


AJAX Grid Step By Step Tutorial 2: After create table, we write code for server side.

For this, we need several file:

  1. config.php: contains database connection and other setting.
  2. books.php: interface that client will be connected. it will generate xml document.
  3. books.class.php: class for retrieve data and generate xml data.
  4. error_handler.php: handle any errors.

Place all file to same folder such as www/test/ajax/books.

Now, we create config.php first. Create a file named "config.php" and enter:

<?php
define('DB_HOST', 		'localhost');
define('DB_USER', 		'root');
define('DB_PASSWORD', 	'admin');
define('DB_DATABASE',	'test');
define('ROWS_PER_VIEW',  3);
?>

Then, bellow for error_handler.php:

<?php
set_error_handler('error_handler', E_ALL);

function error_handler($errNo, $errStr, $errFile, $errLine)
{
  ob_clean();
  $error_message = 'ERRNO: '	. $errNo . chr(10).
                   'TEXT: '		. $errStr . chr(10).
				   'LOCATION: '	. $errFile. chr(10).
				   'LINE: '		. $errLine
                   ;
  echo $error_message;
  exit;
}
?>

Next, for "books.class.php":

<?
include "config.php";

class books{
  public $totPages;
  public $totItems;
  public $getPage;
  private $conn;
  private $grid;
  
  function __construct(){
  
    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	mysql_select_db(DB_DATABASE);
	
	$this->totItems = $this->countAll();
  }
  
  function __destruct(){
  
    mysql_close();
	
  }
  
  private function countAll()
  {
  
    if(!isset($_SESSION['record_count']))
	{
	  $sql  = 'SELECT COUNT(1) FROM books';
	  
	  if($result = mysql_query($sql))
	  {
	    $row = mysql_fetch_row($result);
		$_SESSION['record_count'] = $row[0];
	  }
	}
	return $_SESSION['record_count'];
  }  
  
  
  private function pageQuery($query, $pageNo)
  {

    if($this->totItems <= ROWS_PER_VIEW)
	{
	  $this->totPages = 1;
	}
	else
	{
  	  
	  $this->totPages = ceil($this->totItems / ROWS_PER_VIEW);
	  $start_page = ($pageNo - 1) * ROWS_PER_VIEW;
	  $query .= ' LIMIT ' . $start_page . ',' . ROWS_PER_VIEW;
	}

	$this->getPage = $pageNo;
	return $query;
  }  
  
  public function readPage($page)
  {
    $queryString = $this->pageQuery('SELECT * FROM books', $page);

	if($result = mysql_query($queryString))
	{
	  while($row = mysql_fetch_assoc($result))
	  {
	    $this->grid .= '<row>';
		foreach($row as $name=>$val)
		{
		  $this->grid .= '<' . $name . '>' .
		                 htmlentities($val) .
						 '</' . $name . '>';
		}
		$this->grid .= '</row>';		
	  }
	}
  }    
  
  public function getParamsXML()
  {
    $previous_page 	= ($this->getPage == 1) ? '' : $this->getPage-1;
	$next_page 		= ($this->totPages == $this->getPage) ? '' : $this->getPage+1;
	return '<params>' .
	       '<getPage>'. $this->getPage . '</getPage>'.
		   '<totPages>'. $this->totPages . '</totPages>'.
		   '<totItems>'. $this->totItems . '</totItems>'.
		   '<previous_page>'.$previous_page . '</previous_page>'.
		   '<next_page>'. $next_page . '</next_page>'.
	       '</params>';
  }
  
  public function getGridXML()
  {
    return '<grid>' . $this->grid . '</grid>';
  }    
  
}

?>

Last, create books.php:

<?php
require_once("error_handler.php");
require_once("books.class.php");

$action = 'SHOW_PAGE'; 
$page 	= ($_GET['page']) ? $_GET['page'] : 1;

$books = new Books;
$books->readPage($page);


if(ob_get_length()) ob_clean();

header('Expires: Fri, 25 Dec 1980 00:00:00 GMT');
header('Last-Modified: '. gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: text/xml');
header('Content-type: text/xml');

echo '<?xml version="1.0" encoding="ISO-8859-1"?>';
echo '<data>';
echo '<action>'. $action .'</action>';
echo $books->getParamsXML();
echo $books->getGridXML();
echo '</data>';

?>


Series this article:
AJAX - Grid: Prepare Table Grid
AJAX - Grid: Creating Server Side Task
AJAX - Grid: Testing Server Side Task
AJAX - Grid: Creating XSL for Client Side
AJAX - Grid: Creating JavaScript for Client Side
AJAX - Grid: Creating Page for Final Display

Bookmark and Share Tag: ajax, grid, server side Category: PHP Application Post : March 27th 2008 Read: 23,173

advertisements


blog comments powered by Disqus