note 13883 added to function.split.php
| From: | pete at webmatrix dot co dot uk | Date: | Sun, 08 Jul 2001 12:54:40 +0000 |
| Subject: | note 13883 added to function.split.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-15090@lists.php.net to get a copy of this message | ||
I found strstr and strtok before finding split(), but I've manipulated it to make it a viable
alternative. The code below splits a variable $name into two components, and then run's queries
based on the user's input whether they type in the first name, the last name, or both, in any
order, to find search results. This is much more accurate than a normal search I think. $noresult
merely returns 'no results found' on my search. Enjoy!
$token = strtok($name, " ");
$length = strlen($token);
$partone = substr($name,0,$length);
$parttwo = substr($name,$length+1);
echo $partone . "<BR>" . $parttwo ;
$result = mysql_query("SELECT * FROM customers where name LIKE '%$partone%' OR name
LIKE '%$parttwo%' ");
if (strlen($parttwo) == 0) { $result = mysql_query("SELECT * FROM customers where name
LIKE '%$partone%'"); };
if (strlen($name) == 0) { echo $noresult; die; };
echo "Possible Matches Found: " . mysql_num_rows($result) . "<BR>";
if (mysql_num_rows($result) == 0) { echo $noresult; };
while ($row = mysql_fetch_object($result)){
$row++;
http://www.php.net/manual/en/function.split.php