views:

91

answers:

2

I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that solutions are not limited to this but preferred if possible.

I have a large MYSQL table of widgets and a page that allows the user to search for a specific widget.

Let's say for example the user types the model name for some widget, the search could return 20 different widget models. Each widget model is different, they all have their own widget unit price and widget model name, etc.

Now comes the tricky part (for me anyways), the user should be able to enter a quantity next to a desired widget html text field.

The user then should be able to push an add to cart button that stores the quantity and the model the quantity it's associated with.

(Design note: A MYSQL query is used to return a list of the search results and I use PHP to echo back the rows of widgets found. Upon each iteration I also display the text field where the desired quantity should be entered. ).

My main problem is figuring out how to associate the MYSQL drawn widget name with the quantity entered by the user and store it together in the cart. Could someone point me in the right direction, I'm not sure how to go about tackling this.

I was thinking I might be able to echo the widget model into text field Id , but then how would I determine after post which model had a quantity... This is probably not the best means to do it, even if it was possible.

Thanks

+4  A: 

make the textfield names something like items[uniq_module_id]

<input type="text" name="items[10001]">
<input type="text" name="items[10002]">
<input type="text" name="items[10003]">

then in php

$items = $_POST['items'];
print_r($items);

you will get

[items] => Array
    (
        [uniq_module_id] => quantity
            ...
    )

and a sample

$items = (isset($_POST['items'])) ? $_POST['items'] : array();
if (is_array($items)) {
    foreach ($items as $module_id => $quantity)
    {
        if (intval($quantity) > 0) {
            add2cart($module_id, $quantity);
        }
    }
}
hayato
Simple as that! :)
KOGI
So... let me get this straight... the line$items = $_POST['items'] ;saves all text fields with items[10001], [10002], 3, etc and the associated quantity entered by the user?
yup. $items will be an array with module_id as the key and quantity as the value. and if the textfield was blank the quantity will be blank also.
hayato
A: 

Is there a "add to cart"-button/link at every widget? Generate a button/link that send the needed info (quantity and widget ID) to an add-to-cart.php.

foreach ($rows as $row) {
    // Print widget info
    echo '<input type="text" name="quantity" id="widget_' . $row['widgetID'] . '_qty" value="1">';
    echo "<button onclick=\"javascript: location.href='add-to-cart.php?widgetId=" . $row['widgetID'] . "&quantity=' + document.getElementById('widget_" . $row['widgetId'] . "_qty').value;\" value=\"Add to cart\" />";
}

Or something alike. Using Ajax would maybe be a good solution as well.

Björn
No, I think that would be to much to have an add button at every widget. Seems logical to have 1 add for the entire page. (Just saw Star Trek.. :) Thanks for the input
Fair enough :) However, this approach is very common.
Björn