2012-12-30 30 views
0

任何人都可以給我任何意見關於如何獲得一個帶有4個文本字段的HTML表單來搜索我的數據庫並查找其中的任何相關數據並顯示它?MySQL&CodeIgniter!搜索表單問題

基本上,這種形式並不需要完全填充,用戶只能輸入1或2個字段(例如first_name和last_name),然後模型應該搜索其他2個缺失的字段用戶輸入。

必須填寫至少一個字段才能使搜索操作正常工作,此字段可以是4個(隨機)中的任何一個。該字段被命名爲:

姓 姓 部門 標題

我有我的數據庫包含所需的信息3個表,有如下幾點:

部門(DEPT_NO,DEPT_NAME) employees(emp_no,first_name,last_name) title(emp_no,title)

由於不是所有的人都共享相同的主鍵,我的數據庫中還有另一個表,能夠'員工'表。

(departments_employees)=> dept_emp(EMP_NO,DEPT_NO)

下面我model.php文件使用所有這些表,以尋找一些數據,但到目前爲止,這個功能只搜索相匹配的數據'firstname'條目,其餘輸入字段將被忽略。

<?php 

class Emp_model extends CI_Model { 

function find_dept() 
{ 
    $this->db->select('employees.first_name, employees.last_name, departments.dept_name, titles.title'); 
    $this->db->where('last_name', $this->input->get('lastname')); 
    $this->db->join('dept_emp', 'dept_emp.emp_no = employees.emp_no'); 
    $this->db->join('departments', 'departments.dept_no = dept_emp.dept_no'); 
    $this->db->join('titles', 'titles.emp_no = employees.emp_no'); 
    $query = $this->db->get('employees'); 

    if($query->num_rows > 0) 
    { 
     return $query->result(); 

    } 

    else 
    { 
     redirect('find'); 

    } 

} 
} 

?> 

我的視圖在一個表中顯示結果,因此到目前爲止所有的工作都沒有錯誤。經過研究後,我無法想出辦法做到這一點。如果任何人有任何想法或類似的教程,我可以關注,請讓我知道!非常感謝!謝謝:)

如果我沒有讓自己清楚或需要更多的信息,請讓我知道!

回答

1

你可以傳遞一個數組CI活動記錄,你必須,如果檢查您 你應該叫活動記錄所有投入其中,獲得

$inputs = $this->input->post(); // you get an Associative array of post data 

extract($inputs); // will extract variables from Associative array. 

$where = array(); 

if(strlen($first_name)) 
{ 
    $where['first_name'] = $first_name; 
} 

    if(strlen($last_name)) 
{ 
    $where['last_name'] = $last_name; 
} 

if(strlen($dept)) 
{ 
    $where['dept'] = $dept; 
} 

    if(strlen($title)) 
{ 
    $where['title'] = $title; 
} 

$this->db->where($where); 
$query = $this->db->get('employees'); 
+0

OMG謝謝,這個工作完美后,我加入吧:),althought目前我使用我的本地數據庫,它需要一個痘痘,同時檢索所有信息。希望在將它傳輸到我的教師服務器後,它會加快一點。 再次感謝:) – Ragazz4

1

之前做看來你是非常接近完成你的任務,關於你的代碼應該提到幾件事情。你不應該在模型中直接使用$this->input->get('lastname'),你應該抓住控制器中的信息,並使用這樣的事情把它傳遞給模型:

function find_dept($firstName, $lastName, $dept, $title) { 
    ... 
} 

這將允許你重用模型功能,而無需依靠信息發送給它的方法。

function find_dept($firstName = false, $lastName = false, $dept = false, $title = false) { 
    $this->db->select('employees.first_name, employees.last_name, departments.dept_name, titles.title'); 

    $this->db->join('dept_emp', 'dept_emp.emp_no = employees.emp_no'); 
    $this->db->join('departments', 'departments.dept_no = dept_emp.dept_no'); 
    $this->db->join('titles', 'titles.emp_no = employees.emp_no'); 

    if($firstName && $firstName !== '') 
    { 
    $this->db->where('employees.first_name', $firstName); 
    } 

    if($lastName && $lastName !== '') 
    { 
    $this->db->where('employees.last_name', $lastName); 
    } 

    if($dept && $dept !== '') 
    { 
    $this->db->where('departments.dept_name', $dept); 
    } 

    if($title && $title !== '') 
    { 
    $this->db->where('titles. title', $title); 
    } 


    $query = $this->db->get('employees'); 
} 

爲了詳細說明笨,我使用if($dept && $dept !== '')因爲$this->input->get('lastname')返回false如果它沒有設置,但您可能需要檢查它是否等於一個空字符串。有可能是一個更好的方式來寫這

你也可以$this->db->like改善您的搜索。瞧瞧吧:http://ellislab.com/codeigniter/user-guide/database/active_record.html

+0

非常感謝您的回覆@GrayB,但是我近來應用解決方案,以我的笨文件夾,並得到這個錯誤,當我執行搜索: _Fatal錯誤:讓用盡134217728個字節的內存大小(試在C分配47個字節):\ XAMPP \ htdocs中\ w1228355 \ SYSTEM \數據庫\ DRIVERS \ mysql的\ mysql_result.php上線167_ 我的數據庫是非常巨大的,所以我不知道是否有用它做什麼。同時從我的控制器中獲取信息,如你所建議的,給了我一些錯誤。我很抱歉,但我在CodeIgniter中有點新鮮。任何跟進ups將不勝感激。 – Ragazz4

+0

試試這個鏈接:http://stackoverflow.com/questions/561066/php-fatal-error-allowed-memory-size-of-134217728-bytes-exhausted-codeigniter – GrayB

+0

還檢查解壓php文檔有關警告時使用$ _GET和$ _POST http://php.net/manual/en/function.extract.php – GrayB