Jump to content
Larry Ullman's Book Forums

Cart.Html


Recommended Posts

Hi All,

 

Thank you in advance for your help. I am having a little bit of difficulty understanding cart.php and cart.html.

 

First of all, if you look at the line in cart.html below, you will see name="quantity[' . $row['sku'] . ']". I don't understand that quantity's variable can be sku.

 

 

<td align="center"><input type="text" name="quantity[' . $row['sku'] . ']" value="' . $row['quantity'] . '" size="2" class="small" /></td>

 

 

 

 

 

 

Also, if you look at down below which is from cart.php, how could $_POST['quantity'] have an array of $sku => $qty? Does it related to name="quantity[' . $row['sku'] . ']"? If so, how can you make it related to $_POST['quantity']? I don't understand that you can get the value right away just because that input's name has quantity[sku].

 

elseif (isset($_POST['quantity'])) { // Update quantities in the cart.

 

// Loop through each item:

foreach ($_POST['quantity'] as $sku => $qty) {

 

// Parse the SKU:

list($sp_type, $pid) = parse_sku($sku);

 

if (isset($sp_type, $pid)) {

 

// Determine the quantity:

$qty = (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 0)) !== false) ? $qty : 1;

 

// Update the quantity in the cart:

$r = mysqli_query($dbc, "CALL update_cart('$uid', '$sp_type', $pid, $qty)");

 

 

Thanks for your help. forgive me for the lack of understanding this book and look forward to hearing from you soon.

Link to comment
Share on other sites

First of all, if you look at the line in cart.html below, you will see name="quantity[' . $row['sku'] . ']". I don't understand that quantity's variable can be sku.

 

It's not, what you're doing here is creating an array of products where the key is the SKU and the value is the quantity. In HTML if you specify a name appended with [] it produces an array when submitted to the server. Hence this line:

 

foreach ($_POST['quantity'] as $sku => $qty) {

 

I don't understand that you can get the value right away just because that input's name has quantity[sku].

 

It doesn't the name of the array is quantity and the key is the value of sku not actually 'sku' as a string.

 

Not sure how clear I've made that so here's what the POST array might look like on submission for these products:

 

Toy Car

SKU: 14

Quantity: 5

 

Boomerange

SKU: 12

Quantity: 1

 

POST array would look like:

 

$_POST['quantity'][14] = 5;

$_POST['quantity'][12] = 1;

 

So when you loop through the key identifies the product by SKU and the value represents the quantity of that product.

 

Hope that makes things clear.

Link to comment
Share on other sites

 Share

×
×
  • Create New...