PHP Array and HTML checkboxes
I have a string saved in a MySQL databa开发者_如何学Pythonse, the string is as follows:
item:19.99/item2:24.99
I've managed to split array to this:
Array(
[0] => Array
(
[0] => item
[1] => 19.99
)
[1] => Array
(
[0] => item2
[1] => 24.99
))
I need to loop through the array, displaying all items from a MySQL table called 'categories' (where 'item' is a category) but the tricky bit is making the checkboxes in the array selected followed by a textbox containing the price
I knew this was going to be hard to explain and the only way I can make it simpler to understand is by providing the desired HTML output:
<input type="checkbox" value="item" checked="checked" />Item <input type="text" value="19.99"/>
<input type="checkbox" value="item1" />Item 1 <input type="text"/>
<input type="checkbox" value="item2" checked="checked" />Item 2 <input type="text" value="24.99"/>
<input type="checkbox" value="item3" />Item 3 <input type="text"/>
<input type="checkbox" value="item4" />Item 4 <input type="text"/>
First you should restructure your array of "selected items" so that it's easier to work with. See my example. Then, when you loop through all of your items, simply check if there's a price in the selected array.
Example:
// Updated your array of selected item so that the name is the key and the price the value.
//
// For example:
//
// Array(
// [item] => 19.99
// [item2] => 24.99
// )
foreach ( $selected_items as $key => $selected_item )
{
$selected_items[$selected_item[0]] = $selected_item[1];
unset($selected_items[$key]);
}
// ...
// While looping through all of the items from your SQL query:
while ( $item = mysql_fetch_assoc($result) )
{
if ( ! empty($selected_items[$item]) )
echo '<input type="checkbox" value="' . $item['key'] . '" selected="selected" />' . $item['name'] . '<input type="text" value="' . $selected_item[$item['key']] . '" />';
else
echo '<input type="checkbox" value="' . $item['key'] . '" />' . $item['name'] . '<input type="text"/>';
}
PS: You might want to add a name
attribute to the checkboxes and input fields.
Based on your comment below:
Change your foreach
loop to this:
foreach($b as $c)
{
list($key, $value) = explode(':', $c);
$d[$key] = $value;
}
Without your MySQL organization info, this is the best I can get you:
<?php
$array = array(array('item', '19.99'),
array('item1', NULL),
array('item2', '24.99'),
array('item3', NULL),
array('item4', NULL));
foreach ($array as $item) {
echo '<input type="checkbox" value="' . $item[0] . '"' . ($item[1] ? ' checked="checked"' : '') . ' />' . $item[0] . '<input type="text"' . ($item[1] ? ' value="' . $item[1] . '"' : '') . "/><br />\n";
}
?>
Outputs:
<input type="checkbox" value="item" checked="checked" />item<input type="text" value="19.99"/><br />
<input type="checkbox" value="item1" />item1<input type="text"/><br />
<input type="checkbox" value="item2" checked="checked" />item2<input type="text" value="24.99"/><br />
<input type="checkbox" value="item3" />item3<input type="text"/><br />
<input type="checkbox" value="item4" />item4<input type="text"/><br />
http://codepad.org/fGpGCXwm
Hope you can dissect that and make it work for you.
Would something like this work?
<?php
foreach($array as $values){
echo '<input type="checkbox" value="item" checked="checked" />' . $values[0] . ' <input type="text" value="' . $values[1] . '"/>'
}
?>
I'm not sure how you know if a value is checked or not but once you have that in a variable or as part of the the array just use an if statement to output
checked="checked"
if neccessary.
I'm not sure what kind of backend you're using, but a better way to store data in a database is not to combine multiple types of data in the same field, and to use numeric ID's instead of text-based ID's. You could create a table like:
id name price
----------------------------------
1 Item 19.99
2 Item 1 0
3 Item 2 24.99
4 Item 3 0
5 Item 4 0
Note that my example shows "0" where no price has been entered. This is because you'd probably want the price column to be stored as a decimal number (with two decimal places), which would make an empty value would default to "0". That's okay. Your user interface doesn't have to display the "0".
Then when you retrieve the data from the database, you might end up with an array like this:
Array(
[0] => Array
(
[id] => 1
[name] => Item
[price] => 19.99
)
[1] => Array
[id] => 2
[name] => Item 1
[price] => 0
)
)
and so on...
So then you could display the values in your .php like the following:
<?php
foreach ($data as $row) {
explode($row);
if ($price > 0) {
$checked = 'checked="checked"';
} else {
$checked = '';
$price = '';
}
$pattern = '<input type="checkbox" name="categories[%s]" value="1" %s> '
. '%s <input type="text" name="prices[%s]" value="%s">';
echo sprintf($pattern, $id, $checked, $name, $id, $price);
}
?>
Then when retrieving form input, the variables you want are $_POST['categories']
and $_POST['prices']
.
精彩评论