Welcome Guest, Not a member yet? Register   Sign In
How to search through a database and join the result with another table
#1

I have a database with three tables. I want to search data which matches the submitted data from a form in one table and join the result with data from another table. My problem is that my search function seems not to be working. The join function is not workin because I get the error
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: names

Filename: views/contacts.php

Line Number: 63
the missing names index is from the offices table that I am trying to join with the phones table. I know my problem has to do with the syntax but I am not sure what is the problem. Here is my code

Code from the PhonesController
Code:
//function to search matching contacts
   public function searchContacts(){
       echo 'searching contacts';
       //select data from the database
       $data['phonenumbers'] = $this->contacts_model->search_contacts($this->input->post('search'));
       $data['offices'] = $this->contacts_model->get_offices();
       $departments['departments'] = $this->contacts_model->get_departments();
       
        $this->load->view('templates/header', $data);
        $this->load->view('contacts.php', $departments);
        $this->load->view('templates/footer');
       
   }

Here is the code from the contacts_model
Code:
//this function gets all the contacts given a cerain parameter
   public function search_contacts($param) {
       
       $this->load->library('form_validation');
       
       $this->form_validation->set_rules('$param', 'Search', 'alpha|numeric');
       
       //if($this->form_validation->run() === TRUE)
      // {
           //query
           $this->db->select('users,numbers');
           $this->db->from('phones');
           $this->db->like('users',$param);
           $this->db->or_like('numbers', $param);
           $this->db->join('offices', 'phones.offices = offices.id', 'left');
           
           //return the result
           $query = $this->db->get();

           return $query->result_array();
      // }
     
       
   }

I also want my search to return data depending on the matches,like a person might decide to enter a complete or incomplete name on the search field or number, I want my function to return the matching result. I am not sure on how to get around that. Any help will be gladly appreciated
Reply
#2

In your like statement you have to add the depending table like (as e.g.)

PHP Code:
$this->db->like('offices.users',$param);
$this->db->or_like('phones.numbers'$param); 

Reply
#3

it is now working but is returning everything in my database no matter what I type. Here is my updated code in my contacts_model
Code:
//this function gets all the contacts given a cerain parameter
    public function search_contacts($param) {
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('$param', 'Search', 'alpha|numeric');
        
        //if($this->form_validation->run() === TRUE)
       // {
            //query
            $this->db->select('users,numbers');
            $this->db->from('phones');
            $this->db->like('phones.users',$param);
            $this->db->or_like('phones.numbers', $param);
            $this->db->join('offices', 'phones.offices = offices.id', 'left');
            
            //return the result
            $query = $this->db->get();

            return $query->result_array();
       // }
      
        
    }
Reply




Theme © iAndrew 2016 - Forum software by © MyBB