Jump to content
Larry Ullman's Book Forums

Controlling Query Results From Browse.Php To Cart.Php


Recommended Posts

I am following the ex.2 sample code that comes with Part Three Selling Physical Products chapters of the Effortless E-Comm book.

 

I have added product options to the items to be sold on the site I am building. All of my stored procedures are working correctly, selecting the product options I created. My browse.php and html view files are working correctly with the htaccess file as I can browse between /shop/ and /browse/ correctly on my localhost and see the product options on each html view file.

 

The issue I ran into at this point is getting the undefined variable notices or warnings when the html view files "pass" the result set to my cart.php page.

 

It appears to me that because I have all the 'jumprope' (jumprope versus coffee) and 'other' select results in ONE  fetch array coming from browse.php then the html view files into cart.php, I get an Undefined index error.

 

So I need to make a change to the cart.php page so that there is some type of conditional put on the add_to_cart INSERT procedure. I am using Larry's Rewrite of the Stored Procedures using PHP instead of the stored procedure for the add_to_cart procedure until I get this squared away.

 

Because the only the individual product options are shown on the html view pages, the other category product options are not in the result set cart.php uses in the first add_to_cart SELECT, UPDATE, INSERT statement. Hence I get the Undefined error. And with the code written as pasted below, this is normal and correct. What I mean is that if cart.php is coming from list_customRope.html, there is no 'shirt_size' in the $r = mysqli_query, and the undefined index error or warning come up.

 

If I remove the 'other' columns and values from the add_to_cart INSERT statement shown on cart.php (hat and shirt sizes), my cart table is updated correctly when coming from listcustomRopes.html. 

 

It is hard to explain this so I completely understand if this is too open ended.

 

I have my browse.php page, the list_customRope.html page and cart.php page included below in case one of the forum leaders would have any advice. As always thank you for reading my post.

 

 

----------------------

browse.php ------

----------------------

 

 

<?php
 
// This file lists products in a specific category
// This script is begun in Chapter 8.
 
// Require the configuration before any PHP code:
require ('./includes/config.inc.php');
 
// Validate the required values:
$type = $sp_type = $sp_cat = $category = false;
if (isset($_GET['type'], $_GET['category'], $_GET['id']) && filter_var($_GET['id'], FILTER_VALIDATE_INT, array('min_range' => 1))) {
 
// Make the associations:
$category = $_GET['category'];
$sp_cat = $_GET['id'];
 
// Validate the type:
if ($_GET['type'] == 'goodies') {
 
$sp_type = 'other';
$type = 'goodies';
 
} elseif ($_GET['type'] == 'jumprope') {
 
$type = $sp_type = 'jumprope';
 
}
 
}
 
// If there's a problem, display the error page:
if (!$type || !$sp_type || !$sp_cat || !$category) {
$page_title = 'Error!';
include ('./includes/header.html');
include ('./views/error.html');
include ('./includes/footer.html');
exit();
}
 
// Create a page title:
$page_title = ucfirst($type) . ' to Buy::' . $category;
 
// Include the header file:
include ('./includes/header.html');
 
// Require the database connection:
require (MYSQL);
 
// Call the stored procedure:
 
$have_results=false;
 
$r = mysqli_query($dbc, "CALL select_products('$sp_type', $sp_cat)");
if($r and mysqli_num_rows($r))
{
$have_results=true;
}
 
mysqli_next_result($dbc); 
if ($sp_type=="jumprope")
 
//santity check to see what $sp_type contains
echo "the sp_type is $sp_type";
else
echo "the sp_type is $sp_type";
 
$r_handle = mysqli_query($dbc, "CALL select_handle_length ('jumprope');");   
//var_dump("1");var_dump($r_handle);var_dump(mysqli_num_rows($r_handle));
if($r_handle and mysqli_num_rows($r_handle)>0)
{
$have_results=true;
}
mysqli_next_result($dbc); 
  
$r_rope_length = mysqli_query($dbc, "CALL select_rope_length ('jumprope');");   
//var_dump("2");var_dump($r_rope_length);var_dump(mysqli_num_rows($r_rope_length));
if($r_rope_length and mysqli_num_rows($r_rope_length)>0)
{
$have_results=true;
}
mysqli_next_result($dbc); 
 
$r_rope_gauge = mysqli_query($dbc, "CALL select_rope_gauge ('jumprope');");  
//var_dump("3");var_dump($r_rope_gauge);var_dump(mysqli_num_rows($r_rope_gauge));
if($r_rope_gauge and mysqli_num_rows($r_rope_gauge)>0)
{
$have_results=true;
}
mysqli_next_result($dbc); 
 
