-
-
class Db
- {
- var $conn;
-
- function Db($host="localhost", $user="root",$pass="root",$db="test")
- {
- if(!$this- >conn=mysql_connect($host,$user,$pass))
- die("無法連線到mysql 伺服器");
- mysql_select_db($db,$this->conn);
- mysql_query("設定名稱'UTF-8'");
- }
-
- 函數執行($sql)
- {
- return mysql_query($sql,$this->conn);
- }
-
- function findCount($sql)
- {
- $result=$this->execute($sql);
- return mysql_num_rows($result);
- }
-
- function findBySql($sql)
-
- function findBySql($sql)
- {
- $ array=array();
- $結果=mysql_query($sql);
- $i=0;
- while($row=mysql_fetch_assoc($result))
- {
- $array[ $i]=$row;
- $i++;
- }
- 回傳$array;
- }
-
- //$con的幾種情況
- //空:回傳全部記錄
- //陣列:eg. array('id'=>'1') 傳回id=1的記錄
- //string :eg. 'id=1' 傳回id=1的記錄
- function toExtJson($table,$start="0",$limit="10",$cons="")
- {
- $sql= $this->generateSql($table,$cons);
- $totalNum=$this->findCount($sql);
- $result=$this->findBySql($sql." LIMIT ".$start." ,".$limit);
- $ resultNum = count($result);//目前結果數
- $str="";
- $str.= "{";
- $str.= "'totalCount':'$totalNum', ";
- $str.="'行':";
- $str.="[";
- for($i=0;$i $str.="{";
- $count=count($result[$i]);
- $j=1;
- foreach($result[$i] as $key=>$val )
- {
- if($j {
- $str.="'". $key."':'".$val."',";
- }
- elseif($j==$count)
- {
- $str.="'".$key."':'".$val."'";
- }
- $j++;
- }
-
- $str.="}";
- if ($i != $resultNum-1) {
- $str.= ",";
- }
- }
- $str.="]";
- $str.="}";
- 回傳$str;
- }
-
- functiongenerateSql($table ,$cons)
- {
- $sql="";//sql條件
- $sql="select * from ".$table ;
- if($cons!="")
- {
- if(is_array($cons))
- {
- $k=0;
- foreach($cons as $key=>$val)
- {
- if($ k==0)
- {
- $sql.="where '";
- $sql.=$key;
- $sql.="'='";
- $sql. =$val."'";
- }else
- {
- $sql.="and '";
- $sql.=$key;
- $sql.="'=' ";
- $sql.=$val."'";
- }
- $k++;
- }
- }else
- {
- $sql.=" 其中". $cons;
- }
- }
- return $sql;
- }
-
- function toExtXml($table,$start="0",$limit="10",$cons ="")
- {
- $sql=$this-> ;generateSql($表,$cons);
- $totalNum=$this->findCount($sql);
- $result= $this->findBySql($sql." LIMIT ".$start." ,".$limit);
- $resultNum = count($result);//目前結果數
- header("Content-Type : text/xml");
- $xml="n";
- $xml.="n";
- $xml.="t".$totalNum."n";
- $xml.="tn";
- for($i=0;$i $xml.="tt
- n";
- foreach($result[$i] as $key=>$val)
- $xml.=" ttt".$val."".$鍵。 ">n";
- $xml.="tt
n";
- }
- $xml.="t
n";
- $xml.="n"; 回傳$xml; }
-
- // 輸出word表格
- function toWord($table,$mapping,$fileName)
- {
- header('Content-type: application/doc');
- header('Content-Disposition:attachment; filename="'.$fileName.'.doc"');
- echo ' xmlns:w="urn:schemas-microsoft-com:office:word"
- xmlns= “ http://www.w3.org/TR/REC-html40”>
-
- >
- '.$fileName.'
- 頭>
- ';
- echo'
'; - if(is_array($mapping))
- {
- foreach($mapping as $key=>$val)
- echo'
'.$val.' | ;' ;
- }
- echo'
'; - $results=$this->findBySql('select * 從 '.$table);
- foreach($results as $result)
- {
- echo'
'; - foreach($result as $key=>$val)
- echo'
'.$val.' | ';
- echo'
';
- }
- echo'
';
- echo'';
- echo'
|