PHP自写DATAGRID函数

    技术2025-06-27  19

    最近做的东西经常要做数据集显示,讨厌一遍又一遍地HTML编码和CSS设置,写个function来显示数据吧..

    //************************************************************************************ // 数据集展示函数 //************************************************************************************ /** * 获取当前页面URL和所有参数 * Enter description here ... */ function pageTableDataGridGetURL() { return $_SERVER ["PHP_SELF"] . "?" . $_SERVER ['QUERY_STRING']; } /** * 获得除orderby以外的所有参数 * Enter description here ... */ function pageTableDataGridGetURLparams() { $queryString_list = ""; if (! empty ( $_SERVER ['QUERY_STRING'] )) { $params = explode ( "&", $_SERVER ['QUERY_STRING'] ); $newParams = array (); foreach ( $params as $param ) { if (stristr ( $param, "orderby" ) == false) { array_push ( $newParams, $param ); } } if (count ( $newParams ) != 0) { $queryString_list = "&" . htmlentities ( implode ( "&", $newParams ) ); } } return $queryString_list; } /** * 生成数据展示用的TABLE * @author Hiton 2011-02-10 09:31:00 * @param array $data 数据的数组 * 如 array(0=>array( * 'id'=>'1', * 'title'=>'标题1-123123', * 'date'=>'2011-02-06 11:32:30' * ), * 1=>array( * 'id'=>'2', * 'title'=>'标题2-123123', * 'date'=>'2011-02-08 13:32:30' * ), * 2=>array( * 'id'=>'5', * 'title'=>'标题5-123123', * 'date'=>'2011-02-09 18:32:30' * ) * ) * @param array $title 数据表第一行标题以及对应的数据数组的键值 * 如 array('编号'=>array( * 'data'=>'id', * 'href'=>'javascript:', * 'click_func'=>'testJs', * 'param'=>array( * '0'=>array('key','id'), * '1'=>array('str','0'), * '2'=>array('str','del') * ) * ), * '标题'=>array( * 'data'=>'title', * 'href'=>'./page.php', * 'click_func'=>'', * 'param'=>array( * 'id'=>array('key','id'), * 'action'=>array('str','show') * ) * ), * '时间'=>array( * 'data'=>'date', * 'href'=>'', * 'click_func'=>'', * 'param'=>'' * )) * @param str $row_bg_color 奇数行变色 如#F1F1F1 * @param str $over_bg_color 鼠标悬停数据的背景颜色 如#F1F1F1 * @param array $manage_row 默认空则没有管理列 * 如 array('编辑'=>array( * 'href'=>'./edit.php', * 'click_func'=>'', * 'param'=>array( * 'id'=>array('key','id'), * 'action'=>array('str','edit') * ) * ), * '删除'=>array( * 'href'=>'javascript:', * 'click_func'=>'actionJs', * 'param'=>array( * '0'=>array('key','id'), * '1'=>array('str','del') * ) * ) * ) */ function setPageTableDataGrid($data, $title, $row_bg_color, $over_bg_color, $order = false, $manage_row = '') { $html = ''; //做一个JS $html .= '<mce:script type="text/javascript"><!-- '; //变换颜色的JS $html .= 'function PageTableDataGridLineBgChange(obj,type,overcolor,outcolor){'; $html .= 'var target = document.getElementById(obj);'; $html .= 'if(type == /'over/'){'; $html .= 'target.style.backgroundColor = overcolor;'; $html .= '} else {'; $html .= 'target.style.backgroundColor = outcolor;'; $html .= '}'; $html .= '}'; //全选复选框的JS $html .= 'function PageTableDataGridCheckBoxSwitch(){'; $html .= 'var chk = document.getElementsByName("datagrid_box[]");'; $html .= 'var now = document.getElementById("checkbox_all");'; $html .= 'var nowstatus;'; $html .= 'if(now.checked){'; $html .= 'nowstatus = true;'; $html .= '}else{'; $html .= 'nowstatus = false;'; $html .= '}'; $html .= 'for(var i=0;i<chk.length;i++)'; $html .= '{'; $html .= 'chk[i].checked=nowstatus;'; $html .= '}'; $html .= '}'; $html .= ' // --></mce:script>'; $now_page = $_SERVER ["PHP_SELF"]; $now_params = pageTableDataGridGetURLparams (); //开始创建表格 $html .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">'; //表格的标题行 $html .= '<tr>'; //第一列是选择框列 $html .= '<td height="30" width="3%" align="center" style=" border-bottom:2px #CCC solid" mce_style=" border-bottom:2px #CCC solid"><input type="checkbox" name="checkbox_all" id="checkbox_all" οnchange="PageTableDataGridCheckBoxSwitch()" /></td>'; //后面开始内容列 foreach($title as $k=>$v){ $html .= '<td height="30" align="center" style=" border-bottom:2px #CCC solid" mce_style=" border-bottom:2px #CCC solid">'; //表格排序 if($order){ $html .= '<a href="'.$now_page.'?orderby='.$v['data'].$now_params.'" mce_href="'.$now_page.'?orderby='.$v['data'].$now_params.'">'; } $html .= '<strong>'.$k; if($_GET['orderby'] == $v['data']){ $html .= '↓'; } $html .= '</strong>'; //表格排序 if($order){ $html .= '</a>'; } $html .= '</td>'; } //如果有表格管理列 if(is_array($manage_row)){ $html .= '<td height="30" align="center" style=" border-bottom:2px #CCC solid" mce_style=" border-bottom:2px #CCC solid"><strong>管理</strong></td>'; } $html .= '</tr>'; //表格标题行结束 //表格内容行开始 foreach($data as $k=>$v){ $lineid = $k+1; $html .= '<tr'; $html .= ' id="data_grid_line_'.$k.'"'; //奇数行背景色变色 $this_row_bgcolor = ''; if ($lineid%2 <> 0) { $this_row_bgcolor = $row_bg_color; } $html .= " bgcolor='{$this_row_bgcolor}'"; //鼠标悬停变色 $html .= " οnmοuseοver=PageTableDataGridLineBgChange('data_grid_line_{$k}','over','{$over_bg_color}','{$this_row_bgcolor}')"; $html .= " οnmοuseοut=PageTableDataGridLineBgChange('data_grid_line_{$k}','out','{$over_bg_color}','{$this_row_bgcolor}')"; $html .= '>'; //第一列是选择框列 $html .= '<td height="25" align="center" style="border-bottom:1px #CCC solid" mce_style="border-bottom:1px #CCC solid"><input type="checkbox" name="datagrid_box[]" id="datagrid_box_'.$k.'" /></td>'; //内容行的内容列循环 foreach($title as $key=>$value){ $this_data_key = $value['data']; //在data二维数组里,这个列需要展示的键值 $this_href = $value['href']; //这个值需要超链接地址 $this_click_func = $value['click_func']; //假如是JS事件,那JS的事件名称在这里 $this_param = $value['param']; //JS事件里面或者HREF链接的参数数组 $html .= '<td height="25" align="center" style="border-bottom:1px #CCC solid" mce_style="border-bottom:1px #CCC solid">'; //假如这一列有超链接的话 if($this_href != ''){ //假如有超链接 并且超链接不是javascript: $this_href_string = $this_href; if($this_href != 'javascript:'){ $this_href_string = $this_href; if(is_array($this_param)){ //解析参数数组 $this_href_param_length = count($this_param); $this_href_i = 0; $this_href_string .= '?'; foreach($this_param as $param_key=>$param_value){ if($param_value[0] == 'str'){ //一般的文字 $this_href_string .= $param_key.'='.$param_value[1]; } if($param_value[0] == 'key'){ //数组的某个值 $this_href_string .= $param_key.'='.$v[$param_value[1]]; } //参数之间的&符号 $this_href_i++; $param_position = $this_href_i; if($param_position < $this_href_param_length){ $this_href_string .= '&'; } } } } $html .= '<a href="'.$this_href_string.'" mce_href="'.$this_href_string.'"'; //假如有js事件 if($this_click_func != ''){ //解析JS的参数数组 生成参数的内容 $this_click_param_string = ''; if(is_array($this_param)){ $this_click_param_length = count($this_param); $this_click_i = 0; foreach($this_param as $param_key=>$param_value){ if($param_value[0] == 'str'){ //一般的文字 $this_click_param_string .= '/''.$param_value[1].'/''; } if($param_value[0] == 'key'){ //数组的某个值 $this_click_param_string .= '/''.$v[$param_value[1]].'/''; } //参数之间的逗号分割 $this_click_i++; $param_position = $this_click_i; if($param_position < $this_click_param_length){ $this_click_param_string .= ','; } } } $html .= ' οnclick="'.$this_click_func.'('.$this_click_param_string.')"'; } $html .= '>'; } $html .= $v[$this_data_key]; //这一列的数值出来了 if($this_href != ''){ $html .= '</a>'; } $html .= '</td>'; } //管理列的数据 if(is_array($manage_row)){ $html .= '<td align="center" style="border-bottom:1px #CCC solid" mce_style="border-bottom:1px #CCC solid">'; $manage_length = count($manage_row); $manage_i = 0; foreach($manage_row as $word=>$value){ $this_manage_href = $value['href']; $this_manage_click_func = $value['click_func']; $this_manage_param = $value['param']; //分析超链接 if($this_manage_href != ''){ $this_manage_href_string = $this_manage_href; //这是个超链接 if($this_manage_href != 'javascript:'){ $this_manage_href_string = $this_manage_href; if(is_array($this_manage_param)){ $this_manage_href_param_length = count($this_manage_param); $this_manage_href_i = 0; $this_manage_href_string .= '?'; foreach($this_manage_param as $param_key=>$param_value){ if($param_value[0] == 'str'){ //一般的文字 $this_manage_href_string .= $param_key.'='.$param_value[1]; } if($param_value[0] == 'key'){ //数组的某个值 $this_manage_href_string .= $param_key.'='.$v[$param_value[1]]; } //参数之间的&符号 $this_manage_href_i++; $param_position = $this_manage_href_i; if($param_position < $this_manage_href_param_length){ $this_manage_href_string .= '&'; } } } } $html .= '<a href="'.$this_manage_href_string.'" mce_href="'.$this_manage_href_string.'"'; //有click事件 if($this_manage_click_func != ''){ $this_manage_click_param_string = ''; if(is_array($this_manage_param)){ $this_manage_click_param_length = count($this_manage_param); $this_manage_click_i = 0; foreach($this_manage_param as $param_key=>$param_value){ if($param_value[0] == 'str'){ //一般的文字 $this_manage_click_param_string .= '/''.$param_value[1].'/''; } if($param_value[0] == 'key'){ //数组的某个值 $this_manage_click_param_string .= '/''.$v[$param_value[1]].'/''; } //参数之间的&符号 $this_manage_click_i++; $param_position = $this_manage_click_i; if($param_position < $this_manage_click_param_length){ $this_manage_click_param_string .= ','; } } } $html .= ' οnclick="'.$this_manage_click_func.'('.$this_manage_click_param_string.')"'; } $html .= '>'; } $html .= $word; if($this_manage_href != ''){ $html .= '</a>'; } //分隔符 $manage_i++; if($manage_i < $manage_length){ $html .= ' | '; } } $html .= '</td>'; } $html .= '</tr>'; } return $html; }

    最新回复(0)