Log in

View Full Version : foreach+array=problem...



Johan Beijar
03-25-2008, 09:38 PM
Dear all,

I have problems with a Foreach-statement...and I hope someone can help me...

I take the
$assortquant = $HTTP_POST_VARS['assortsizequant']; from a form-page.

I then retrieve data from the DB:
$assortsize = mysql_fetch_array($resultassortsize);.


exampel:

assortsizequant assortsize

1 37
2 38
2 39
3 40
2 41
1 42

I would then like to insert this info into the assort_quant_value-table with a foreach-statement and this is where I run into problems...

The quantity (assortsizequant) is inserted correctly into the DB but the assortsize is not. I only get the number 3 into the DB where it should say 37-42.

Can anyone guide me...?

Thank you,

Johan Beijar



<?
include("include/session.php");
error_reporting(E_ALL);
$query = mysql_query ("INSERT INTO Assortment_product (assortment_id, productid, username) values (NULL, " . $_SESSION['prodid'] . ", '$session->username')") or
exit(mysql_error());
$result = mysql_query($query);
//get last inserted ID
$lastassortid=mysql_insert_id();
//Insert quantity of each size per product into value_assortment-table
//get the value_id and them store in a array
$resultprodname = mysql_query("SELECT productname, productid FROM products WHERE productid = {$_SESSION['prodid']} and username='$session->username'");
$prodname = mysql_fetch_array( $resultprodname );
$prodid=$prodname['productid'];
$resultassortsize = mysql_query
("SELECT
attribute_values.value_id
FROM
attribute_values,
product_attributes
WHERE
product_attributes.username = '$session->username' and
attribute_values.value_id = product_attributes.value_id and
product_attributes.productid = ".$prodid." and
attribute_values.attrib_id = 1")
or die(mysql_error());
$assortsize = mysql_fetch_array($resultassortsize);
//Get the quantity from the assortment_add.php-page.
$assortquant = $HTTP_POST_VARS['assortsizequant'];
$i=0;
foreach ($assortquant as $assqt)
{
$assortins=mysql_query("
INSERT INTO assort_quant_value
(quantity, value_id)
values
(
" .$assqt. ",
" .$assortsize['value_id']['$i']. "
)") or
exit(mysql_error());
$i++;
}
header("Location: assortment_add.php");
?>

Nile
03-25-2008, 09:50 PM
can you be a bit more clearer on what you want? I get your problem, just not what you want.

Johan Beijar
03-25-2008, 10:20 PM
Sorry...

I would like:

assortsizequant assortsize

1 | 37
2 | 38
2 | 39
3 | 40
2 | 41
1 | 42

To be inserted instead of

1 | 3
2 | 3
2 | 3
3 | 3
2 | 3
1 | 3

I hope this clarifies it...

Should I use
$assortsize = mysql_fetch_row($resultassortsize);
instead of
$assortsize = mysql_fetch_array($resultassortsize);
?


/Johan

JazzcatCB
03-26-2008, 09:09 PM
Hi Johan,

Your problem may be here:



" .$assortsize['value_id']['$i']. "


Try this instead:


" .$assortsize['value_id'][$i]. "


I hope that helps.