Welcome Guest, Not a member yet? Register   Sign In
My join is not working
#1

I have a search field in my form which searches data from a database and displays it on a result page. When I try to search data I get this error.
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: names

Filename: views/searchresults.php

Line Number: 43

I know the data I am passing to the view does not contain the 'names'  field which means the query I am using is not retrieving this value from the database. Here is my database query
Code:
//this function gets all the contacts given a cerain parameter
   public function search_data($param) {
       
           //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->or_like('offices.names', $param);
           $this->db->join('offices', 'phones.offices = offices.id', 'left');
         

           //return the result
           $query = $this->db->get();

           return $query->result_array();
       
   }

The 'names' field is in my office table, thus it must be my join function which is not working. Any help will be greatly appreciated
Reply
#2

$this->db->join('offices', 'offices.id = phones.offices', 'left');
Reply
#3

Your query won't return the field 'names' because you aren't requesting it: "$this->db->select('users,numbers');".
Reply
#4

At least tell us the structure of the tables you're trying to join, in any case a LEFT JOIN will bring you everything from the "left" table and NULL if no coincidence in "joined" table.
Reply
#5

I have three tables the phones table which has these fields(users, numbers, offices and id). The offices field is a foreign key which references the id field in the offices table. The offices table has these fields(names, id and departments). The departments field in the office table is a foreign key which references the id field in the departments table. The departments table has these fields(names, id). I want the query to return the results if the user enters a department or an office or a phone number
Reply
#6

You haven't select your office.names, try this :

PHP Code:
$this->db->select('phones.users, phones.numbers');
           
$this->db->select('offices.names');
           
$this->db->from('phones');
           
$this->db->join('offices''phones.offices = offices.id''left');
           
$this->db->like('phones.users',$param);
           
$this->db->or_like('phones.numbers'$param);
           
$this->db->or_like('offices.names'$param); 

Reply




Theme © iAndrew 2016 - Forum software by © MyBB