Jump to content


Photo

searching multiple checkbox


  • Please log in to reply
13 replies to this topic

#1 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 23 September 2006 - 08:05 PM

Hello,

I have written a simple html form with checkboxes, which refer to various services on offer. I have created a MySQL database with a single table listing location names and the services available at each ('1' representing the presence of a service - otherwise NULL if the service is not present).

<form name="myform" action="search.php" method="GET">

<div align="left"><br>
<input type="checkbox" name="option" value="Electricity"> Electricity<br>
<input type="checkbox" name="option" value="Shower"> Shower<br>
<input type="checkbox" name="option" value="Toilet"> Toilet<br>
<input type="checkbox" name="option" value="Refuse-bin"> Refuse-bin<br>
<br>
<br>
<input type="submit" type="Submit">


I need a little help with the php script which will be called when the form is submitted, to return the results. Users of this form will select multiple checkboxes and i need to return the names of locations which match the services selected.

Being new to PHP i think that the script will need to create an array and then perhaps use the ISSET function to confirm which services were selected, but the ISSET function will not work directly with a mysql_query WHERE clause...perhaps i need to assign the chosen options to a variable somewhere first but i'm really not sure. I only have the following at the moment:

$query = mysql_query("SELECT name FROM facilities WHERE 'option' ISSET")
        or die ("Couln't execute query.");


// Perform Query
$result = mysql_query($query);
echo "$result";


At the moment of course it does not work, but if someone could give me a few pointers it would be very much appreciated!




#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 23 September 2006 - 08:56 PM

make the names of the options array

<input type="checkbox" name="option[]" value="Refuse-bin"> Refuse-bin
Tell me the problem, I will try tell you the solution

#3 Destruction

Destruction
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 23 September 2006 - 08:57 PM

A little search goes a long long way.  This topic has come up many times and one of the more fully answered threads is this one:

http://www.phpfreaks...c,107961.0.html

If you still have any questions afterwards please do still ask :)

Hope this helps,

Dest

#4 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 September 2006 - 11:16 AM

wow thanks very much, i will review these posts and get back to you.

Many thanks

#5 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 September 2006 - 02:13 PM

Being new to PHP i am often not sure what search criteria to use - out of interest what did you use to find the above thread?

I have modified my form to take advantage of an Array as suggested by Onlyican :

<form name="myform" action="search.php" method="GET">

<div align="left"><br>

<input type="checkbox" name="option[]" value="1"> Electricity<br>
<input type="checkbox" name="option[]" value="2"> Shower<br>
<input type="checkbox" name="option[]" value="3"> Toilet<br>
<input type="checkbox" name="option[]" value="4"> Refuse-bin<br>

<br>
<input type="submit" type="Submit">
</div>

</form>

Having had a look at the following thread : http://www.phpfreaks...,107961.0.html  the IF Statement posted by HuggieBear looks the most logical to me.

<?php
$user="root";
$host="127.0.0.1";
$password="";
$database = "moor_simple";
$connection = mysql_connect($host,$user,$password)
      or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
      or die ("couldn't select the database");


$sql = "SELECT name FROM moor_facility WHERE id";
if (isset($_GET['option[1]']))(
   $sql .= "AND electricity = IS NOT NULL";
}
if (isset($_GET['option[2]']))(
   $sql .= "AND shower = IS NOT NULL";
}
if (isset($_GET['option[3]']))(
   $sql .= "AND toilet = IS NOT NULL";
}
if (isset($_GET['option[4]']))(
   $sql .= "AND bin = IS NOT NULL";
}
$result = mysql_query($sql);

echo $result <br>;

?>


As yet it doesn't work, however, i think it might be to do with my use of IS NOT NULL. I have read that checkboxes that are not checked do not get put in the array, which seems to complicate matters. Have i understood the correct syntax ?

Thankyou for your support

#6 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 02:26 PM

My suggestion would be to echo $sql before running the query, that way if it looks wrong you'll know it's the php as opposed to the DB.

In this instance you'd have seen your query isn't as you'd expect it, you're not referencing the variables inside the array correctly...

change:
$_GET['option[1]']