$r_handle_color = mysqli_query($dbc, "CALL select_handle_color ('jumprope');");   
//var_dump("4");var_dump($r_handle_color);var_dump(mysqli_num_rows($r_handle_color));
if($r_handle_color and mysqli_num_rows($r_handle_color)>0)
{
$have_results=true;
}
mysqli_next_result($dbc); 
 
$r_endcap_color = mysqli_query($dbc, "CALL select_endcap_color ('jumprope');");   
//var_dump("5");var_dump($r_endcap_color);var_dump(mysqli_num_rows($r_endcap_color));
if($r_endcap_color and mysqli_num_rows($r_endcap_color)>0)
{
$have_results=true;
}
 
mysqli_next_result($dbc); 
 
$r_shirt_size = mysqli_query($dbc, "CALL select_shirt_size ('other');");   
//var_dump("5");var_dump($r_shirt_size);var_dump(mysqli_num_rows($r_shirt_size));
if($r_shirt_size and mysqli_num_rows($r_shirt_size)>0)
{
$have_results=true;
}
mysqli_next_result($dbc); 
 
$r_hat_size = mysqli_query($dbc, "CALL select_hat_size ('other');");   
//var_dump("5");var_dump($r_hat_size);var_dump(mysqli_num_rows($r_hat_size));
if($r_hat_size and mysqli_num_rows($r_hat_size)>0)
{
$have_results=true;
}
mysqli_next_result($dbc);
  
 
mysqli_close($dbc);
 
 
// For debugging purposes:
if (!$r) echo mysqli_error($dbc);
 
// If records were returned, include the view file NOTE: Added:
if (mysqli_num_rows($r) > 0) {
if ($category == "Tee-Shirts") {
// Three versions of this file:
include ('./views/list_teeShirts.html');
} elseif ($category == "Hats") {
include ('./views/list_hats.html');
} elseif ($category == "Pre-Built Dynamite Stick") {
include ('./views/list_prebuiltRope.html');
} elseif ($category == "Customized Dynamite Stick") {
include ('./views/list_customRope.html');
}
} else { // Include the "noproducts" page:
include ('./views/noproducts.html');
 
 
}
 
 
 
 
// Include the footer file:
include ('./includes/footer.html');
?>
 

----------------------

listcustomRope.html ------

----------------------

 

 

<?php // This page is included by browse.php.
// This page displays the available Customized Jump Rope products.
// This page will make use of the query result $r.
// The query returns an array of: description, image, sku, name, and stock.
 
// Only display the header once:
 
$header = false; 
 
// Added later in Chapter 8:
include ('./includes/product_functions.inc.php');
 
// Loop through the results:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
 
    
// If the header hasn't been shown, create it:
if (!$header) 
    { ?>
<!-- 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><?php echo $category; ?></h2>
              <div class="img-box">
                <p><img alt="<?php echo $category; ?>" src="/products/<?php echo $row['image']; ?>" /><?php echo $row['description']; ?></p>
<p><small>All listed products are currently available.</small>
<form action="/cart.php" method="get"><input type="hidden" name="action" value="add" /><select name="sku">
<?php // The header has now been shown:
$header = true;
} // End of $header IF.
 
 
// Create each option:
echo '<option value="' . $row['sku'] . '">' . $row['name'] . get_price($type, $row['price'], $row['sale_price']) . '</option>';
 
 
} // End of WHILE loop. 
if($header)
 
?>
</select><br />    
 
    
<?php
 
$header = false;
// Loop through the results:
if($r_handle)
{
while ($row = mysqli_fetch_array($r_handle, MYSQLI_BOTH)) 
{
// If the header hasn't been shown, create it:
if (!$header) 
{ ?>
Handle Length <select name="handle_length">
<?php // The header has now been shown:
$header = true;
} // End of $header IF.
 
// Create each option:
echo '<option value="' . $row['handle_length'] . '">' . $row['handle_length'] . ' </option>';
 
} // End of WHILE loop. 
if($header)
{
  }
}
?>
</select><br />
    
<?php
 
$header = false;
// Loop through the results:
if($r_handle)
{
while ($row = mysqli_fetch_array($r_rope_length, MYSQLI_ASSOC)) 
{
// If the header hasn't been shown, create it:
if (!$header) 
{ ?>
Rope Length <select name="rope_length">
<?php // The header has now been shown:
$header = true;
} // End of $header IF.
 
// Create each option:
echo '<option value="' . $row['rope_length'] . '">' . $row['rope_length'] . '</option>';
} // End of WHILE loop. 
if($header)
{
  }
}
?>
</select><br />
    
<?php
 
