Results 1 to 4 of 4

Thread: foreach+array=problem...

  1. #1
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question foreach+array=problem...

    Dear all,

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

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

    I then retrieve data from the DB:
    Code:
    $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


    Code:
    <?
    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");
    ?>

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    can you be a bit more clearer on what you want? I get your problem, just not what you want.
    Jeremy | jfein.net

  3. #3
    Join Date
    Mar 2008
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question

    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

  4. #4
    Join Date
    Mar 2008
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Hi Johan,

    Your problem may be here:

    Code:
    " .$assortsize['value_id']['$i']. "
    Try this instead:
    Code:
    " .$assortsize['value_id'][$i]. "
    I hope that helps.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •