0

I have ONE input text field and the user can enter a first name, last name, id, email or phone number and it will get that the rest of the users information. However when a user tries to enter a first and last name like Jerry Smith (and this has been happening alot) I get a null value.

What can I do so if the user enters both a first and last name it will return the person and not null.

Here is my code....

            //$id = 1234;
            //$phone = '555-555-5555';
            //$email = '[email protected]';
            //$firstname = 'Jerry';
            //$lastname = 'Smith';
              $fullname = 'Jerry Smith';

            if($id || $phone || $email || $firstname || $lastname) {
                    $data = get_user_info($id, $phone, $email, $firstname, $lastname);
            }

            function get_user_info($id = null, $phone = null, $email = null, $firstname = null, $lastname = null){
            if($id) {
                    $this->db->or_where('ID', $id);
            }
            if($phone) {
                    $this->db->or_where('HomePhone', $phone);
            }
            if($email) {
                    $this->db->or_where('HomeEmail', $email);
            }
            if($firstname) {
                    $this->db->or_where('FirstName', $firstname);
            }
            if($lastname) {
                    $this->db->or_where('LastName', $lastname);
            }
            $query = $this->db->get($this->my_table);
            $member = $query->result_array();
            return $member;
    }
4
  • I'm assuming you have but first I have to ask, have you tried testing this with data that you know exists? A valid first and last name? Commented May 23, 2013 at 21:22
  • i assume you get your values with input class post method so try comparing like; $id !== FALSE Commented May 23, 2013 at 21:35
  • @rcpayan that won't work because the input is a string. So if he wants to compare 'John Smith' against the first name, unless the first name is 'John Smith' he will always get NULL. He needs to break up the input. Commented May 23, 2013 at 23:13
  • but he said he gets first name and surname from form input seperately that's why i offered thats Commented May 24, 2013 at 6:38

2 Answers 2

1
function get_user_info($id = null, $phone = null, $email = null, $firstname = null, $lastname = null)
{
     $this->db->select('*');
     $this->db->where('ID',$id);
     $this->db->or_where('HomePhone',$phone);
     $this->db->or_where('HomeEmail',$email);
     $this->db->or_where('FirstName',$firstname);
     $this->db->or_where('LastName',$lastname);
     $query = $this->db->get($this->my_table);
     $member = $query->result_array();
     return $member;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Why not just split up the input text by space using explode() and moving your if checks into a foreach loop? Example:

$input = explode(' ', $input_field);

foreach ($input as $i) {
    if ($i == 'first_name') //do something
    if ($i == 'last_name') //do something
}

Right now you're just checking whatever the user inputs so the odds of getting NULL are high.

As a side note you should probably validate the input a bit more before you run it against the database. Even though CodeIgniter will automatically escape your queries for you, it's a good practice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.