2010年1月28日 星期四

[PHP]查詢欄位資料,並限制在一定的欄位資料量才查詢。

其實這是在ptt看到別人寫的問題。
然後把他物件化實作成功能。
主程式如下:
require("sql_connect.php");
require("class.data_serach.php");
$data_serach = new data_serach("test","sick_member",3);
$sql = $data_serach->sql_text();
如此就會獲得一個可以用來查詢的sql字串,再看使用者怎麼去用這個字串。
其中資料庫名稱和資料表以及需要多少表單的資料量才能進行搜尋,必須在物件成生時就給
予其值。(因為寫入建構子)

物件檔名:class.data_serach.php
class data_serach{
    
    function __construct($database,$table,$mixtimer){
        $this->database = $database;
        $this->table = $table;
        $this->mixtimer = $mixtimer;
        $this->load_field();
        $this->post_check();   
    }
    
    function load_field(){
        $result = mysql_list_fields($this->database,$this->table);
        $field_num = mysql_num_fields($result);
        for ($i = 0;$i <$field_num;$i++){
            $fieldarr[] = mysql_field_name($result,$i);   
        }
    $this->fieldarr = $fieldarr;    
    }
    
    function post_check(){
        foreach($this->fieldarr as $value){
            if (strlen($_POST[$value]) != 0){
            $this->where .= "{$value}='{$_POST[$value]}' AND ";
            $this->timer +=1;
            }
        }
    }    
    
    function sql_text(){
        if (($this->timer >= $this->mixtimer)) return "SELECT * FROM incoming WHERE ".substr($this->where,0,-5);
    }
}

2010年1月24日 星期日

[PHP]欄位同mysql資料庫欄位即可順利新增(增加欄位比對,並將欄位參數化)

這一次的修改本物件的功能。
之前會發生如果表單傳入的name名稱多於資料庫欄位時,會造成資料存入錯誤。
雖然前一個版本加上手動排除多餘欄位,但不甚方便。
這一次更新的版本,新增name名稱與資料庫欄位判斷,會將資料庫沒有的欄位名稱排除。
如此就不會造成欄位不符而回應錯誤。
另外為了能夠支援不同型式的資料來源(get、post或是二者都接受),將不同的函式修正成一個參數型態。
物件方法:
$add_sql = new add_sql("資料庫名稱","資料來源")
//資料來源參數:post = $_POST,get = $_GET,request = $_REQUEST

使用範例1:(以支援$_POST為例)
require_once("class.add_sql.php");
$add_sql = new add_sql("table","post");
$add_sql->sql_save();

使用範例2:(以支援$_GET且額外插入資料為例)
require_once("class.add_sql.php");
$add_sql = new add_sql("table","get");
$add_data -> add_text("guestdate",date("Y-m-d H:i:s"));
$add_sql->sql_save();

物件程式:class.add_sql.php
class add_sql{
    function __construct($table,$PostData){
        $this->table = $table;
        $this->load_field();
        $this->PostData($PostData);
        
    }  
    
    function PostData($PostDataIndex){
      $postarray = array("post" => $_POST , "get" => $_GET , "request" => $_REQUEST);
      $this->SetPostData($postarray[$PostDataIndex]);
    }
    
    function SetPostData($PostData)
    {
        $this->PostData = $PostData;
        $this->dearray();
    }
    
    function load_field(){
        $sql = "SELECT * FROM {$this->table} LIMIT 0,1";
        $result = mysql_query($sql);
        $this->sqlfield = mysql_fetch_assoc($result); 
    }  
    
    function bool_field($keyword){
        foreach($this->sqlfield as $key => $value){
            if ($keyword == $key) return TRUE;
        }    
        return FALSE;
    }
    
    function dearray(){
        foreach($this->PostData as $key => $value){
            if ($this->bool_field($key)) {
                $this->add_text($key,$value);
                
            }  
        }
    }
    
    function add_text($add_field,$add_value){
        $this->field .= $add_field.",";
        $this->data .= "'".htmlentities($add_value,ENT_QUOTES,"utf-8")."',";
     }
  
    function sql_text(){
        $field = substr($this->field,0,strlen($this->field) -1);
        $data = substr($this->data,0,strlen($this->data) -1);
        $sql_stmt =  "INSERT INTO `".$this->table."` (".$field.") VALUES (".$data.")";
        return $sql_stmt;
    }
  
    function sql_save(){
        mysql_query($this->sql_text()) or die("NO Action");
    }  
}

2010年1月18日 星期一

[PHP]只要欄位名稱同資料庫名稱時即可順利存入mysql(增加GET、REQUEST)

這次經過我們「敏捷開發」講師ERIC的指導之下。
更進一步的了解了「多型」、「超級繼承」以及「介面」、虛擬函式等等技術。
所以也同時開發出除了原本針對$_POST,又可以變更成$_GET或是$_REQUEST的做法。
用法:
如果是post的來源:
require("class.get_post_sql.php");
$postdata = new add_post("資料表名稱");
$postdata -> sql_save;

如果是get的來源:
require("class.get_post_sql.php");
$postdata = new add_get("資料表名稱");
$postdata -> sql_save;

如果有post和get的來源:
require("class.get_post_sql.php");
$postdata = new add_request("資料表名稱");
$postdata -> sql_save;

