php自定义方法:自动生成数据表格(支持分页)

来源:互联网 发布:刺客列传网络剧视频 编辑:程序博客网 时间:2024/05/04 08:07

php页面代码:

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link type = "text/css" rel = "StyleSheet" href = "mycss/myCss.css"/>
</head>
<body>
<div id="loca">
<?php
  require 'Function.php';

   include 'Fun.php';  
  $fun = new Sql_Function();
   $arr = array("0" => "UserID","1"=>"UserName","2"=>"UserAge");//要显示的数据表字段名称
  $arrCName = array("0"=>"员工编号","1"=>"员工姓名","2"=>"员工年龄");//字段对应的标题名称
  $pagingstyle = "margin:10px auto;color:purple;text-align:center;font-size:14px;";
  Paging(5, "tbluserinfo", "test", $arr, "人员信息一览表", "mytbl", $arrCName,$pagingstyle);

  //参数说明:每页数量,数据表名,文件名,要显示数据表字段名(数组),标题名称,表格样式,字段对应标题(数组),分页css样式
?>
</div>
</body>
</html>

页面执行结果。如果需要,可以使用jquery对表格进一步美化,这里不再赘述。

图片

引入的php文件代码如下:

1、Function.php

<?php
  class Sql_Function
  {
 
  public $conn;
 
  function getConn()
  {  
    $conn = mysql_connect("localhost:3307","root","123456");
     mysql_query("set names gbk;");
    if ($con)
    {
        die('Could not connect: ' . mysql_error());
        return "数据库连接失败!";
    }
    else
    {
     mysql_select_db("marketsystem",$conn);
     return $conn;
    }
   
   
  }
  function getRecordNum($strSql)
  {
   $conn = $this->getConn();
    $res = mysql_query($strSql); 
    $rows = mysql_num_rows($res);
    return $rows;
 
  }
  function getRecordResult($strSql)
  {
   $conn = $this->getConn();
    $res = mysql_query($strSql);
   return $res;
   
  }
  function DoInsert($strSql)
  {
   $conn = $this->getConn();
   $res = mysql_query($strSql,$conn);
   return $res;
  }
  function  DoUpdate($strSql)
  {
   $conn = $this->getConn();
   $res = mysql_query($strSql,$conn);
   return $res;
  }
  function DoDelete($strSql)
  {
   $conn = $this->getConn();
   $res = mysql_query($strSql);
   return $res;
  }
  //自动生成数据表格。参数:数据库名称,查询语句,数据表字段,表格标题,表格样式,标题列名称
  function makeTable($strSelectSql,$arr,$strTitle,$tableStyleClass,$arrColName)
  {
       try {
       $conn = $this->getConn();//得到数据库连接
        $result = mysql_query($strSelectSql);//得到查询结果
        
        $arrLength = count($arr);
        //echo $arrLength;
        $strHtml= "<table class = '" . $tableStyleClass . "'><tr><td colspan = '" . $arrLength . "'>" . $strTitle ."</td></tr>";
        $strHtml .= "<tr>";
        for($j = 0;$j < $arrLength ; $j++)
        {
         $strHtml .= "<td>" . $arrColName[$j] . "</td>";
        }
        $strHtml .= "</tr>";
        
        while($row = mysql_fetch_array($result))
        {
         $strHtml .="<tr>";
         for($i = 0;$i < $arrLength ; $i++)
         {
           $strHtml .= "<td>" . $row[$arr[$i]] ."</td>";
            }
            $strHtml .= "</tr>";
        }
        
        $strHtml .= "</table>";
        
        echo  $strHtml;
     }catch (Exception $ee)
     {
      echo "运转过程中出现错误!<br>请检查输入的数据表字段名称是否存在";
     }
  }
  public function Paging($perNumber,$tblName,$fileName,$arr,$strTitle,$tableStyleClass,$arrColName)
  {
//$perNumber=10; //每页显示的记录数
$page=$_GET['page']; //获得当前的页面值
$count=mysql_query("select count(*) from " . $tblName); //获得记录总数
$rs=mysql_fetch_array($count);
$totalNumber=$rs[0];
$totalPage=ceil($totalNumber/$perNumber); //计算出总页数
if (!isset($page)) {
 $page=1;
} //如果没有值,则赋值1
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始的记录
$result=mysql_query("select * from ".$tblName." limit $startCount,$perNumber"); //根据前面的计算出开始的记录和记录数
//while ($row=mysql_fetch_array($result)) {
// echo "user_id:".$row[0]."<br>";
// echo "username:".$row[1]."<br>"; //显示数据库的内容
//}
$selectSql = "select * from ".$tblName." limit $startCount,$perNumber";
$this->makeTable($selectSql, $arr, $strTitle, $tableStyleClass, $arrColName);
if ($page != 1) { //页数不等于1
?>
<a href= <?php echo "'".$fileName.".php?page = ".($page - 1)."'"?>>上一页</a> <!--显示上一页-->
<?php
}
for ($i=1;$i<=$totalPage;$i++) {  //循环显示出页面
?>
<a href=<?php echo "'".$fileName.".php?page = ". $i."'"?>><?php echo $i ;?></a>
<?php
}
if ($page<$totalPage) { //如果page小于总页数,显示下一页链接
?>
<a href=<?php echo "'".$fileName.".php?page = ".( $page + 1)."'"?>>下一页</a>
<?php
}
   
  }
  }
