View Full Version : One last PHP/MySql question
boosty
29-09-2003, 04:22 PM
I am trying to put together a couple of pages, the first one a form where users can select options, the second one takes the options and runs a query to insert the options into the database.
Lets say for example they are ordering a car. I have a table called car_options , and it is populated with a unique ID, and the options, for example "Power Steering", "Central Locking", "Cruise Control" etc. This table can be updated as more options become available or are removed.
I can write the first page, I just do a select * from car_options, and use a loop to go through the rows creating a "Post" form with checkboxes for each of the options ie:
input type="checkbox" name="power_steering"
My problem on the next page, accessing the variables.
Normally I would just say if $power_steering == yes etc etc, but because I don't know what the options are going to be, how can I program to check their state?
I hope that makes sense, and someone can help me!
Thanks
SB
SB,
The following snippet is taken from OReilly's "PHP Cookbook" by David Sklar and Adam Trachtenberg (ISBN 1-56592-681-1), a book which I thoroughly recommend. I think you can use it to solve your problem.
HTH,
Scott
Recipe 9.11 Using Form Elements with Multiple Options
9.11.1 Problem
You have a form element with multiple values, such as a checkbox or select element, but PHP sees only one value.
9.11.2 Solution
Place brackets ([ ]) after the variable name:
<input type="checkbox" name="boroughs[]" value="bronx"> The Bronx
<input type="checkbox" name="boroughs[]" value="brooklyn"> Brooklyn
<input type="checkbox" name="boroughs[]" value="manhattan"> Manhattan
<input type="checkbox" name="boroughs[]" value="queens"> Queens
<input type="checkbox" name="boroughs[]" value="statenisland"> Staten Island
Inside your program, treat the variable as an array:
print 'I love ' . join(' and ', $boroughs) . '!';
9.11.3 Discussion
By placing [ ] after the variable name, you tell PHP to treat it as an array instead of a scalar. When it sees another value assigned to that variable, PHP auto-expands the size of the array and places the new value at the end. If the first three boxes in the Solution were checked, it's as if you'd written this code at the top of the script:
$boroughs[ ] = "bronx";
$boroughs[ ] = "brooklyn";
$boroughs[ ] = "manhattan";
You can use this to return information from a database that matches multiple records:
foreach ($_GET['boroughs'] as $b) {
$boroughs[ ] = strtr($dbh->quote($b),array('_' => '\_', '%' => '\%'));
}
$locations = join(',', $boroughs);
$dbh->query("SELECT address FROM locations WHERE borough IN ($locations)");
This syntax also works with multidimensional arrays:
<input type="checkbox" name="population[NY][NYC]" value="8008278">New York...
If checked, this form element sets $population['NY']['NYC'] to 8008278.
Placing a [ ] after a variable's name can cause problems in JavaScript when you try to address your elements. Instead of addressing the element by its name, use the numerical ID. You can also place the element name inside single quotes. Another way is to assign the element an ID, perhaps the name without the [ ], and use that ID instead. Given:
<form>
<input type="checkbox" name="myName[]" value="myValue" id="myName">
</form>
the following three refer to the same form element:
document.forms[0].elements[0]; // using numerical IDs
document.forms[0].elements['myName[ ]']; // using the name with quotes
document.forms[0].elements['myName']; // using ID you assigned
boosty
29-09-2003, 08:16 PM
Thanks so much.
That was more complex than my solution needed to be, but it put me on the right path - arrays.
Took me a little while to understand them, but with the php.net page and a few tutorials I am within sight of it working now.
I will post my solution back when I am done so if anyone else is looking for something similar they can see how it is done.
SB
vBulletin® v3.6.8, Copyright ©2000-2009, Jelsoft Enterprises Ltd.