有其他的資料來源(以post為例)
require("class.get_post_sql.php");
$postdata = new add_post("資料表名稱");
$postdata -> add_text("資料欄位","值");
$postdata -> sql_save;

檔名:class.get_post_sql.php
interface add_interface{
  
  function add_text($add_field,$add_value);
  
  function sql_text();
  
  function sql_save();
  
  function dearray();
    
}

abstract class super_add implements add_interface{
   
  function __construct($table){
    $this->table = $table;
    $this->dearray();
  }  
    
  function add_text($add_field,$add_value){
    $this->field .= $add_field.",";
    $this->data .= "'".htmlentities($add_value,ENT_QUOTES,"utf-8")."',";
  }
  
  function sql_text(){
    $this->field = substr($this->field,0,strlen($this->field) -1);
    $this->data = substr($this->data,0,strlen($this->data) -1);
    return "INSERT INTO `".$this->table."` (".$this->field.") 
            VALUES (".$this->data.")";
  }
  
  function sql_save(){
    mysql_query($this->sql_text()) or die("NO SAVE");
  }  
}

class add_get extends super_add{

  function dearray(){
    foreach($_GET as $key => $value){
      if ($key != "submit" && $key != "MM_insert" && $key != "button") {
        $this->add_text($key,$value);
      }  
    }
  }
    
}

class add_post extends super_add{
 
  function dearray(){
    foreach($_POST as $key => $value){
      if ($key != "submit" && $key != "MM_insert" && $key != "button") {
        $this->add_text($key,$value);
      }  
    }
  }
 
}

class add_request extends super_add{
 
  function dearray(){
    foreach($_REQUEST as $key => $value){
      if ($key != "submit" && $key != "MM_insert" && $key != "button") {
        $this->add_text($key,$value);
      }  
    }
  }
 
}

2010年1月5日 星期二

[PHP]chghtml - 置換html樣板內容

程式用途:置換html設定的樣板文字(以#?及?#包起來的文字)。
範例:
以一個留言版的html模板為例其顯示如下:


#?account?#
#?guestdate?#

#?textguest?#

當你的php讀取完需要置換的資料並儲存成陣列之後。
就可以使用chghtml這個物件來置換網頁資料並做最終輸出。
其使用方式如下:
$data = array("account"=>"使用者帳號","guestdate"=>"發表時間","textguest"=>"文章內容");
require("class.chghtml.php");
$chghtml = new chghtml("樣板檔名");
$chghtml -> chgpage($data);
※置換陣列變數:需要定義key及value,若資料來源為資料庫時,模版的文字名稱需要和
資料庫的欄位名稱相同才能順利置換。若為一般來源,key應該要等於模版文字名稱。

顯示結果:

使用者帳號
發表時間

文章內容

物件檔名:class.chghtml.php
class chghtml{
    
  function __construct($load){
    $this->load = file_get_contents($load);
  }
      
  function chgpage($data = array()){
    $this->chgpage = $this->load;
    foreach($data as $key => $value){
      $value = str_replace("\r\n","<br>",$value);
     $this->chgpage = str_replace("#?".$key."?#",
      html_entity_decode($value),$this->chgpage);
    }
  }
  
  function output(){
   return $this->chgpage;
  }
}

2010年1月3日 星期日

[PHP]add_data - 表單名稱與資料庫欄位相同時,即可新增入資料庫。

這個物件的功能,主要是當使用者要將表單送出的資料存入資料庫時。
只要表單的欄位名稱和資料庫的欄位名稱相同時,即可以順利儲存。
(註:若名稱不同會無法存入,請務必要讓名稱相同。)
(註2:非使用表單如送出,或是只是內部判斷密碼是否相同的情形,請勿設定「name」。
最好的方式是判斷資料使用java script做判斷處理)
另外,由於有時候會有額外的資訊,比如說和時間有關的即時生成資料。
故此物件也加入了額外資料的處理方法。
使用方法的程式段:
無額外資料段使用法:

require("class.add_data.php");
$add_data = new add_data("資料表名稱");
$add_data -> sql_save();

有額外資料段使用法:
require("class.add_data.php");
$add_data = new add_data("資料表名稱");
$add_data -> add_text("欄位名稱","資料內容")
$add_data -> sql_save();

物件檔名:class.add_data.php
class add_data{
  
  function __construct($table){
    $this->table = $table;
    $this->Post_dearray();
  }

  function Post_dearray(){
    foreach($_POST as $key => $value){
      if ($key != "submit" && $key != "MM_insert" && $key != "button") {
        $this->add_text($key,$value);
      }  
    }
  }
  
  function add_text($add_field,$add_value){
    $this->field .= $add_field.",";
    $this->data .= "'".htmlentities($add_value,ENT_QUOTES,"utf-8")."',";
  }
  
  function sql_text(){
    $this->field = substr($this->field,0,strlen($this->field) -1);
    $this->data = substr($this->data,0,strlen($this->data) -1);
    return "INSERT INTO `".$this->table."` (".$this->field.") 
            VALUES (".$this->data.")";
  }
  
  function sql_save(){
    mysql_query($this->sql_text()) or die("NO SAVE");
  }
}