to:
$_GET['option'][1]


Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#7 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 September 2006 - 03:29 PM

Thanks Huggie! Can i just confirm that the following php will work for a mysql table like the following:

[tr][td]
id name     electricity           toilet                      shower               bin
1     a 1               NULL                      NULL                  1
2     b 1               1                            1                        1
3     c 1               NULL                      1                        NULL
4     d NULL                     NULL                        NULL                  1


sorry the columns don't line up very well - i hope you can make sense of it!



$sql = "SELECT name FROM moor_facility WHERE id";
if (isset($_GET['option'][1]))(
   $sql .= "AND electricity = IS NOT NULL";
}
if (isset($_GET['option'][2]))(
   $sql .= "AND shower = IS NOT NULL";
}
if (isset($_GET['option'][3]))(
   $sql .= "AND toilet = IS NOT NULL";
}
if (isset($_GET['option'][4]))(
   $sql .= "AND bin = IS NOT NULL";
}

echo $sql <br>;

$result = mysql_query($sql);

echo $result <br>;

...on another note the "echo $..." doesn't display anything - which i guess indicates that the SQL is wrong?

Cheers

#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 03:38 PM

Try this:

<?php
$sql = "SELECT name FROM moor_facility WHERE id";
if (isset($_GET['option'][1]))(
   $sql .= " AND electricity IS NOT NULL"; // I have added spaces in front of each of these values and removed the equals '='
}
if (isset($_GET['option'][2]))(
   $sql .= " AND shower IS NOT NULL"; // added a space, removed =
}
if (isset($_GET['option'][3]))(
   $sql .= " AND toilet IS NOT NULL"; // added a space, removed =
}
if (isset($_GET['option'][4]))(
   $sql .= " AND bin IS NOT NULL"; // added a space, removed =
}

echo "$sql<br>\n"; // quoted the echo string and added a newline character

$result = mysql_query($sql);

echo "$result<br>\n"; // quoted the echo string and added a newline character
?>

You don't use = with IS NOT NULL just the column name

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#9 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 September 2006 - 04:12 PM

Thanks for your answers Huggie!

Unfortunately it still wont display anything. In fact i put a little text in the HTML before your <?php  ?> and this wont display either. However, if i comment out the php then it is clearly displayed at the top of the screen.

Do you think this is something todo with why my results wont display?


Again many thanks for your time.

#10 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 04:37 PM

In that case it would appear something else before hand is falling over.

Can you provide me with the whole code for the page that you have.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#11 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 September 2006 - 04:54 PM

The HTML form:

<html>
<head>
<title>Mooring Facilities Search Form</title>
</head>
<body>
Please select the facilities you would like at a mooring:
<form name="myform" action="search.php" method="GET">
<div align="left"><br>

<input type="checkbox" name="option[]" value="1"> Electricity<br>
<input type="checkbox" name="option[]" value="2"> Shower<br>
<input type="checkbox" name="option[]" value="3"> Toilet<br>
<input type="checkbox" name="option[]" value="4"> Refuse-bin<br><br><br>
<input type="submit" type="Submit">

</div>
</form>

</body>
</html>

and the PHP script:

<?php
$user="root";
$host="127.0.0.1";
$password="";
$database = "moor_simple";
$connection = mysql_connect($host,$user,$password)
      or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
      or die ("couldn't select the database");
?>

<html>
<head>
<title>search database for matching records</title>
</head>
<body>
The Search Results are:

