Jump to content


Photo

**SOLVED** Foreach loop problem


  • Please log in to reply
2 replies to this topic

#1 RobOgden

RobOgden
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 March 2006 - 03:58 PM

Hi,

I'm having real trouble making sense of strings passed back from a credit card processor to my site via <input> tags.

If the customer buys more than one product, I get the following in the passback:


<input type="hidden" name="product_id" value="2" />
<input type="hidden" name="product_id1" value="1" />
<input type="hidden" name="quantity" value="5" />
<input type="hidden" name="quantity1" value="3" />
and so on.

I need to match 'quantity1' to 'product_id1' etc. and then input them into the MySQL database into a table called 'orders'. Each product order gets its own row, but is linked by the client_id.

I have set up the following 'foreach' loop


foreach($_POST as $key => $value) 
{ 
$title = preg_replace('/[^a-z,A-Z]/','',$key); 
$str = "$title[0]$title[1]$title[2]$title[3]$title[4]$title[5]$title[6]$title[7]$title[8]"; 
$str2 =  "$title[0]$title[1]$title[2]$title[3]$title[4]$title[5]$title[6]$title[7]"; 
if ($str == 'productid') { 
$product_id = $value; 
$num = preg_replace('/[^0-9]/','',$key); 
} elseif ($str2 == 'quantity') { 
$quantity = $value; 
$query4 = "INSERT into orders (client_id, product_id, order_number, quantity, date) VALUES ('$client_id', '$product_id', '$order_number', '$quantity', NOW())"; 
$result4 = @mysql_query ($query4); 

if ($result4) { 
$query5 = "UPDATE products SET quantity_sold = quantity_sold + '$quantity' WHERE product_id = $product_id"; 
$result5 = @mysql_query ($query5); 
} 
} 

} 
mysql_close(); 
} 

This works fine when the <input> tags come in the following order:

<input type="hidden" name="product_id" value="2" />
<input type="hidden" name="quantity" value="5" />
<input type="hidden" name="product_id1" value="1" />
<input type="hidden" name="quantity1" value="3" />

But I don't have control over the order the 3rd party shopping cart sends them in!

Although the quantity seems to work, the problem is that it seems to input the same product_id into all the rows, depending on which product_id is processed first. This also means the quantity in the products row is wrong.

Does anyone have an idea how I can keep the loop but match the relevant 'product_id's with 'quantity's? I'm sure the currently unused $num variable could help.


I'd be really grateful for any help.

All the best,

Rob

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 March 2006 - 08:04 PM

This

    $items = array();
    foreach($_POST as $k => $v) {

            if (strpos($k, 'product_id')!==false)
                         $items[substr($k, 10)]['prod'] = $v;

            elseif (strpos($k, 'quantity')!==false)
                         $items[substr($k, 8)]['qty'] = $v;

    }

matches the product and quantity fields and gives-->

Array
(
    [0] => Array
        (
            [prod] => 2
            [qty] => 5
        )

    [1] => Array
        (
            [prod] => 1
            [qty] => 3
        )

)

hth
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 RobOgden

RobOgden
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 March 2006 - 10:10 AM

Thanks so much Barand. It's fixed! I've ended up with:

$items = array();
    foreach($_POST as $k => $value) {

            if (strpos($k, 'product_id')!==false)
                         $items[substr($k, 10)] = $value;

            elseif (strpos($k, 'quantity')!==false)
                         $items1[substr($k, 8)] = $value;

   }

   
$n = -1;

foreach($items1 as $k => $v) {
$n = $n + 1;
$product_id = $items[$n];
$quantity = $items1[$n];

$query4 = "INSERT into orders (client_id, product_id, order_number, quantity, date) VALUES ('$client_id', '$product_id', '$order_number', '$quantity', NOW())";
$result4 = @mysql_query ($query4);

if ($result4) {
$query5 = "UPDATE products SET quantity_sold = quantity_sold + '$quantity' WHERE product_id = $product_id";
$result5 = @mysql_query ($query5);
}
}
mysql_close();


I had to use the quantity variables as the key, as there was a rogue 'merchant_product_id' which kept getting in.

Seems to work perfectly, whichever order the $_POST variables arrive in.

All the best,

Rob




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users