Comparing first 4 characters from table and fetching data from database in codeigniter
I have two different ones tables
like testimonial
and pagetitles
. By choosing pagetitles
from database
. I need to compare the first 4 characters, if both are 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
reviews:
+----------------+-------------------+-------------+
| testimonial_id | testimonial_name | client_name |
+----------------+-------------------+-------------+
| 1 | testimonial | abc |
| 2 | testimonial | def |
+----------------+-------------------+-------------+
PAGETITLE
+--------------+-------------+
| pagetitle_id | pagetitle |
+--------------+-------------+
| 1 | testimonial |
| 2 | career |
+--------------+-------------+
+3
source to share
2 answers
I think you should use if then statement in mysql, so if we want to change your query, it should 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 am not testing it, but I think it should be true
+1
source to share
If you don't have solutions yet, please try this below code,
$testimonials = $this->db->query("SELECT `P`.*, `T`.`testimonial_name`
FROM (`pagetitle` P) INNER JOIN `testimonials` T ON
((SUBSTR(`T`.`testimonial_name`, 1, 4)) = (SUBSTR(`P`.`page_title`, 1, 4)))
WHERE `P`.`page_title` = SUBSTR('{$id}', 1, 4)")->result_array();
print_r($testimonials);
/* Output of $testimonials will be like below:
Array
(
[0] => Array
(
[fieldname_1] => val
[fieldname_2] => val
)
[1] => Array
(
[fieldname_1] => val
[fieldname_2] => val
)
)
*/
if(count($testimonials) > 0) {
//$output = $testimonials->result();
return $testimonials[0];
} else {
return false;
}
+1
source to share