* * Usage example: * * $orderby=get_order(); * $query="SELECT * FROM fic_courses $orderby"; * $courses=DB::get($query,PDO::FETCH_ASSOC); * $headers=array( * 'course_id'=>'', * 'title'=>'Course Title', * 'date_start_course'=>'Start', * 'date_end_course'=>'End', * 'date_start_reg'=>'Reg Starts', * 'date_end_reg'=>'Reg Ends', * 'active'=>'Active?', * 'entry_date'=>'Created' * ); * show_list($courses,$headers); * * * Accompanying CSS for table output: * * .list * { * border:1px solid #999; * } * .list th * { * background:#eee; * border:1px solid #000; * font-weight:bold; * } * .list th a * { * display:block; * padding:0 14px; * } * .list th a:hover * { * background-color:#fff; * } * .row1 * { * background:#ddd; * } * .row2 * { * background:#ccc; * } * .row1:hover, .row2:hover * { * background-color:#fec; * } * .current-sort * { * background:#fda; * } * .sort-desc * { * background:#fec url(../img/up.gif) no-repeat right; * } * .sort-asc * { * background:#fec url(../img/down.gif) no-repeat right; * } * * Accompanying JavaScript for select all / inverse: * * * */ /** * Show a list of values, for forms. * @param array $list associative array * @param array $headers column name => column title (for table heads) * @param string $type checkbox, radio, simple * @param array $array actions to display in actions select list * @param string $form_id id of form holding list * @param bool $sortable whether or not to show sortable column headers (links in th's) * @param array|string $selected if type is checkbox, array otherwise string with one val */ function show_list($list,$headers,$type='checkbox',$actions=null,$form_id=null,$sortable=true,$selected=null) { $count = 0; if ( is_array($list) && count($list)>0 && is_array($headers) ) { if ( $type!='simple' && !empty($_GET['sort']) && !empty($_GET['order']) ) { form_hidden('sort',$_GET['sort']); form_hidden('order',$_GET['order']); } echo "\n".''; show_headers($headers,$type,$sortable); echo "\n".''; foreach ($list as $row) { show_row($headers,$row,$type,$count++,$selected); } echo "\n".''; echo "\n".'
'; if ($type=='checkbox') { echo << // js; echo "\n".'

'; } if ($type=='radio'||$type='checkbox-small') { echo '
'; } if (is_array($actions)&&$type!='simple') { if (count($actions) == 1) { $actions = array_values($actions); echo '

'; form_submit('submit','submit','button1',$actions[0].' »'); echo '

'; } else { echo '

'; echo ''; form_select('action','action','text2',$actions,''); form_submit('submit','submit','button1','Go »'); echo '

'; } } } elseif ( !is_array($headers) ) { echo "\n".'

FIX HEADERS ARRAY

'; } else { echo "\n".'

No records found.

'; } } /** * Show table headers. * @param array $headers column name => column title (for table heads) * @param string $type type of list that is being shown * @param bool $sortable whether or not to show sortable column headers (links in th's) */ function show_headers($headers,$type,$sortable=true) { echo "\n".''; $sort=$_GET['sort']; $order=get_order(); $count=0; foreach ($headers as $col=>$title) { if ( !empty($sort) && !empty($order) ) { if ($col==$sort && $order=='ASC') { $a_class=' class="sort-asc current-sort" '; } elseif ($col==$sort && $order=='DESC') { $a_class=' class="sort-desc current-sort" '; } else { $a_class=null; } } if ($type!='simple'&&$count==0) { echo "\n".' '; continue; } elseif($sortable) { $qs = array(); foreach ($_GET as $qn=>$qv) { $qs[$qn] = $qv; } // existing query string variables $qs['sort'] = $col; // add/replace sort to query string $qs['order'] = $order; // add/replace order by to query string foreach ($qs as $qn=>$qv) { $querystring[] = $qn.'='.$qv; } // existing query string variables echo "\n".''.$title.''; unset($qs); unset($querystring); } else { echo "\n".''.$title.''; } $count++; } echo "\n".''; } /** * Show table data. * @param array $headers column name => column title (for knowing which ones to display) * @param array $row table row, assoc * @param string $type type of table, determines first column, which could be an input * @param array|string $selected selected items; if type is checkbox, array otherwise string with one val */ function show_row($headers,$row,$type,$num=null,$selected=null) { $indexes=array_keys($headers); $idname = $indexes[0]; $count=0; $tr_class=($num%2)?' class="row1" ':' class="row2" '; echo "\n".''; foreach ($indexes as $index) { $row[$index]=clean_out($row[$index]); if ($type!='simple'&&$count==0) { $id=preg_replace('/[^[:alnum:]]/', '', $index).$row[$index]; if ($type=='checkbox'||$type=='checkbox-small') { echo "\n".''; form_checkbox($idname.'[]',$id,null,$row[$index],(is_array($selected) && in_array($row[$index], $selected))); echo "\n".''; } elseif ($type=='radio') { echo "\n".''; form_radio($idname,$id,null,$row[$index], ($row[$index] == $selected)); echo "\n".''; } } else { echo ($type=='simple')?"\n".''.$row[$index].'':"\n".''; } $count++; } echo "\n".''; } /** * Determine current sort order. */ function get_order() { return ($_GET['order']=='ASC')?'DESC':'ASC'; } /** * Determine whether or not list is currently sorted. * @param string $method which http method to check for sort information * @return mixed cleaned orderby clause based on saved sort information or null if no orderby is set in the defined method */ function get_orderby($method='get') { if ( $method=='get' && !empty($_GET['sort']) && !empty($_GET['order']) ) { $sort=clean_in($_GET['sort']); $order=clean_in($_GET['order']); return " ORDER BY $sort $order "; } elseif ( $method=='post' && !empty($_POST['sort']) && !empty($_POST['order']) ) { $sort=clean_in($_POST['sort']); $order=clean_in($_POST['order']); return " ORDER BY $sort $order "; } elseif ( $method=='session' && !empty($_SESSION['sort']) && !empty($_SESSION['order']) ) { $sort=clean_in($_SESSION['sort']); $order=clean_in($_SESSION['order']); return " ORDER BY $sort $order "; } else return null; }