看板 PHP 關於我們 聯絡資訊
整個方法重新改寫結構(是還可以再更加優美,不過先做到這樣好了。) 主程式檔名:whquery.php <?php require("sql_connect.php"); //連結資料庫程式檔,請自行實作他。 require("class.data_serach.php"); $data_serach = new data_serach("資料庫名稱","資料表名稱",3); //3表示要3個欄位才行,依實際需求來改 $sql = $data_serach->sql_text(); if (!empty($sql)){ $result = mysql_query($sql,$conn); while($row = mysql_fetch_array($result)) { $datatable .= get_datatable($row["id"],$row["item"] ,$row["barcode"],$row["datearrivetom"]); } echo $datatable; }else{ echo "輸入資料不足,無法查詢"; } mysql_free_result($result); mysql_close(); fucntion get_datatable($id,$item,$barcode,$datearrivetom){ $html = "<tr><td align='left' height='20'>$id</td>"; $html .= "<td align='left'>$item</td>"; $html .= "<td align='left'>$barcode</td>"; $html .= "<td align='left'>$dataarrivetom</td></tr>"; return $html; } ?> 檔名:class.data_serach.php 物件功能: 1.比對輸入欄位及資料庫欄位 2.輸入資料的欄位大於預設數量才進行查詢。 使用方法: $data_serach = new 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-01-29 10:21修正: PO程式時不小心多打了一個「function」,已修正。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.136.48.82 ※ 編輯: tkdmaf 來自: 220.136.43.164 (01/29 10:52)