0

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

  1. 用户在文本框中输入邮政编码。
  2. 用户提交条目。
  3. 函数在数据库中搜索具有相同邮政编码的商店
  4. 在表格中显示结果。

到目前为止,我创建了一个表单,用户可以在其中输入他们的邮政编码,然后是一个不完整的函数,用于选择邮政编码覆盖的 id、姓名、地址等。

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);

}
4

1 回答 1

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.
    }
}
于 2013-08-13T09:48:49.790 回答