Comparing first 4 characters from table and fetching data from database in codeigniter

Viewed 223

I have two different tables as testimonial and pagetitles. While fetching the Pagetitles from database. I need to compare first 4 characters if both matches then I should get the data.

function getpagetitle($id)
{

    $this->db->select('P.*,T.testimonial_name');        
    $this->db->from('pagetitle AS P');      
    $this->db->join('testimonials AS T','SUBSTR(T.testimonial_name, 4) = SUBSTR(P.page_title, 4)','INNER');
    $this->db->where(array('P.page_title'=>$id));       
    $q=$this->db->get();        
    //var_dump($this->db->last_query());
    //print_r($q->num_rows());
    if($q->num_rows()>0)
      {
    $output = $q->result();

   return $output[0];
        }
    else
    {
    return false;
    }
}

Database tables

testimonials:

+----------------+-------------------+-------------+
| testimonial_id |  testimonial_name | client_name |
+----------------+-------------------+-------------+
| 1              |  testimonial      | abc         |
| 2              |  testimonial      | def         |
+----------------+-------------------+-------------+

Pagetitle

+--------------+-------------+
| pagetitle_id | pagetitle   |   
+--------------+-------------+
|  1           | testimonial |
|  2           | career      |
+--------------+-------------+
2 Answers
Related