$header = false;
// Loop through the results:
if($r_rope_gauge)
{
while ($row = mysqli_fetch_array($r_rope_gauge, MYSQLI_ASSOC)) 
{
// If the header hasn't been shown, create it:
if (!$header) 
{ ?>
Rope Gauge <select name="rope_gauge">
<?php // The header has now been shown:
$header = true;
} // End of $header IF.
 
// Create each option:
echo '<option value="' . $row['rope_gauge'] . '">' . $row['rope_gauge'] . '</option>';
} // End of WHILE loop. 
if($header)
{
  }
}
?>
</select><br />
 
<?php
 
$header = false;
// Loop through the results:
if($r_handle_color)
{
while ($row = mysqli_fetch_array($r_handle_color, MYSQLI_ASSOC)) 
{
// If the header hasn't been shown, create it:
if (!$header) 
{ ?>
Handle Color <select name="handle_color">
<?php // The header has now been shown:
$header = true;
} // End of $header IF.
 
// Create each option:
echo '<option value="' . $row['handle_color'] . '">' . $row['handle_color'] . '</option>';
} // End of WHILE loop. 
if($header)
{
  }
}
?>
</select><br />
 
<?php
 
$header = false;
// Loop through the results:
if($r_handle)
{
while ($row = mysqli_fetch_array($r_endcap_color, MYSQLI_ASSOC)) 
{
// If the header hasn't been shown, create it:
if (!$header) 
{ ?>
End Cap Color <select name="end_cap_color">
<?php // The header has now been shown:
$header = true;
} // End of $header IF.
 
// Create each option:
echo '<option value="' . $row['end_cap_color'] . '">' . $row['end_cap_color'] . '</option>';
} // End of WHILE loop. 
if($header)
{
  }
}
?>
</select><br />
 
<p>Quantity<select name="qty"><option>1</option><option>2</option><option>3</option></select></p>          
 
 
</select> <input type="submit" value="Add to Cart" class="button" /></p></form></div>
             </div>
          </div>
       </div>
  <div class="left-bot-corner">
  <div class="right-bot-corner">
      <div class="border-bot"></div>
     </div>
  </div>
</div>
<!-- box end -->
 

 

----------------------

cart.php ------

----------------------

 

 

<?php
 
// This file manages the shopping cart.
// This script is begun in Chapter 9.
 
// Require the configuration before any PHP code:
require ('./includes/config.inc.php');
 
// Check for, or create, a user session:
if (isset($_COOKIE['SESSION'])) {
$uid = $_COOKIE['SESSION'];
} else {
$uid = md5(uniqid('biped',true));
}
 
// Send the cookie:
setcookie('SESSION', $uid, time()+(60*60*24*30));
 
// Include the header file:
$page_title = '1st JumpRope goes to Larry Ullman';
include ('./includes/header.html');
 
// Require the database connection:
require (MYSQL);
 
// Need the utility functions:
include ('./includes/product_functions.inc.php');
 
// If there's a SKU value in the URL, break it down into its parts:
if (isset($_GET['sku'])) {
list($sp_type, $pid) = parse_sku($_GET['sku']);
}
 
if (isset ($pid, $sp_type, $_GET['action']) && ($_GET['action'] == 'add') ) { // Add a new product to the cart:
 
//$r = mysqli_query($dbc, "CALL add_to_cart('$uid', '$sp_type', $pid, 1)");
 
$r = mysqli_query($dbc, "SELECT id FROM carts where user_session_id='$uid' AND product_type='$sp_type' AND product_id=$pid");
if (mysqli_num_rows($r) == 1) { // Exists in cart, UPDATE!
    list($cid) = mysqli_fetch_array($r, MYSQLI_NUM);
    $r = mysqli_query($dbc, "UPDATE carts SET quantity=quantity+1, date_modified=NOW() WHERE id=$cid");
} else { // Not in cart, INSERT!
    $r = mysqli_query($dbc, "INSERT INTO carts (user_session_id, product_type, product_id, quantity, handle_length, rope_length, rope_gauge, handle_color, end_cap_color, shirt_size, hat_size) VALUES ('$uid', '$sp_type', $pid, 1, '".$_GET['handle_length']."', '".$_GET['rope_length']."', '".$_GET['rope_gauge']."', '".$_GET['handle_color']."', '".$_GET['end_cap_color']."', '".$_GET['shirt_size']."', '".$_GET['hat_size']."')");
}
// For debugging purposes:
if (!$r) echo mysqli_error($dbc);
 
} elseif (isset ($sp_type, $pid, $_GET['action']) && ($_GET['action'] == 'remove') ) { // Remove it from the cart.
 
$r = mysqli_query($dbc, "CALL remove_from_cart('$uid', '$sp_type', $pid)");
 
} elseif (isset ($sp_type, $pid, $_GET['action'], $_GET['qty']) && ($_GET['action'] == 'move') ) { // Move it to the cart.
 
// Determine the quantity:
$qty = (filter_var($_GET['qty'], FILTER_VALIDATE_INT, array('min_range' => 1))) ? $_GET['qty'] : 1;
 
// Add it to the cart:
$r = mysqli_query($dbc, "CALL add_to_cart('$uid', '$sp_type', $pid, $qty)");
 
// Remove it from the wish list:
$r = mysqli_query($dbc, "CALL remove_from_wish_list('$uid', '$sp_type', $pid)");
 
} 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)");
 
}
 
} // End of FOREACH loop.
 
}// End of main IF.
 