<?php
$sql = "SELECT name FROM moor_facility WHERE id";
if (isset($_GET['option[1]'])(
   $sql .= " AND electricity IS NOT NULL"; // I have added spaces in front of each of these values and removed the equals '='
}
if (isset($_GET['option'][2]))(
   $sql .= " AND toilet IS NOT NULL"; // added a space, removed =
}
if (isset($_GET['option'][3]))(
   $sql .= " AND shower IS NOT NULL"; // added a space, removed =
}
if (isset($_GET['option'][4]))(
   $sql .= " AND bin IS NOT NULL"; // added a space, removed =
}

echo "$sql<br>\n"; // quoted the echo string and added a newline character

$result = mysql_query($sql);

echo "$result<br>\n"; // quoted the echo string and added a newline character
?>

</body>
</html>

Cheers Huggie!

#12 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 07:20 PM

OK, I've changed the both the HTML in the form and the PHP/SQL in the results page, so give the following a try.  I'd suggest copying and pasting exactly as is, then putting your database password in, it should work for the setup you have as I've kept your table/column names the same:

Form:
<html>
 <head>
  <title>Mooring Facilities Search Form</title>
 </head>
 <body>
  Please select the facilities you would like at a mooring:
  <form name="myform" action="search.php" method="GET">
   <div align="left"><br>
    <input type="checkbox" name="option[]" value="electricity"> Electricity<br>
    <input type="checkbox" name="option[]" value="shower"> Shower<br>
    <input type="checkbox" name="option[]" value="toilet"> Toilet<br>
    <input type="checkbox" name="option[]" value="bin"> Refuse-bin<br><br><br>
    <input type="submit" type="Submit">
   </div>
  </form>
 </body>
</html>

Results Page:
<?php

// Your db connection options
$user="root";
$host="127.0.0.1";
$password="";
$database = "moor_simple";
$connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server");
$db = mysql_select_db($database, $connection) or die ("couldn't select the database");

// Base SQL statement
$sql = "SELECT name FROM moor_facility WHERE id";

// If they've selected options, which ones
if (isset($_GET['option'])){
   $options = $_GET['option'];
   foreach ($options as $opt){
      if ($opt == "electricity"){
         $sql .= " AND electricity IS NOT NULL";
      }
      else if ($opt == "shower"){
         $sql .= " AND shower IS NOT NULL";
      }
      else if ($opt == "toilet"){
         $sql .= " AND toilet IS NOT NULL";
      }
      else if ($opt == "bin"){
         $sql .= " AND bin IS NOT NULL";
      }
   }
}

// Run the query
$result = mysql_query($sql) or die ("couldn't run the query $sql" . mysql_error());

// Echo the top html
echo <<<HTML
<html>
 <head>
  <title>search database for matching records</title>
 </head>
 <body>
  <table width="300" cellpadding="2" cellspacing="0" border="1">
   <tr>
    <td>
     The Search Results are:
    </td>
   </tr>
HTML;

// While there's still rows, loop through them
while($row = mysql_fetch_array($result, MYSQL_ASSOC){
	echo <<<HTML
	<tr>
	 <td>
	  {$row['name']}
	 </td>
	</tr>
HTML;
}

// Echo your footer
echo <<<HTML
  </table>
 </body>
</html>
HTML;
?>

Here's a few changes I made and the reasons for them, read these to better help yourself understand what I did and why...

Change: You had the if statement incorrect, you had normal parenthesis, instead of curly braces, changed to curly's.
Reason: Incorrect syntax.

Change: You had $option[1], $option[2], $option[3] and $option[4], changed these to start at $option[0].
Reason: Array indices start at 0, not 1.

Change: Did away with the whole of the above options and replaced with a foreach loop.
Reason: If someone only submitted refuse-bin on the form, and none of the others, it would actually become $option[0], not the expected $option[4], which meant your SQL statement would be incorrect.

Change: The values on the form itself.
Reason: To help tidy up code and assist with the above foreach loop.

Change: Moved all the HTML into heredoc syntax.
Reason: I believe that it makes it look tidier and easier to read.

Change: Rather than echo $result which would just print Array() it now echos the results from the db.
Reason: You had the syntax not quite right.


Well that's it, I hope it helps, if it doesn't work (as I've coded it on the fly and haven't tested it) then please post back the error you get and I'll correct.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#13 daniish

daniish
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 September 2006 - 09:13 PM

Huggie you're an absolute LEGEND! Thankyou so much


...just one lil error:
Parse error: parse error, unexpected '{' in C:\wamp\www\moorings\search.php on line 52




#14 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 September 2006 - 11:00 PM

Not bad for untested :)

The end of line 52 looks like this:
...MYSQL_ASSOC){


Change it to this:
...MYSQL_ASSOC)
){

Notice the extra closing parenthesis.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users