Jump to content



Photo

Checkboxes: Adding Multuple Items To Shopping Cart


  • Please log in to reply
3 replies to this topic

#1 Bill

Bill

    Member

  • Members
  • PipPip
  • 46 posts

Posted 19 April 2015 - 6:42 PM

Hi all!

 

My goal is to enhance the design of the sales program to enable the selection of many items or just one item that can be moved to the shopping cart using checkboxes.

 

Have searched this site and on the net to solve problem in conjuction with studing lines of code in various programs listed in the book to gain clearer understanding.of problem. 

 

Here is list_sales.html with addition of checkbox code line.

<?php 

// This page is included by sales.php.
// This page displays the available sale products.
// This page will make use of the query result $r.
// The query returns an array of: description, image, sku, name, and stock.

// Added later in Chapter 8:
include ('./includes/product_functions.inc.php'); 

?>

<!-- box begin -->
<div class="box alt">
 	<div class="left-top-corner">
    	<div class="right-top-corner">
       	<div class="border-top"></div>
       </div>
    </div>
    <div class="border-left">
    	<div class="border-right">
       	<div class="inner">
			<h2>Current Sale Items</h2>
		
				<form action="http://localhost/larryullman/Sites/ecom_book/ex2.2/html/sales.php" method="post" accept-charset="utf-8">
				<?php 
								
// Loop through each item:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
	
	echo '<h3 id="' . $row['sku'] . '">' . $row['category'] . '::' . $row['name'] .'</h3>
  	<div class="img-box">
     	<p><img alt="' . $row['name'] . '" src="http://localhost/larryullman/Sites/ecom_book/ex2.2/html/products/' . $row['image']  . '" />' . $row['description'] . '<br />' . 
		get_price('goodies', $row['price'], $row['sale_price']) . '
		<strong>Availability:</strong> ' . get_stock_status($row['stock']) . '</p>
    <p><a href="http://localhost/larryullman/Sites/ecom_book/ex2.2/html/cart.php?sku=' . $row['sku'] . '&action=add" class="button">Add to Cart</a></p>
	
   <td align="left"><input type="checkbox" name="item['.$row['sku'].']" id="item[' . $row['sku'] . ']"  size="5" class="button" /> </td>
	 
	 	</div>';
					
}
 
?> 
<div class="field"><input type="submit" value="Add Products" class="button" /></div>	
	</fieldset>
</form> 

          </div>
       </div>
    </div>
    <div class="left-bot-corner">
    	<div class="right-bot-corner">
       	<div class="border-bot"></div>
       </div>
    </div>
 </div>
<!-- box end -->

          </div>
       </div>
    </div>
    <div class="left-bot-corner">
    	<div class="right-bot-corner">
       	<div class="border-bot"></div>
       </div>
    </div>
 </div>
<!-- box end -->

And here is sale.php from from with inclusion of a for loop. 

<?php

// This file is the sales page. 
// It lists every sales item.
// This script is begun in Chapter 8.

// Require the configuration before any PHP code:
require ('./includes/config.inc.php');

// Include the header file:
$page_title = 'Sale Items';
include ('./includes/header.html');

// Require the database connection:
require (MYSQL);

// Invoke the stored procedure:

$r = mysqli_query ($dbc, 'CALL select_sale_items(true)');

if (mysqli_num_rows($r) > 0) {
	include ('./views/list_sales.html');
} else {
	include ('./views/noproducts.html');
}


	// run through items added to cart:
	if (isset($_POST['items']) && is_array($_POST['items'])) {
	
	// Loop through each submitted value:
		foreach ($_POST['items'] as $sku => $qty) {
			
			// Validate the added quantity:
			if (filter_var($qty, FILTER_VALIDATE_INT, array('min_range' => 1))) {

				// Parse the SKU:
				list($type, $id) = parse_sku($sku);
				
						
				$items = (empty($_POST['items'][$sku]))? NULL:
				$_POST['items']['$sku'];
				
				}
				
			} // End of IF.

		} // End of FOREACH.


// Include the footer file:
include ('./includes/footer.html');
?>

Thank you in advance!

 

 

Windows 7

Xampp 1.8.3

php version 5.5.11

phpMyAdmin 4.1.12

Mysql Server 5.6

 

 


  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 4814 posts
  • LocationState College, PA (USA)

Posted 20 April 2015 - 8:04 AM

You didn't really ask a question, so I assume it's something along the lines of "how do I do this?" To start, I'll say that I can't think of a single site where the customer adds multiple things to the cart at once using checkboxes. That's an odd UX. And, again, I've not seen it. What's more common is that a single thing at a time is added to the cart using Ajax, so the customer never leaves the current page.

 

Regardless, what I'd do is have the form be sent to cart.php via GET. Change the name of the checkbox inputs to sku[]. Then, on cart.php, if sku is an array, use a loop to go through each item. Otherwise, it's a direct link submission (a single product was added by clicking a link), sku is not an array, and the original code would be used. 


  • 0

#3 Bill

Bill

    Member

  • Members
  • PipPip
  • 46 posts

Posted 4 May 2015 - 5:35 AM

Thanks Larry


  • 0

#4 Bill

Bill

    Member

  • Members
  • PipPip
  • 46 posts

Posted 7 May 2015 - 2:21 AM

I have been able to figure out how to get multiple checkboxes to work.  After spending a couple of weeks studying similar programs from example 2,  I was surprised that it only took a few minutes to do in terms of coding at the keyboard and not one error message! Parts of the code from the add_inventory script in admin section and cart.php only required small changes.  

 

However, it never occurred to me that ecommerce sites only use one item as part of payment process.  After searching online for a solution I noticed one fellow said that a website is “flawed” if you cannot select more than one item. This information was in the search results and I did not click on the link.  Anyway, I am not an expert here.  I am only designing a non-ecommerce program for my own purposes and thus these multiple checkboxes are not part of a payment gateway at all. What I have been developing is a food program where I can select certain items from a list of categories and move them to another list like cart. The user interface is now more efficient and effortless as many products can be selected on one page to view, whereas previously this required many clicks.

 

So thanks again as your instructions were easy to implement Larry. 


  • 0