Source Code: ajaxdatagrid
//Datagrid Calling page
<script type="text/javascript">
//call back Javascript function
function returnPageData_Callback(str) {
//get the element you want to bind the datagrid to
var htmlOutput = document.getElementById("ajaxgrid1");
htmlOutput.style.visibility = "visible";
//bind the ajax response
htmlOutput.innerHTML = str;
}
function returnPageData(offset,orderby,dir,perpage) {
//invoke the showDataGrid server fiunction
agent.call('ajaxFunctions.php','showDataGrid','returnPageData_Callback',offset,orderby,dir,perpage);
}
</script>
//element the ajax response will be bound to
<div id='ajaxgrid1'></div>
///Ajax Data Code
<?php
//main function that does all of the work
//$setoffset - start at DataRow x
//$setorderby - order the data by column name
//$setdir - order the dat by ASC or DESC
function showDataGrid($setoffset,$setorderby,$setdir){
//create an array of columns
//columnName is the SQL column name
//columnDisplay is the friendly column display name
$columns[0] = array("columnID"=>"0","columnName"=>"ProjName","columnDisplay"=>"Name");
$columns[1] = array("columnID"=>"1","columnName"=>"ProjDesc","columnDisplay"=>"Description");
//set some defaults
$offset = 0;
$per_page = 5;
$column = $columns[0];
$orderby = $column['columnName'];
$dir = 'ASC';
if ($setoffset > 0) {
$offset = $setoffset;
}
if ($setorderby != '') {
$orderby = $setorderby;
}
if ($setdir != '') {
$dir = $setdir;
}
//get some data
$projects = getProjects($offset,$per_page,$orderby,$dir);
//display the data navigation
$html .= pc_indexed_links(count(getProjects()),$offset,$per_page,$orderby,$dir);
//display the datagrid
$html .= phpDataGrid($projects,$columns,$offset,$per_page);
//echo the results
echo $html;
}
//get projects
function getProjects($offset='',$per_page='',$orderby='',$dir='') {
$mysql = new PDO('mysql:host=[HOST];dbname=[DATABASE]','[USERNAME]','[PASSWORD]');
if($orderby != ''){
$strSQLExtra .=" ORDER BY ".$orderby." ".$dir;
}
if($offset > 0) {
$strSQLExtra .= " LIMIT ".$per_page." OFFSET ".($offset-1);
}else if($per_page > 0){
$strSQLExtra .= " LIMIT ".$per_page;
}else{
$strSQLExtra = '';
}
$strSQL = "SELECT * FROM tblProjects ".$strSQLExtra;
//echo $strSQL;
$projects = array();
foreach( $mysql->query( $strSQL ) as $row ){
$projects[$row['ProjID']] = $row;
}
$mysql = null;
return $projects;
}
function pc_print_link($inactive,$text,$offset='',$urlParams=null){
$html = '';
if($inactive) {
$html .= "<span class='inactive'>".$text."</span>\n";
} else {
$html .= "<span class='active'>\n";
$html .= "<a href='#' OnClick=returnPageData(".$offset.",'".$urlParams['orderby']."','".$urlParams['dir']."',".$urlParams['perpage'].");return false'>$text</a></span>\n";
}
return $html;
}
function pc_indexed_links($total,$offset,$per_page,$orderby,$dir) {
$separator = ' | ';
$html = '';
$urlParams = array("orderby"=>$orderby,"dir"=>$dir,"perpage"=>$per_page);
// print "<<Prev" link
$html .= pc_print_link($offset == 1, '<< Prev', $offset - $per_page,$urlParams);
// print all groupings except last one
for ($start = 1, $end = $per_page;
$end < $total;
$start += $per_page, $end += $per_page) {
$html .= $separator;
$html .= pc_print_link($offset == $start, "$start-$end", $start,$urlParams);
}
$end = ($total > $start) ? "-$total" : '';
$html .= $separator;
$html .=pc_print_link($offset == $start, "$start$end", $start,$urlParams);
// print "Next>>" link
$html .= $separator;
$html .=pc_print_link($offset == $start, 'Next >>',$offset + $per_page,$urlParams);
return $html;
}
function phpDataGrid($dataobjects,$columns,$offset,$per_page){
if($dataobjects == null){
return "No Data";
}
$html .= "<table border='0' width='90%'>";
$html .= "<tr bgcolor = '#EFEFEF'>";
for($i=0;$i<count($columns);$i++){
$column = $columns[$i];
$html .= "<td width='30%'><strong>";
$html .= $column['columnDisplay'];
$html .= " <a href='#' OnClick=returnPageData(".$offset.",'".$column['columnName']."','ASC',".$per_page.");return false'>+</a>";
$html .=" / ";
$html .= "<a href='#' OnClick=returnPageData(".$offset.",'".$column['columnName']."','DESC',".$per_page.");return false'>-</a> ";
$html .= "</strong></td>";
}
$html .= "</tr>";
$count = 0;
foreach($dataobjects as $object){
if($count % 2) {
$bgcolor = '#EFEFEF';
} else {
$bgcolor = '#ffffff';
}
$html .= "<tr valign=top bgcolor=".$bgcolor.">";
for($i=0;$i<count($columns);$i++){
$column = $columns[$i];
$html .= "<td>";
$html .= stripslashes($object[$column['columnName']]);
$html .= "</td>";
}
$html .= "</tr>";
$count++;
}
$html .= "</table>";
return $html;
}
include_once("ajaxagent/agent.php");
$agent->init();
?>
//SQL SOURCE
CREATE TABLE `tblProjects` (
`ProjID` int(11) NOT NULL auto_increment,
`ProjName` varchar(100) NOT NULL default '',
`ProjDesc` text,
`DateUpdated` datetime default NULL,
PRIMARY KEY (`ProjID`)
) TYPE=InnoDB AUTO_INCREMENT=13;
INSERT INTO `tblProjects` VALUES (1, 'Project One',Project One Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (2, 'Project Two',Project Two Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (3, 'Project Three',Project Three Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (4, 'Project Four',Project Four Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (5, 'Project Five',Project Five Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (6, 'Project Six',Project Six Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (7, 'Project Seven',Project Seven Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (8, 'Project Eight',Project Eight Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (9, 'Project Nine',Project Nine Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (10, 'Project Ten',Project Ten Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (11, 'Project Eleven',Project Eleven Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (12, 'Project Twelve',Project Twelve Description', '2006-11-10 10:04:36');
INSERT INTO `tblProjects` VALUES (13, 'Project Thriteen',Project Thriteen Description', '2006-11-10 10:04:36');





