Re: Check Boxes
| From: | Chris Dowell | Date: | Fri, 10 Sep 2004 18:16:03 +0000 |
| Subject: | Re: Check Boxes | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196327@lists.php.net to get a copy of this message | ||
Try something along these lines:
...
Date: <input type="checkbox" name="columns[]" value="date" />
Time: <input type="checkbox" name="columns[]" value="time" />
Cost: <input type="checkbox" name="columns[]" value="cost" />
Colour: <input type="checkbox" name="columns[]" value="colour" />
...
<?php
...
mysql_query("SELECT " . implode(',', $_REQUEST['columns']) . " FROM tablename");
...
?>
This should product a query like
SELECT date,time,colour FROM tablename;
And it's a lot neater than writing a huge chunk of code to generate your column list when a little judicious use of built in functions can do it all for you.
Cheers
Chris
Gryffyn, Trevor wrote:
You got it right, except you're going to have it blow up on you if someone doesn't select something Try these changes: $a1 = $_POST['ch1']; $a2 = $_POST['ch2']; $a3 = $_POST['ch3']; # Add this bit If (isset($_POST['ch1')) { $collist .= ",$a1"; } If (isset($_POST['ch2')) { if ($collist == "") { $collist = $a2; } else { $collist .= ",$a2"; } }If (isset($_POST['ch3')) { if ($collist == "") { $collist = $a3; } else { $collist .= ",$a3"; } } # Make slight changes here if ($collist <> "") { $query = "SELECT $collist FROM form"; $result=@mysql_query ($query); } Enter_Date<input type="checkbox" name="ch1" value="Enter_Date"> Opening_Units<input type="checkbox" name="ch2" value="Opening_Units"> Unit_Consumed<input type="checkbox" name="ch3" value="Unit_Consumed">-----Original Message----- From: balwantsingh [mailto:balwantsingh@indoasian.com] Sent: Wednesday, August 18, 2004 5:33 AM To: php-general@lists.php.net Subject: [PHP] [PHP-GEN] Check Boxes balwantsingh wrote:can somebody advise me i want to use checkboxes on my website, i want that if userselects somecheckboxes (there will be more than 20 checkboxes),checkbox's value will bestored in variables and than SELECT query command will berun using thesevariables through PHP. but my problem is that in SELECTquery command aftereach column name comma (,) is required and if i use thesame than it isdisplaying "You have an error in your SQL syntax near'FROM form' at line1"i am using following coding $a1 = $_POST['ch1']; $a2 = $_POST['ch2']; $a3 = $_POST['ch3']; if ($a1 or $a2 or $a3) { $query = "SELECT $a1, $a2, $a3 FROM form"; $result=@mysql_query ($query); } Enter_Date<input type="checkbox" name="ch1" value="Enter_Date"> Opening_Units<input type="checkbox" name="ch2" value="Opening_Units"> Unit_Consumed<input type="checkbox" name="ch3" value="Unit_Consumed"> balwant -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php