|
|
|
Advanced ADODB Pager
10
This is a rewrite of a basic ADODB / PHP Database pager. This pager has some very slick search, sort, and display configuration options. Modifications and improvements are very welcome. Thus I am sharing this code.
A basic example for using this pager would be as follows:
// Check class code for CSS styles used
require_once ('class.init.php');
// SQL Statement
$sql = 'SELECT table1.field1, table1.field2, table1.field3 FROM table1';
// Field Labels
$label_arr = array (
'field1' => 'Field 1 Heading',
'field2' => 'Field 2 Heading',
'field3' => 'Field 3 Heading'
);
// Hidden Fields
$vsel_arr = array (
'field3' => 0 // don't display field3 unless requested by the user
);
$sort_arr = array (
'field1' => 'asc' // sort by field1 ascending order (asc) or (desc)
);
// Config array
$config = array (
'db' => &$dbconn, // open ADODB connection
'sql' => $sql, // String of sql
'id' => 'table1_pager', // pager id for session variables
'label_arr' => $label_arr, // field heading array
'vsel_arr' => $vsel_arr, // fields to hide
'extra_param' => '', // extra paramaters to pass with edit, delete, add anchors
'form_width' => '100%', // table with
'form_title' => 'Table 1', // table title
'edit_anchor' => 'Edit', // link to edit
'del_anchor' => 'Del', // link to delete
'add_anchor' => 'Add', // link to add
'print_anchor' => 'Print', // link to print
'header_view' => true, // no function at this time
'search_view' => true, // no function at this time
'pk' => array ('field1' => true), // primary key field
'sort_arr' => $sort_arr, // sort field array
);
// create object then render it ...
$_pager = new pager($config); // create instance
echo $_pager->render(25); // render output
A basic example for using this pager would be as follows:
// Check class code for CSS styles used
require_once ('class.init.php');
// SQL Statement
$sql = 'SELECT table1.field1, table1.field2, table1.field3 FROM table1';
// Field Labels
$label_arr = array (
'field1' => 'Field 1 Heading',
'field2' => 'Field 2 Heading',
'field3' => 'Field 3 Heading'
);
// Hidden Fields
$vsel_arr = array (
'field3' => 0 // don't display field3 unless requested by the user
);
$sort_arr = array (
'field1' => 'asc' // sort by field1 ascending order (asc) or (desc)
);
// Config array
$config = array (
'db' => &$dbconn, // open ADODB connection
'sql' => $sql, // String of sql
'id' => 'table1_pager', // pager id for session variables
'label_arr' => $label_arr, // field heading array
'vsel_arr' => $vsel_arr, // fields to hide
'extra_param' => '', // extra paramaters to pass with edit, delete, add anchors
'form_width' => '100%', // table with
'form_title' => 'Table 1', // table title
'edit_anchor' => 'Edit', // link to edit
'del_anchor' => 'Del', // link to delete
'add_anchor' => 'Add', // link to add
'print_anchor' => 'Print', // link to print
'header_view' => true, // no function at this time
'search_view' => true, // no function at this time
'pk' => array ('field1' => true), // primary key field
'sort_arr' => $sort_arr, // sort field array
);
// create object then render it ...
$_pager = new pager($config); // create instance
echo $_pager->render(25); // render output
**** Pager INIT file starts HERE copy and save as class.init.php
406) {
$session_vars = '_SESSION';
$post_vars = '_POST';
$get_vars = '_GET';
$server_vars = '_SERVER';
$cookie_vars = '_COOKIE';
$files_vars = '_FILES';
$env_vars = '_ENV';
} else {
$session_vars = 'HTTP_SESSION_VARS';
$post_vars = 'HTTP_POST_VARS';
$get_vars = 'HTTP_GET_VARS';
$server_vars = 'HTTP_SERVER_VARS';
$cookie_vars = 'HTTP_COOKIE_VARS';
$files_vars = 'HTTP_POST_FILES';
$env_vars = 'HTTP_ENV_VARS';
}
foreach (${$GLOBALS['server_vars']} as $index=>$value) {
$GLOBALS[$index] = $value;
}
/* recursively strip slashes from the values of an array */
function RemoveSlashes(&$array) {
if(count($array) > 0) {
foreach ($array as $index=>$value) {
if (is_array($array[$index])) {
RemoveSlashes($array[$index]);
}
else {
$array[$index] = stripslashes($value);
}
}
}
}
// Remove slashes if PHP added them
if (get_magic_quotes_gpc()) {
global $REQUEST_METHOD;
if ($REQUEST_METHOD == "POST") {
global ${$GLOBALS['post_vars']};
RemoveSlashes(${$GLOBALS['post_vars']});
} else if ($REQUEST_METHOD == "GET") {
global ${$GLOBALS['get_vars']};
RemoveSlashes(${$GLOBALS['get_vars']});
}
}
// set include_path
// $include_path = ini_get('include_path');
// $include_path .= ereg_replace(':', $include_separator, ':.:adodb:class');
// ini_set('include_path', $include_path);
// error reporting
// error_reporting($error_report);
// include 'config.php';
// include 'adodb.inc.php';
// include 'class.template.inc.php';
// include 'class.block.inc.php';
include 'class.pager.inc.php';
//include 'class.xls.BIFFwriter.inc.php';
//include 'class.xls.Worksheet.inc.php';
//include 'class.xls.Format.inc.php';
//include 'class.xls.OLEwriter.inc.php';
//include 'class.xls.Parser.inc.php';
//include 'class.xls.Workbook.inc.php';
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
function parsedate($str) {
if (ereg('^'.$GLOBALS['date_ereg'].'$', $str, $regs)) {
eval('$timestamp = '.$GLOBALS['date_eval'].';');
}
return $timestamp;
}
?>
**** end of init
**** Pager Class starts HERE copy and save as class.pager.inc.php
sql = $sql;
$this->id = $id;
$this->db = $db;
$this->label_arr = $label_arr;
$this->optional_arr = $optional_arr;
$this->var_arr = $var_arr;
$this->eval_arr = $eval_arr;
$this->header_view = $header_view;
$this->search_view = $search_view;
$this->excel_save = ${$GLOBALS['get_vars']}[$xls_str] ? TRUE : FALSE;
$this->csv_save = $this->excel_save ? FALSE : (${$GLOBALS['get_vars']}[$csv_str] ? TRUE : FALSE);
if ($this->excel_save || $this->csv_save) {
$this->label_arr['add_anchor'] = FALSE;
$this->label_arr['edit_anchor'] = FALSE;
$this->label_arr['del_anchor'] = FALSE;
$this->label_arr['print_anchor'] = FALSE;
$this->header_view = FALSE;
$this->search_view = FALSE;
}
${$GLOBALS['session_vars']}[$key_str] = IsSet(${$GLOBALS['post_vars']}[$key_str]) ? ${$GLOBALS['post_vars']}[$key_str] :
( ${$GLOBALS['get_vars']}[$key_str] ? ${$GLOBALS['get_vars']}[$key_str] :
( ${$GLOBALS['session_vars']}[$key_str] ? ${$GLOBALS['session_vars']}[$key_str] : '' ) );
${$GLOBALS['session_vars']}[$fdat_str] = ${$GLOBALS['post_vars']}[$fdat_str] ? ${$GLOBALS['post_vars']}[$fdat_str] :
( ${$GLOBALS['get_vars']}[$fdat_str] ? ${$GLOBALS['get_vars']}[$fdat_str] :
( ${$GLOBALS['session_vars']}[$fdat_str] ? ${$GLOBALS['session_vars']}[$fdat_str] : '' ) );
${$GLOBALS['session_vars']}[$tdat_str] = ${$GLOBALS['post_vars']}[$tdat_str] ? ${$GLOBALS['post_vars']}[$tdat_str] :
( ${$GLOBALS['get_vars']}[$tdat_str] ? ${$GLOBALS['get_vars']}[$tdat_str] :
( ${$GLOBALS['session_vars']}[$tdat_str] ? ${$GLOBALS['session_vars']}[$tdat_str] : '' ) );
//exit;
$force_clean = ${$GLOBALS['get_vars']}['force_clean'] ? ${$GLOBALS['get_vars']}['force_clean'] : 0;
//if ($force_clean || ${$GLOBALS['session_vars']}[$sql_str]!=$sql || ${$GLOBALS['session_vars']}[$key_str]=='listall') {
if ($force_clean || ${$GLOBALS['session_vars']}[$key_str]=='listall') {
${$GLOBALS['session_vars']}[$curr_page] = '';
${$GLOBALS['session_vars']}[$orderby] = '';
${$GLOBALS['session_vars']}[$orderby_type] = '';
${$GLOBALS['session_vars']}[$key_str] = '';
${$GLOBALS['session_vars']}[$fdat_str] = '';
${$GLOBALS['session_vars']}[$tdat_str] = '';
${$GLOBALS['session_vars']}[$fsel_str] = '';
${$GLOBALS['session_vars']}[$vsel_str] = '';
${$GLOBALS['session_vars']}[$dsel_str] = '';
${$GLOBALS['session_vars']}[$ppag_str] = '';
${$GLOBALS['session_vars']}[$diso_str] = 1;
${$GLOBALS['session_vars']}[$orderby_arr] = $initial_sort; // Added by PC
${$GLOBALS['session_vars']}[$curr_where_str] = ''; // Added by PC
/* Reset Advanced Search Fields */
${$GLOBALS['session_vars']}[$advs_field] = array(); // Added by PC
${$GLOBALS['session_vars']}[$advs_op] = array(); // Added by PC
${$GLOBALS['session_vars']}[$advs_val] = array(); // Added by PC
${$GLOBALS['session_vars']}[$advs_andor] = array(); // Added by PC
}
${$GLOBALS['session_vars']}[$curr_page] = ${$GLOBALS['post_vars']}[$next_page] ? ${$GLOBALS['post_vars']}[$next_page] :
( ${$GLOBALS['get_vars']}[$next_page] ? ${$GLOBALS['get_vars']}[$next_page] :
( ${$GLOBALS['session_vars']}[$curr_page] ? ${$GLOBALS['session_vars']}[$curr_page] : 1 ) );
${$GLOBALS['session_vars']}[$orderby] = ${$GLOBALS['get_vars']}[$orderby] ? ${$GLOBALS['get_vars']}[$orderby] :
( ${$GLOBALS['session_vars']}[$orderby] ? ${$GLOBALS['session_vars']}[$orderby] : '' );
${$GLOBALS['session_vars']}[$orderby_type] = ${$GLOBALS['get_vars']}[$orderby_type] ? ${$GLOBALS['get_vars']}[$orderby_type] :
( ${$GLOBALS['session_vars']}[$orderby_type] ? ${$GLOBALS['session_vars']}[$orderby_type] : 'ASC' );
${$GLOBALS['session_vars']}[$sql_str] = $sql;
${$GLOBALS['session_vars']}[$fsel_str] = ${$GLOBALS['post_vars']}[$fsel_str] ? ${$GLOBALS['post_vars']}[$fsel_str] :
( ${$GLOBALS['get_vars']}[$fsel_str] ? ${$GLOBALS['get_vars']}[$fsel_str] :
( ${$GLOBALS['session_vars']}[$fsel_str] ? ${$GLOBALS['session_vars']}[$fsel_str] : array() ));
${$GLOBALS['session_vars']}[$dsel_str] = (${$GLOBALS['post_vars']}[$dsel_str]!="") ? ${$GLOBALS['post_vars']}[$dsel_str] :
( ${$GLOBALS['get_vars']}[$dsel_str] ? ${$GLOBALS['get_vars']}[$dsel_str] :
( ${$GLOBALS['session_vars']}[$dsel_str] ? ${$GLOBALS['session_vars']}[$dsel_str] : array() ));
${$GLOBALS['session_vars']}[$diso_str] = isset(${$GLOBALS['get_vars']}[$diso_str]) ? ${$GLOBALS['get_vars']}[$diso_str] :
( isset(${$GLOBALS['session_vars']}[$diso_str]) ? ${$GLOBALS['session_vars']}[$diso_str] : 1 );
$this->disorder = ${$GLOBALS['session_vars']}[$diso_str];
$this->curr_page = ${$GLOBALS['session_vars']}[$curr_page];
/* Setup search flag */
$this->do_search = IsSet(${$GLOBALS['post_vars']}['search_mode']) ? ${$GLOBALS['post_vars']}['search_mode'] : '';
if (${$GLOBALS['post_vars']}[$addv_str]) {
$max_vsel = 0;
foreach (${$GLOBALS['session_vars']}[$this->id.'_vselect'] as $key => $value) {
if ($value > $max_vsel) {
$max_vsel = $value;
}
}
for ($i=0;$idisorder) {
$prev_key = '';
foreach (${$GLOBALS['session_vars']}[$this->id.'_vselect'] as $key => $value) {
if ($key == ${$GLOBALS['get_vars']}[$left_str]) {
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key]--;
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$prev_key]++;
}
$prev_key = $key;
}
} else if (${$GLOBALS['get_vars']}[$righ_str] && ! $this->disorder) {
$prev_key = '';
foreach (${$GLOBALS['session_vars']}[$this->id.'_vselect'] as $key => $value) {
if ($prev_key == ${$GLOBALS['get_vars']}[$righ_str]) {
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key]--;
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$prev_key]++;
}
$prev_key = $key;
}
}
/*if (${$GLOBALS['session_vars']}[$key_str]=='listall') {
${$GLOBALS['session_vars']}[$key_str] = '';
${$GLOBALS['session_vars']}[$fsel_str] = '';
}*/
// PC Routine to build sort array
if (${$GLOBALS['session_vars']}[$order_set] != 1) {
${$GLOBALS['session_vars']}[$order_set] = 1;
${$GLOBALS['session_vars']}[$orderby_arr] = $initial_sort;
}
if (${$GLOBALS['get_vars']}[$orderby]) { // user clicked orderby
// Check for field in array
$obkey = ${$GLOBALS['get_vars']}[$orderby];
${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'][$obkey] = ${$GLOBALS['get_vars']}[$orderby_type];
if (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'][$obkey] == 'none')
unset (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'][$obkey]);
}
// print the array for debug
//print_r (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr']);
// Build sort string
$orderby_sql = '';
foreach (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'] as $key => $value)
$orderby_sql .= ($orderby_sql == '') ? ' ORDER BY '.$key.' '.$value : ', '.$key.' '.$value;
// End of PC Routine
// $orderby_sql = ${$GLOBALS['session_vars']}[$orderby] ? ' ORDER BY ' . ${$GLOBALS['session_vars']}[$orderby] . ' ' .
// ${$GLOBALS['session_vars']}[$orderby_type] : '';
$this->orderby_sql = $orderby_sql;
}
function render_first($anchor=true) {
global $PHP_SELF;
$next_page = 1;
if ($anchor) {
$ret = <<< EOT
id}_next_page={$next_page}&{$this->label_arr['extra_param']} title="First Page">{$this->first}
EOT;
} else {
$ret = $this->first;
}
return $ret;
}
function render_next($anchor=true) {
global $PHP_SELF;
$next_page = $this->rs->AbsolutePage()+1;
if ($anchor) {
$ret = <<< EOT
id}_next_page={$next_page}&{$this->label_arr['extra_param']} title="Next Page">{$this->next}
EOT;
} else {
$ret = $this->next;
}
return $ret;
}
function render_last($anchor=true) {
global $PHP_SELF;
if (!$this->db->pageExecuteCountRows) {
return '';
}
$next_page = $this->rs->LastPageNo();
if ($anchor) {
$ret = <<< EOT
id}_next_page={$next_page}&{$this->label_arr['extra_param']} title="Last Page">{$this->last}
EOT;
} else {
$ret = $this->last;
}
return $ret;
}
function render_prev($anchor=true) {
global $PHP_SELF;
$next_page = $this->rs->AbsolutePage()-1;
if ($anchor) {
$ret = <<< EOT
id}_next_page={$next_page}&{$this->label_arr['extra_param']} title="Previous Page">{$this->prev}
EOT;
} else {
$ret = $this->prev;
}
return $ret;
}
function render_grid()
{
global $PHP_SELF, ${$GLOBALS['session_vars']};
$rs = $this->rs;
$label_arr = $this->label_arr;
$optional_arr = $this->optional_arr;
$eval_arr = $this->eval_arr;
// $str = '';
$str = '';
$str .= 'No'.' ';
$num_col = $rs->FieldCount();
$num_col++; //for field no.
$label_arr['edit_anchor'] ? $num_col++ : '';
$label_arr['del_anchor'] ? $num_col++ : '';
$max_vsel = 0;
for ($i=0;$ifield_arr);$i++) {
$key = $this->field_arr[$i];
if ($optional_arr[$key])
continue;
if (! isset(${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key])) {
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key] = ++$max_vsel;
}
}
$max_vsel = 0;
if (! $this->disorder) {
if (is_array(${$GLOBALS['session_vars']}[$this->id.'_vselect']) ) {
asort(${$GLOBALS['session_vars']}[$this->id.'_vselect']);
foreach (${$GLOBALS['session_vars']}[$this->id.'_vselect'] as $key => $value) {
if ($value > $max_vsel) {
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key] = ++$max_vsel;
}
}
}
$i=0;
foreach (${$GLOBALS['session_vars']}[$this->id.'_vselect'] as $key => $value) {
$field_arr[$i++] = $key;
}
} else {
$field_arr = $this->field_arr;
for ($i=0;$ifield_arr);$i++) {
$key = $this->field_arr[$i];
if ($optional_arr[$key])
continue;
if (${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key]) {
${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key] = ++$max_vsel;
}
}
}
$skey = 0;
for ($i=0;$iid.'_vselect'][$key])) {
// ${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key] = ++$max_vsel;
//}
if (! ${$GLOBALS['session_vars']}[$this->id.'_vselect'][$key])
continue;
$skey++;
$ordimg = "";
// This section replaced by PC multi column sort
// if (${$GLOBALS['session_vars']}[$this->id.'_orderby']==$key) {
// if (${$GLOBALS['session_vars']}[$this->id.'_orderby_type']=='asc') $ordimg = '↑';
// else if (${$GLOBALS['session_vars']}[$this->id.'_orderby_type']=='desc') $ordimg = '↓';
// }
if (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'][$key]) {
if (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'][$key] == 'asc') {
$ordimg = '↑';
$ordtype = 'desc';
} elseif (${$GLOBALS['session_vars']}[$this->id.'_orderby_arr'][$key] == 'desc') {
$ordimg = '↓';
$ordtype = 'none';
}
} else {
$ordtype = 'asc';
}
// $ordtype = (${$GLOBALS['session_vars']}[$this->id.'_orderby']==$key) ? ((strtolower(${$GLOBALS['session_vars']}[$this->id.'_orderby_type'])=='asc') ? 'desc' : 'asc') : 'asc';
if (! $this->disorder && $skey>1) { $pkey_str = 'id.'_left='.$key."&{$this->label_arr['extra_param']} title=\"Move View to Left\">".'<-'.""; }
if (! $this->disorder && $skey<$max_vsel) { $nkey_str = 'id.'_right='.$key."&{$this->label_arr['extra_param']} title=\"Move View to Right\">".'->'.""; }
$str .= "";
$str .= <<< EOT
{$pkey_str} id}_orderby={$key}&{$this->id}_orderby_type={$ordtype}&{$this->label_arr['extra_param']} title="Sort by {$fldname}">{$fldname} {$ordimg} {$nkey_str}
EOT;
// $str .= '|';
$str .= ' ';
}
// $form_del_anchor = ereg_replace('.*openIT\(\'([^\']+)\'.*', "\\1", $label_arr['del_anchor']);
$form_del_anchor = $label_arr['del_anchor'];
$form_del_anchor_before = ereg_replace('%s', '', $form_del_anchor);
$del_func_js = '
<SCRIPT language=javascript>
function ToggleAll(e,f){ if(e.checked){ CheckAll(f);}else{ ClearAll(f);}}
function CheckAll(f){ var ml=f;var len=ml.elements.length;for(var i=0;i
function DelSelected'.$delform_name.'(f){ var ml=f;var len=ml.elements.length;var url="";for(var i=0;iDel Selected Status


