-3

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); 
Alex Netkachov
  • 13,172
  • 6
  • 53
  • 85

3 Answers3

1

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/

Afsar
  • 3,104
  • 2
  • 25
  • 35
  • My problem is how r u going to differentiate firstname and last name.. Right now i have different text boxes for first name and last name.. So there is no problem .. I want to provide only one text box like full name.. – Shrinidhi Kulkarni Aug 14 '14 at 07:08
  • @ShrinidhiKulkarni,I can get the output that matches the input from user(One input box) against the database . Why you want to check first name or last name , where all matters is getting the matched record. see Query Modified – Afsar Aug 14 '14 at 07:16
  • ok.. I got it.. Your solution seems correct. Can u tell me the exact civicrm file where I have to make changes? Thank u for the solution by the by – Shrinidhi Kulkarni Aug 14 '14 at 07:23
1

You should use a query like this one:

SELECT CONCAT(firstname, lastname) AS fullname FROM tablename WHERE fullname LIKE '%input%';

So you can find both firstname and lastname with one input field.

Enjoy your code!

Magicianred
  • 566
  • 2
  • 8
0

You can check for a whitesapce in the input value and extract the first name and last name using the position of the whitespace. If there is no whitespace in the input value you should call the API using first name only.

kartik
  • 583
  • 8
  • 24