Search code examples
phpcivicrm

search either by first name or last name


I have two text boxes for first name and last name. Admin can search records either by first name or last name from respective text boxes. I make an api calls sending first name or last name to fetch records.

What I want is to provide just one textbox. Here admin can type either firstname or last name or full name. How can I sort out these names and make an api call. if user has typed only first name then i send only first name in api call. If he has provided both name I send both names. I first check in which textbox he has typed and accordingly I make api calls

$params = array( 
    'return' => array(
        'first_name',
        'display_name',
        'image_URL',
        'last_name',
        'phone',
        'email'
    ),
    'version' => 3,
    'sequential' => 1,
    'limit' => 25,
    'first_name' = "Shrinidhi",
    'last_name' = "Kulkarni"
);

$contact = civicrm_api('Contact', 'get', $params); 

Solution

  • Try this:

    $sql = "SELECT 'first_name','display_name','image_URL','last_name','phone','email' from Contact where 'shrinidhi' in (first_name,last_name)";
    CRM_Core_DAO::executeQuery($sql);
    

    For further reference: http://www.vedaconsulting.co.uk/civicrm/civicrm-executing-custom-sql/