Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox loop 2

Status
Not open for further replies.

smashing

Programmer
Oct 15, 2002
170
US
I have a form that has group of checboxs for a user to select a color. I have named the checboxes the same to be able to loop thru them:

<input type="checkbox" name="products_item_color[]" value="01">Black
<input type="checkbox" name="products_item_color[]" value="02">Blue

etc.

Then in my script I try to loop thru any checked values grab them into a vraiable, and put it into one field of the db ...
$theitemcolor=$HTTP_POST_VARS['products_item_color'];
for ($i=0; $i < count($theitemcolor); $i++) {
$insert_color.=$theitemcolor[$i];
}



It looks like insert_color has no value and I don't know why not. Please help!!!
 
There are so many things wrong with that code, let's just take a simplified approach.

Try something like:
Code:
//It'd be better to use $POST[], but we'll ignore that
$colors = $HTTP_POST_VARS[ 'products_item_color' ];
// initialize your variables! plus it avoids scoping issues
$insert_color = '';
// foreach is much simpler, more readable and less error prone
foreach ( $colors as $color ) {
   // process on $color
   $insert_color .= $color;
}
// if $insert_color is still empty... 
// ADD OUTPUT INSIDE THE LOOP to figure out what's going on.
 
foreach is easier to use, and probably one of the most widely used PHP functions out there... but it does have it's drawbacks... namely, it makes a copy of the array and loops through that copy, rather than looping through the actual array. This can be both a performance hit for very large arrays, and requires you to be aware of this fact if you plan on modifying the information within the array.

That said... the original posted code is 100% functional... you should definitely initialize the $insert_color variable... but either way, it should work perfectly fine.

To verify, I copied and pasted your code into a new file, and it does work perfectly fine. My guess is that means you're either not setting your form up properly, or you have a variable name typo going on somewhere.
 
Thanks to both of you. I tried with the foreach loop now and immediatly got hit with:
Warning: Invalid argument supplied for foreach() in /... on line 221
Bear in mind that I'm messing around in OsCommerce files which are complicated (for me at least).
Reverting to my own version the only issue I can think of is this:
The data gets inserted later on in the script as such:
$sql_data_array = array('products_quantity' => tep_db_prepare_input($HTTP_POST_VARS['products_quantity']),

'products_model' => tep_db_prepare_input($HTTP_POST_VARS['products_model']),


and within this loop I have added my own as a continuation:
'products_item_color' => "$insert_color",

Finaly it inputs into the db:
tep_db_perform(TABLE_PRODUCTS, $sql_data_array, 'update', "products_id = '" . (int)$products_id . "'");

If I tried giving some value to $insert_color after the loop above such as $insert_color='whatever'; and it does enter the db so I'm at a loss here. Please help. If this works out for me I'll re-use it countless times so its a big headache for me as of now
 
I have a feeling this is one of those cases where we can help you help yourself, more than we can help you.

some debugging code that will help you.

Code:
echo '<pre>';
print_r($HTTP_POST_VARS);

print_r($theitemcolor);

Each inserted in the right places... the foreach() error you received means you didn't send it an array, which means that using that same variable in a for loop isn't going to do anything interesting either.

The output for the first print_r will show you everything you're sending as part of the POST, and the second will show you specifically what you're taking from the POST to the for loop.

Another tool which would greatly assist you is to use Firefox, install the webdeveloper plugin, then under the Forms menu, click display form details... this step would be on the page before you submit.

My best guess is either your checkboxes are outside of the form tags, or that you have a typo, perhaps a case sensitivity issue.
 
Thanks Skyflier.
ok I'm getting there lets not stop please!
Doing what you suggested I sure see everything being sent from the form beside my checkboxes!

This is exactly how I have them in the form (and I checked they are between the form tags:

<table class="main" width="305" border="0" cellspacing="2" cellpadding="0">
<tr>
<td><input type="checkbox" name="products_item_color[]" value="01">Black</td>
<td><input type="checkbox" name="products_item_color[]" value="02">Blue</td>
<td><input type="checkbox" name="products_item_color[]" value="03">Red</td>
</tr>
<tr>
<td><input type="checkbox" name="products_item_color[]" value="04">Green</td>
<td><input type="checkbox" name="products_item_color[]" value="05">Yellow</td>
<td><input type="checkbox" name="products_item_color[]" value="06">White</td>
</tr>
<tr>
<td><input type="checkbox" name="products_item_color[]" value="07">Cyan</td>
<td><input type="checkbox" name="products_item_color[]" value="08">Turquiose</td>
<td><input type="checkbox" name="products_item_color[]" value="09">Pink</td>
</tr>
</table>


Maybe something how the arrays are set up in OsCommerce? I see for example products_description outputs in print_r($HTTP_POST_VARS); as[products_description] => Array taking a look in the form I see they call the text area where you input the description as: products_description[1]
???
 
Ok I'm on to something now.... (silly silly me) When you finish selecting/inserting stuff on the first page you hit a preview button which takes you to page 2 and then confirm takes you to page 3. Of course all stuff is contained in hidden fields on page 2. I now have to figure out how to get the array in there.called. Hold off repliying for now, I'm giving you a star for your help, and if I'll need more of it later I'll sure post again. Thanks for steering me in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top