Jump to content

Creating an array


boblee

Recommended Posts

Hello all, I want to create an array from information posted from checkboxes which are created by a foreach statement. For example information is taken out of the database table full of product numbers and several checkboxes are displayed for every product number, when the user checks multiple boxes and hit submit an array of the products in created. I got the checkbox part working, but I don't how to create the array. Would any one mind filling me in? -Thanks

 

the code that works do far:

$conn = "SELECT product_id FROM `products`";
$result = mysql_query($conn, $query);

while ($row = mysql_fetch_row($result)) {
  $product[] = $row[0];
}

foreach ($product as $y) {
  echo "<input class='checkbox' type='checkbox'  name='$i++' value='$y'>\n" . $y . "</input>\n";
}

Link to comment
https://forums.phpfreaks.com/topic/138447-creating-an-array/
Share on other sites

Why are you creating an extra array to hold the "products"? Also, where is the variable "$i" set?

You can create the checkbox line in the while loop:

<?php
$conn = "SELECT product_id FROM `products`";
$result = mysql_query($conn, $query);

while ($row = mysql_fetch_assoc($result)) {
  echo '<input class="checkbox" type="checkbox" name="product[]" value="' . $row['product_id'] . '">' . $row['product_id'] . "</input>\n";
}
?>

 

Ken

Link to comment
https://forums.phpfreaks.com/topic/138447-creating-an-array/#findComment-723903
Share on other sites

Probably because I don't know what I'm doing ;D As for the the variable "$i", just a typo I guess, I thought I had 'products_id' in there.

 

alright so using that while statement I can create the series of check boxes, but how do I create an array based on the boxes the user has checked? -Thanks

Link to comment
https://forums.phpfreaks.com/topic/138447-creating-an-array/#findComment-724047
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.