Help with Search function please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rosslad
Forum Newbie
Posts: 4
Joined: Mon Feb 11, 2008 3:09 pm

Help with Search function please

Post by rosslad »

~pickle | Please use [ php ], [ code=text ] and [ syntax="..." ] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello all,
This is my first time posting here so please be kind and if I have posted in the wrong forum please let me know.

I have been trying to get this search function working for nearly a week now and I hate to admit it but its got me beat :cry:

I really don't like asking for help as I like figuring things out myself but I must say I am defeated here and could do with a fresh pair of eyes to help me out (there I said it!)

My problem - Using a checkbox array to search a specific mysql table.

It works till the point of if just one checkbox is selected its great, but two or more is causing a problem.

the form:

Code: Select all

<form action="searchlistings.php" method="post" name="category">
<input type="hidden" name="category" value="1" />
<input type="checkbox" name="list[]" value="beaches" />
<input type="checkbox" name="list[]" value="hotels" />
<input type="checkbox" name="list[]" value="parks" />
</form>
the table includes the fields:

Code: Select all

id - location - category - country - state - city - auth
the php:

Code: Select all

if ( $_POST['category'] == 1 ) {
 
    $searchat = category;
 
if ( $_POST['list'] != '' ) {
    $p.="AND ";
}
 
$checks = $_POST['list'];
if( sizeof( $checks ) ) {
    $o.="OR ";
  for( $i = 0; $i < count( $checks ); $i++ ) {
        if (count($checks) > 1) {
 
    $c.="$searchat like '%$checks[$i]%' $o ";
} else { 
    $c.="$searchat like '%$checks[$i]%' ";
}
echo "\$checks[$i]: ". $checks[$i]."<br>\n";
 
}
}
 
    $sql = "SELECT * FROM listings WHERE $c AND auth = 'Yes'";
}
As i think you can see if more than 1 checkbox is selected I am receiving an error regarding the word "OR" just before "AND":

Code: Select all

WHERE category LIKE '%beaches%' OR  category LIKE '%parks%' OR   AND  auth = 'Yes'
Can anyone show me how I can get rid of the last "OR" if no more categories are to be searched?

Please feel free to comment on any code mistakes I have made or Improvements you think I should do (if I am asking for help I may as well go all out :oops: ).

Many many thanks in advance
Ross


~pickle | Please use [ php ], [ code=text ] and [ syntax="..." ] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Help with Search function please

Post by yacahuma »

change your sql

Code: Select all

 
$array2 = array();
foreach($array as $el)
  $array2[] = "'' . $el . "'"; //is there a better way to quote all elements of the array???
$csv = implode(",", $array2);
$sql  = "select * from XXX where  category in ($csv)";
 
Post Reply