显示与用户输入匹配的结果

时间:2013-08-13 09:36:23

标签: php mysql sequelpro

我正在尝试创建一个函数,通过匹配前几个字母(例如SW1),在数据库中搜索位于用户在表单中输入的邮政编码内的商店。所以基本上:

  1. 用户在文本框中输入邮政编码。
  2. 用户提交条目。
  3. 函数在数据库中搜索具有相同邮政编码的商店
  4. 在表格中显示结果。
  5. 到目前为止,我创建了一个用户可以输入邮政编码的表单,然后是一个不完整的函数,用于选择id,name,address等postcodes_covered。

    function postcode_lookup()
    {
        $this->load->helper('form');
        $this->layout->view('reports_postcode_lookup_form');
    }
    
    function do_postcode_lookup()
    {
    
        $this->db->select('id,name, address1,address2,address3,address4,address5,postcode')
                 ->like('postcodes_covered',$this->input->post('postcode'));
    
    
    
    
    }
    

    如何将所选数据显示在表格中?

    此代码有效。它从数据库中获取数据并将其显示在表中:

    function do_postcode_lookup()
    {
    
        $data['p'] = $p =  $this->input->post('postcode');
        $data['postcode'] = array();
    
        $this->db->select('id,name, address1,address2,address3,address4,address5,postcode');
        $this->db->like('postcodes_covered', $p);
        $bodyshops = $this->db->get('bodyshops')->result();
    
        $this->load->library('Bodyshop', NULL);
        foreach($bodyshops as $b)
            $data['postcode'][$b->id] = new Bodyshop($b->id, $b);
    
        $this->load->helper('form');
        $this->layout->view('reports_postcode_lookup_table', $data);
    
    }
    

1 个答案:

答案 0 :(得分:0)

您没有提到您正在使用的框架或数据库库,但伪代码将是:

function output_postcodes(){

    $result = $this->db->select('id,name, address1,address2,address3,address4,address5,postcode')
             ->like('postcodes_covered',$this->input->post('postcode'));

    while($row = $result->fetchArray()){//Syntax may differ depending on what you are using.
        echo $row['id']. " - ".$row['postcode'];//Change to however you want to display.
    }
}