No, no, no -- I'm assuming that searchFor is your textbox, right? You need to explode that first:
PHP Code:
$vars = explode( " ", $_POST['searchFor'] );
Put that at the top of your scrpit -- this will break all of the words that are seperated by spaces in the box into an array, and then you can count the array.
PHP Code:
$sql = "SELECT * FROM article,writers,news,category WHERE 1";
for( $i = 0; $i < count( $vars ); $i++) {
$sql .= "OR `article`.title LIKE '%$_POST['searchfor'][$i]%' ";
$sql .= "OR `article`.contents LIKE '%$_POST['searchfor'][$i]%' ";
$sql .= "OR `news`.title LIKE '%$_POST['searchfor'][$i]%' ";
$sql .= "OR `news`.contents LIKE '%$_POST['searchfor'][$i]%' ";
$sql .= "OR `writers`.name LIKE '%$_POST['searchfor'][$i]%' ";
$sql .= "OR `category`.category LIKE '%$_POST['searchfor'][$i]%' ";
}
If I am correct, that should be what you are looking to do.
I think you are assuming that your user is smart enough to enter all of the data into that field in the correct order -- You'd better replan that one. Users can find bugs/leaks that you never thought possible. =]
Alternatively, you can go with my favorite -- The foreach construct:
PHP Code:
$sql = "SELECT * FROM article,writers,news,category WHERE 1";
foreach( $vars as $key => $value ) {
$sql .= "OR `article`.title LIKE '%$_POST['searchfor'][$value]%' ";
$sql .= "OR `article`.contents LIKE '%$_POST['searchfor'][$value]%' ";
$sql .= "OR `news`.title LIKE '%$_POST['searchfor'][$value]%' ";
$sql .= "OR `news`.contents LIKE '%$_POST['searchfor'][$value]%' ";
$sql .= "OR `writers`.name LIKE '%$_POST['searchfor'][$value]%' ";
$sql .= "OR `category`.category LIKE '%$_POST['searchfor'][$value]%' ";
}
Bookmarks