Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8200967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:38:15+00:00 2026-06-07T06:38:15+00:00

I am using codeigniter , I want to use an IF condition in WHERE

  • 0

I am using codeigniter, I want to use an IF condition in WHERE clause. But it is not executing properly.

$where_string="( a.FirstName LIKE '".$search_phrase."%' OR ( IF(c.PrivacySettingElephantiUser=1,'a.LastName LIKE \'".$search_phrase."%\'  OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')))";
$this->db->where($where_string);

i am doing a search here . by first name , last name and first name concat last name .

i check the value c.PrivacySettingsElephantiUser . if only it is true , i am cheking for last name and first name last name concat value , the thing is if the c.PrivacySettingsElephantiUser=1 ANDc.PrivacySettingsElephantiUser=0,

this part is not executing .

OR ( IF(c.PrivacySettingElephantiUser=1,'a.LastName LIKE \'".$search_phrase."%\' OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')

it is only searching by the first name always , ignoring the if condition ,

How to write this correctly?

Is there an easy way to do this using AND ,OR logic?

UPDATE

this my full query

public function search_people($profile_id,$search_phrase=NULL,$country=NULL,$state=NULL,$city=NULL,$neighborhood=NULL,$type=NULL,$limit=NULL,$offset=NULL)
{
    $this->db->select('SQL_CALC_FOUND_ROWS a.ProfileID,a.FirstName,a.LastName,a.StateName,a.CityName,a.ShowLastName,a.UserType,a.ProfileImg,b.FriendStatus,b.RequesterID,b.AccepterID',FALSE);
    $this->db->from($this->mastables['xxxxxxxx'].' as a');
$this->db->join($this->mastables['yyyyyyyyyyy'].' as b'," b.RequesterID=a.ProfileID AND b.AccepterID=".$profile_id." OR b.AccepterID=a.ProfileID AND b.RequesterID=".$profile_id,'left');


    $this->db->where('a.ProfileID !=',$profile_id);
    $this->db->where('a.UserType',2);

    if($type=="friend")
    {

        $this->db->join($this->mastables['profile_privacy_settings'].' as c'," c.EntityID=a.ProfileID AND c.UserType=2 AND c.ProfilePrivacySettingDefaultID=1 ",'inner');
        $where_string="( a.FirstName LIKE '".$search_phrase."%' OR ( IF(c.PrivacySettingFriend=1,'a.LastName LIKE \'".$search_phrase."%\'  OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')))";
        $this->db->where($where_string);
        $this->db->where('b.FriendStatus',1);
    }
    else if($type=="other")
    {
        $this->db->join($this->mastables['profile_privacy_settings'].' as c'," c.EntityID=a.ProfileID AND c.UserType=2 AND c.ProfilePrivacySettingDefaultID=1 ",'inner');
        $where_string="( a.FirstName LIKE '".$search_phrase."%' OR ( IF(c.PrivacySettingElephantiUser=1,'a.LastName LIKE \'".$search_phrase."%\'  OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\' ','')))";
        $this->db->where($where_string);
        $this->db->where ('(b.FriendStatus IS NULL OR b.FriendStatus=0)');
    } else {    
        $this->db->join($this->mastables['profile_privacy_settings'].' as c'," c.EntityID=a.ProfileID AND c.UserType=2 AND c.ProfilePrivacySettingDefaultID=1 ",'inner');
        $where_string="( a.FirstName LIKE '".$search_phrase."%' OR ( IF(c.PrivacySettingElephantiUser=1 OR c.PrivacySettingFriend=1,'a.LastName LIKE \'".$search_phrase."%\'  OR CONCAT(a.FirstName, \' \', a.LastName) LIKE \'".$search_phrase."%\'','')))";
        //$where_string="( a.FirstName LIKE '".$search_phrase."%' OR ( IF(c.PrivacySettingElephantiUser=1 OR c.PrivacySettingFriend=1,'a.LastName LIKE b%','')))";
        $this->db->where($where_string);
    }

    if($country)
    {
        $this->db->where('a.CountryID',$country);
    }
    if($state)
    {
        $this->db->where('a.StateID',$state);
    }
    if($city)
    {
        $this->db->where('a.CityID',$city);
    }
    if($neighborhood)
    {
        //$this->db->where('',$neighborhood);
    }
    if($limit)
    {
        $this->db->limit($limit,$offset);   
    }

$query = $this->db->get();
//echo $this->db->last_query();die;
$result['result'] = $query->result_array();

$query = $this->db->query('SELECT FOUND_ROWS() AS `Count`');
    $result["totalrows"] = $query->row()->Count;

if(!empty($result))
{
    return $result; 
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:38:18+00:00Added an answer on June 7, 2026 at 6:38 am

    not getting PHP operations just writing sql code.. Use Case statement in your where query

    case when (c.PrivacySettingElephantiUser=1 AND (a.LastName LIKE '%something%' 
    OR CONCAT(a.FirstName, \' \', a.LastName) like '%something%') then 1 else 0 end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using CodeIgniter with version 2.1.0. I want to use Hooks for login
My problem is in using Codeigniter custom library but I think it is not
I am building a Facebook app using iframe rendering. I want to use CodeIgniter
I'm trying to write a sign up form using CodeIgniter and want to use
I'm using Codeigniter not for so long but I've some charset problems.. I'm asking
I'm using CodeIgniter, and will likely use their template library as I want to
I have a very AJAX-ish listing webpage created using Codeigniter where I want registered
I'm using CodeIgniter + Zend libraries. I want to let users upload videos to
I'm creating my own script using the CodeIgniter MVC framework. Now, i want users
I'm using CodeIgniter 2.1 I use CodeIgniter's session to handle whether a user logged

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.