Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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      |
+--------------+-------------+
like image 808
user8001297 Avatar asked Feb 01 '26 00:02

user8001297


1 Answers

i think you have to use if then statement in mysql , so if we want to change your query , it must be something like this :

select P.*,T.testimonial_name from pagetitle AS P , testimonials as T (
 select IF 'SUBSTR(T.testimonial_name, 4) = SUBSTR(P.page_title, 4)'  
 where P.page_title>5
)

i dont test it , but i think it must be true

like image 96
Nima habibkhoda Avatar answered Feb 03 '26 13:02

Nima habibkhoda