Jump to content

**SOLVED** Foreach loop problem


RobOgden

Recommended Posts

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:

[code]

<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" />
[/code]
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


[code]
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();
}
[/code]

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

[code]
<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" />
[/code]

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
Link to comment
Share on other sites

This

[code]    $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;

    }
[/code]

matches the product and quantity fields and gives-->

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

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

)[/code]

hth
Link to comment
Share on other sites

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

[code]
$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();

[/code]

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.