开发者

Creating an order system - have a problem where only one of many possible items is being added to order

开发者 https://www.devze.com 2023-03-28 14:52 出处:网络
I am having a problem with a script that controls an order system that i am creating. The system contains a search feature that queries a user supplied string against products held in a mysql table,

I am having a problem with a script that controls an order system that i am creating.

The system contains a search feature that queries a user supplied string against products held in a mysql table, if the string matches part of an items description, then that item is returned as a result.

Now, The problem is this.

If a user searches a string that returns say 5 results.

Then the user decides they would like to add 3 of one result and 2 of another result.

They then click on order, which submits the form.

Here is where the problem appears, as only the first result that the user has entered a quantity for is added to the order.

The order should contain two items, one with a quantity of 3 and another with a quantity of 2. It is not doing that and i have no idea what is going wrong here.

Here is the code that controls 开发者_如何学Gothe instance of the user clicking on the order button:

if (!isset($_SESSION['order']))
{
    $_SESSION['order'] = array();
}

if (!isset($_SESSION['quantity']))
{
    $_SESSION['quantity'] = array();
}

$productQuantities=$_POST['quantity'];
if (isset($_POST['action']) and $_POST['action'] == 'Order' and $productQuantities > 0)
{
foreach($productQuantities as $productId=>$quantityS)
{
        if ($quantityS > 0) 
            {
            $_SESSION['order']["$productId"] = $productId;
            $_SESSION['quantity']["$productId"] = $quantityS;
            header('Location: .');
            exit();
            }
}
}

Can anyone see where I am making a mistake in this?

Any input would be greatly appreciated, and I will supply any additional code on request.

Thanks!


You create a loop, but in the first iteration of that loop with a quantity, you send a Location header and exit the script execution. Clearly no more products can be added to $_SESSION once you terminate the script and send the visitor to another page.

Don't prematurely end the loop, and only send a (proper!) location header after you've finished.

0

精彩评论

暂无评论...
验证码 换一张
取 消