// Get the cart contents:
$r = mysqli_query($dbc, "CALL get_shopping_cart_contents('$uid')");
 
if (mysqli_num_rows($r) > 0) { // Products to show!
include ('./views/cart.html');
} else { // Empty cart!
include ('./views/emptycart.html');
}
 
// Finish the page:
include ('./includes/footer.html');
?>
Link to comment
Share on other sites

Yeah...that's a lot to take in and yet still lacking specifics. How about providing:

- the version of PHP in use

- what actual error messages you're getting

- what debugging steps you've taken and what the results are

 

It may also help to have a sense of the database structure, but I hesitate to ask that considering how long this post is already.

 

It may also help for you to say (along with the above): "I think the problem is ____".

Link to comment
Share on other sites

PHP version is 5.4.10

 

The error message is: An error occurred in script '/Applications/MAMP/htdocs/cart.php' on line 43:

Undefined index: handle_length 

 

To debug I am using  <pre><?php print_r($_GET); ?></pre>  to monitor the arrays to make sure my select statements are working when going from browse.php to the html view pages.

 

The database has the same structure as described in the book with two additional tables for my product options which are named options and option_groups.

 

I think the problem is that my INSERT INTO carts statement on cart.php, includes columns and values that are not a <option value> on every html view file. So I need to somehow put a conditional on INSERT INTO carts statement that only attempts to add to the carts table based on the <option value> from the html view page the user is on when submitting. 

 

These questions are probably too localized and ambiguous. Especially for a Sunday morning so no worries if this post needs to be closed! 

Link to comment
Share on other sites

It's up to you whether we proceed or not. 

 

As for that error message, which line in your code is 43? 

 

As for your debugging, I don't see how <?php print_r($_GET); ?> is going to reflect issues with the stored procedure results. I'd confirm the stored procedure being called and then run that procedure using another interface to confirm the results (like you'd do with any problematic query).

 

Again, up to you whether we work through this or not.

Link to comment
Share on other sites

Here is line 43

 

 

    $r = mysqli_query($dbc, "INSERT INTO carts (user_session_id, product_type, product_id, quantity, handle_length, rope_length, rope_gauge, handle_color, end_cap_color, shirt_size, hat_size) VALUES ('$uid', '$sp_type', $pid, 1, '".$_GET['handle_length']."', '".$_GET['rope_length']."', '".$_GET['rope_gauge']."', '".$_GET['handle_color']."', '".$_GET['end_cap_color']."', '".$_GET['shirt_size']."', '".$_GET['hat_size']."')");
 
This code will produce the Undefined Index error when the [_GET] Array does not contain each of the column => value pairs it is looking for.
 
I have separate view files; one for jump ropes, one for hats, one for shirts.
 
So if the user adds a shirt to the cart from the shirts html view file for example, the _GET array does not contain a  jumprope $_GET['handle_length'] option value and the Undefined Index error is produced.
 
I have these pages on a testing server and I could provide the URL but I am not sure if you have the bandwidth to check that out right now.
Link to comment
Share on other sites

Larry I believe I have figured out my issue. I will use the following to check for a value in the $_GET array and assign a blank string if not. When I add this code on cart.php BEFORE line 43 shown above, I no longer get the Undefined Index notice. Thanks for hanging in there with me on this post.

 

 

$handle_length = isset($_GET['handle_length']) ? $_GET['handle_length'] : '';
$rope_length = isset($_GET['rope_length']) ? $_GET['rope_length'] : '';
$rope_gauge = isset($_GET['rope_gauge']) ? $_GET['rope_gauge'] : '';
$handle_color = isset($_GET['handle_color']) ? $_GET['handle_color'] : '';
$end_cap_color = isset($_GET['end_cap_color']) ? $_GET['end_cap_color'] : '';
$shirt_size = isset($_GET['shirt_size']) ? $_GET['shirt_size'] : '';
$hat_size = isset($_GET['hat_size']) ? $_GET['hat_size'] : '';
Link to comment
Share on other sites

 Share

×
×
  • Create New...