私は現在、自分のウェブサイトの非常に単純な検索機能に取り組んでいます。ユーザーが検索クエリを入力すると、結果を表示するページが表示されます。私の場合、ユーザーは他のユーザーを検索しています。私の問題は、文字列を 2 つの変数に分割するにはどうすればよいかということです。
クエリの例:
マット・モリス
スペースが発生する文字列を PHP で区切りたいと思います。どうやってやるの?
<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
}
?>
ありがとう