0

I am trying to add on some checkboxes with a form that is already made for adding/editing products.

I have been able to add on radio buttons in the past. To do this I have used a switch like so:

if (!isset($pInfo->color_id)) 
    $pInfo->color_id = '1';
switch ($pInfo->color_id) {
    case '1': $green_color_id = true;
        break;
    case '2': $turquoise_color_id = true;
        break;
    default: $green_color_id = true;
}

However now, since I want to use checkboxes I don't think I can use a switch like that, since I want multiple colors to be picked and each one of them inserted into the database as a separate field.

Here is my php/html for creating the checkbox:

 <tr>
     <td class="main">Colors</td>
     <td class="main">
         <?php echo '&nbsp;Green' . tep_draw_checkbox_field('color_id', '1', $green_color_id) . '&nbsp;Turquoise' . tep_draw_checkbox_field('color_id', '2', $turquoise_color_id); 
         ?>
     </td>
 </tr>

this is my query for actually inserting it in the database:

if (!isset($HTTP_GET_VARS['color_id'])) {
    tep_db_query("insert into " . TABLE_PRODUCTS_TO_COLORS . " (products_id, color_id) values ('" . (int)$products_id . "', '" . (int)$color_id . "')");
}
else {
    mysql_query("update " . TABLE_PRODUCTS_TO_COLORS . " SET  color_id = '" . (int)$color_id . "' WHERE products_id ='" . (int)$products_id . "'");
}     

this is how the custom function tep_draw_checkbox_field is defined:

function tep_draw_checkbox_field($name, $value = '', $checked = false, $compare = '') {
    return tep_draw_selection_field($name, 'checkbox', $value, $checked, $compare);
}

I'm pretty sure I will need to store the selected values into an array, and then change the query to use the array. I'm not sure how to get started or if that is even right though. Any help is vastly appreciated!

4
  • Note: $HTTP_GET_VARS is deprecated since PHP 4.1 and shouldn't be used anymore. Use $_GET instead. Commented Nov 18, 2011 at 19:00
  • @MarcB I realized that but it is used everywhere in oscommerce. Should I just start switching to $_Get? Commented Nov 18, 2011 at 19:09
  • 1
    Yes, switch. Deprecated features can vanish at any time from the next PHP release. They're not particularly LIKELY to, but why play chicken when you don't have to? Commented Nov 18, 2011 at 19:11
  • Thanks I will start changing over. Commented Nov 18, 2011 at 19:11

1 Answer 1

1

1) Generation of checkboxes so they will be sent as array:

Note the first argument of tep_draw_checkbox_field() (brackets)

<table>
    <tr>
        <td class="main">Colors</td>
        <td class="main">
         <?php echo '&nbsp;Green' . tep_draw_checkbox_field('color_id[]', 1, $green_color_id) . '
                     &nbsp;Turquoise' . tep_draw_checkbox_field('color_id[]', 2, $turquoise_color_id); ?></td>
    </tr>
</table>

2) Regarding the switch: Make sure, $pInfo->color_id is an array. Instead using a switch, use in_array():

<?php
if (!isset($pInfo->color_id)) $pInfo->color_id = Array('1');
if (in_array('1', $pInfo->color_id)) { $green_color_id = true; }
if (in_array('2', $pInfo->color_id)) { $turquoise_color_id = true; }
    ?>

It would be easier though, if you used in_array() in the tep_draw_checkbox_field() as 3rd argument instead, that would save you definining variables for each color:

<table>
    <tr>
        <td class="main">Colors</td>
        <td class="main">
         <?php echo '&nbsp;Green' . tep_draw_checkbox_field('color_id[]', 1, in_array('1', $pInfo->color_id)) . '
                     &nbsp;Turquoise' . tep_draw_checkbox_field('color_id[]', 2, in_array('2', $pInfo->color_id)); ?></td>
    </tr>
</table>

3) Finally, inserting into the database: $HTTP_GET_VARS['color_id'] (better: use $_GET['color_id']) is now an array.

<?php
// First, delete all n entries of colors-to-products
tep_db_query("delete from " . TABLE_PRODUCTS_TO_COLORS . " WHERE products_id = '" . (int)$products_id . "'");

     // now insert colors from form-request
if (is_array($_GET['color_id'])) {
         foreach ($_GET['color_id'] as $colorid) {
                     tep_db_query("insert into " . TABLE_PRODUCTS_TO_COLORS . " (products_id, color_id) values ('" . (int)$products_id . "', '" . (int)$colorid . "')");
         }
}

?>
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help! I am getting an error on the page: Warning: in_array() expects parameter 2 to be array I am guessing because my $pInfo->color_id is not in an array? But it is so I am confused!
Its actually working with the first checkbox 1 but not the second.
Sure! Here is the whole php document. pastebin.com/eLTkVxX8 The query is at line 252 The chackbox html/php is at line 610
on line 233 you are trying to insert a single value as color_id, looking at your question you have a table which stores multiple color_id values (getting this from the array), which you do on line 252. remove the color_id from the query on line 233 and remove line 223 (this destroys the array you get from the form)
I tried this along with some more fiddling and I can't seem to get the value to enter the database. The best I got so far is that the correct product ID is entered into the database but just 0 for color_id. Here is the code again if you don't mind giving it another look pastebin.com/pjvuGUkH
|

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.