?>

Fun.php


<?php
 function Paging($perNumber,$tblName,$fileName,$arr,$strTitle,$tableStyleClass,$arrColName,$pagingStyle)
 {
 $conn = getConn();//得到数据库连接
//$perNumber=10; //每页显示的记录数
$page=$_GET['page'];
//$page = $curPage;//获得当前的页面值

$count=mysql_query("select count(*) from " . $tblName); //获得记录总数
$rs=mysql_fetch_array($count);
$totalNumber=$rs[0];
$totalPage=ceil($totalNumber/$perNumber); //计算出总页数
if (!isset($page)) {//如果没有值,则赋值1
 $page=1;
}
//echo $page;
$startCount=($page-1)*$perNumber; //分页开始,根据此方法计算出开始的记录

//$result=mysql_query("select * from ".$tblName." limit $startCount,$perNumber"); //根据前面的计算出开始的记录和记录数

$selectSql = "select * from ".$tblName." limit $startCount,$perNumber";
//echo $selectSql."<br>";
//echo $page;
makeTable($selectSql, $arr, $strTitle, $tableStyleClass, $arrColName);
?>
<div style = <?php echo "/"".$pagingStyle."/""?>>
<?php
if ($page != 1) { //页数不等于1
?>
<a href= <?php echo "/"".$fileName.".php?page=".($page - 1)."/""?>>上一页</a> <!--显示上一页-->
<?php
}
for ($i=1;$i<=$totalPage;$i++) {  //循环显示出页面
?>
<a href=<?php echo "/"".$fileName.".php?page=".$i."/""?>><?php echo $i ;?></a>
<?php
}
if ($page<$totalPage) { //如果page小于总页数,显示下一页链接
?>
<a href=<?php echo "/"".$fileName.".php?page=".( $page + 1)."/""?>>下一页</a>
<?php
}
?>
</div>
<?php
   
  }
 
 
    function makeTable($strSelectSql,$arr,$strTitle,$tableStyleClass,$arrColName)
  {
       try {
     
        $result = mysql_query($strSelectSql);//得到查询结果
        
        $arrLength = count($arr);
        //echo $arrLength;
        $strHtml= "<table class = '" . $tableStyleClass . "'><tr><td colspan = '" . $arrLength . "'>" . $strTitle ."</td></tr>";
        $strHtml .= "<tr>";
        for($j = 0;$j < $arrLength ; $j++)
        {
         $strHtml .= "<td>" . $arrColName[$j] . "</td>";
        }
        $strHtml .= "</tr>";
        
        while($row = mysql_fetch_array($result))
        {
         $strHtml .="<tr>";
         for($i = 0;$i < $arrLength ; $i++)
         {
           $strHtml .= "<td>" . $row[$arr[$i]] ."</td>";
            }
            $strHtml .= "</tr>";
        }
        
        $strHtml .= "</table>";
        
        echo  $strHtml;
     }catch (Exception $ee)
     {
      echo "运转过程中出现错误!<br>请检查输入的数据表字段名称是否存在";
     }
  }
 
   function getConn()
  {  
    $conn = mysql_connect("localhost:3307","root","123456");
     mysql_query("set names gbk;");
    if ($con)
    {
        die('Could not connect: ' . mysql_error());
        return "数据库连接失败!";
    }
    else
    {
     mysql_select_db("marketsystem",$conn);
     return $conn;
    }
   
   
  }
?>
总体感觉这样调用分页还是比较方便的,最起码少写了很多代码。

代码如有谬误,请多指教。

原创粉丝点击