Jump to content

Two arrays on a form


lional

Recommended Posts

Hi All

I am trying to create a list of all products where we can select the quantity which is a text box and if it is optional which is a checkbox.

I want to be able to assign quantities to all the products at the same time.

Here is my script where I assign quantities.

$query_aa = "SELECT * from supplements WHERE active='1' AND supp_cat = '1' ORDER BY supplement";
$result_aa = mysql_query($query_aa, $conn);
while ($row_aa = mysql_fetch_assoc($result_aa)){
  $supp_id_out = $row_aa["supp_id"];
$supplement_aa_out = $row_aa["supplement"];
$price_aa_out = $row_aa["price"];
$price_aa_out = rtrim($price_aa_out, "0");
      
      print <<<PRICES
      <tr>
            <td valign="top" align="center" width="80">
            <font face="arial" size="2"><input type="text" size="6" name="product[$supp_id_out]" value=""></td>
            <td valign="top" width="430">
            <font face="arial" size="2"> $supplement_aa_out
            </td>
            <td align="left" width="140"><font face="arial" size="2"> R $price_aa_out</td>
            <td valign="top" align="center" width="80">
            <font face="arial" size="2"><input type="checkbox" size="6" name="opt[$supp_id_out]" value="1" checked></td>

 

Here is my receiving code where I want it to receive the arrays and insert it into a mysql table

foreach($_POST['product'] as $key => $value)
{
$qty1 = $value;
$pid = $key;

	// add to the cart session variable
	$_SESSION['cart'][$pid] = $qty1;
	foreach($_POST['opt'] as $opt => $optional)
	{
	$req = $optional;
}


// Insert main details into Remote Users Table
if ($qty1 > 0) {
$query1 = "INSERT INTO client_supps (client_id, supp_id, qty, optional) VALUES ('$client1_out', '$pid', '$qty1', '$req')";
		$result1 = @mysql_query ($query1);
}	

 

Thanks

 

Lional

Link to comment
https://forums.phpfreaks.com/topic/153595-two-arrays-on-a-form/
Share on other sites

The second array opt[$supp_id_out] does not update my table correctly. even if it is unchecked it still makes it a 1.

I need the product[$supp_id_out] to update the quantity in the table which id does but opt[$supp_id_out] does not work correctly.

 

I am not sure if this section

foreach($_POST['product'] as $key => $value)

{

$qty1 = $value;

$pid = $key;

 

// add to the cart session variable

$_SESSION['cart'][$pid] = $qty1;

foreach($_POST['opt'] as $opt => $optional)

{

$req = $optional;

}

 

is correct

 

Link to comment
https://forums.phpfreaks.com/topic/153595-two-arrays-on-a-form/#findComment-807103
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.