0

I am trying to create a function that searches the database for stores that are located within the postcode entered by the user in a form by matching the first few letters for example, SW1. So basically:

  1. User inputs postcode in text box.
  2. User submits entry.
  3. Function searches the database for stores that have the same postcode
  4. Displays results in table.

So far what i have creates a form where the user can enter their postcode, and then an incomplete function that selects the id, name, address etc WHERE the 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'));




}

How can i display the selected data into a table?

This code works. It gets the data from the database and displays it in a table:

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

}
  • How can i display the results into a table? –  Aug 13 '13 at 09:41
  • `fetch...` + `while` loop? I'm not a PHP specialist so someone else will probably come with the exact code needed. But that's the spirit. While waiting for answers, try to search google on those terms: "php fetch while loop"... – Sylvain Leroux Aug 13 '13 at 09:42
  • I'm not an expert myself, PHP is completely new to me. But thank you for your suggestion –  Aug 13 '13 at 09:45

1 Answers1

0

You don't mention which framework or db library you're using but pseudo code would be:

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.
    }
}
Jim
  • 22,354
  • 6
  • 52
  • 80