PHP: Search properly -


i have right now:

if(isset($_post["search"]) && !empty($_post["search"])) {     $full_name = mysql_real_escape_string($_post["search"]);     $sex = mysql_real_escape_string($_post["sex"]);     list($firstname, $lastname) = array_map('ucfirst', explode(' ', $full_name));     $query = "select firstname, lastname, id, user_name, sex, last_access, bostadsort users (firstname '$firstname' or lastname '$lastname') , sex = '$sex'";      $result1 = mysql_query($query) or die(mysql_error());     $count = mysql_num_rows($result1);     while($get = mysql_fetch_array  ($result1)){         echo $get["firstname"] . " " .$get["lastname"]."<br>";     } } 

this search query. form called "search full names". type in, , splits $firstname , $lastname.

works great, no problems.

although if enter user´s lastname, because maybe dont remember users firstname, code take firstname, because before space firstname (list() line), , puts in $firstname , result nothing, there's no firstname lastname.

how can solve this? if search firstname works fine, full name too, not lastname. smart solution?

if understand correctly, simplest solution:

list($firstname, $lastname) = array_map('ucfirst', explode(' ', $full_name, 2)); if (!$lastname) $lastname = $firstname; 

edit: added limit explode line.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -