I have a string saved in a MySQL database, 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:
PS: You might want to add a
nameattribute to the checkboxes and input fields.Based on your comment below:
Change your
foreachloop to this: