2

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"/>
2
  • What is the price for item3 and where is it stored? And where are the other items names stored? Commented Jun 27, 2011 at 22:55
  • Can we have some of the format/data from your database? The categories stuff? Commented Jun 27, 2011 at 22:55

4 Answers 4

1

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yea, what Francois said (and typed much faster than I could). +1 :)
I know this is amateurish but how can I restructure the array, at the moment I have this: $b = explode("/", $a['cat']); foreach($b as $c){ $d[] = explode(":",$c); } Where $a['cat'] is the string
@Lee Price - My updated answer has a new foreach loop for you to use which will do what the foreach loop in my original answer does (i.e. item_name => price). From there, when you loop through the rest of your items, you can check if it exists in the $d array as in my example.
you legend! I didn't do it exactly as you suggested, I used array_key_exists() instead but i got it working thanks to your restructure of the array, so I'm gonna give you all the credit on this one :) thanks again
@Lee Price - I'm glad I could help. :)
1

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.

2 Comments

Thanks for this, but this method is not for me as the items are in a different table to the checked items
If you could provide some structure of your database then I could make those changes.
0

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.

Comments

0

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'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.