I am currently working on a VERY simple search feature for my website. The user types in their search query and then they are taken to a page that displays the results. In my case, the users are search for other users. My problem is, how do I separate a string into two variables?
Example Query:
Matt Morris
I would like my PHP to separate the string where the space occurs. How can I do that?
<html>
<form method="get" action="results.php">
<input type="text" name="search"/>
</form>
</html>
<?php
// code to explode the string into $fname and $lname
if there is a space present
$fname= Matt
$lname= Morris
if (isset($fname) && isset($lname)) {
$search_query= "SELECT * FROM users WHERE fname LIKE %'".$fname."'% AND lname LIKE
%'".$lname."'%";
// code to run the query and display results
}
else if (isset($fname)) {
$search_query= "SELECT * FROM users WHERE fname LIKE %'".$fname."'%";
// code to run the query and display results
}
else if (isset($lname)) {
$search_query= "SELECT * FROM users WHERE lname LIKE %'".$lname."'%";
// code to run the query and display results
}
?>
Thanks
You could do it like this: