Jump to content
Larry Ullman's Book Forums

Put Decimals Instead Of Intergers In The Shopping Cart.Php


Recommended Posts

I don't know how to modify the cart script to accommodate decimals in the $qty input field! Everytime I try to use say 1.3 and click on the update button the digit is rounded up or down. After changing the filter validation from INT to FLOAT I thought it would allow decimals but this is not the case! Does anyone know how I would go about inputing decimals in the cart quantity field?

 

Here is 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 = 'Coffee - Your Shopping Cart';
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)");

// 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_FLOAT, array('min_range' => 0))) ? $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')");
//echo '<pre>' . print_r ($energy, 1) . '</pre>';
//echo '<pre>' . print_r ($r, 1) . '</pre>';
//print $energy;
//print $r;
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');
?>

 

Cheers

Link to comment
Share on other sites

Thanks for your response. If I alter the filter_validate_int to filter_validate_float and drop the extra parameters as with the following:

 

//$qty = (filter_var($qty, FILTER_VALIDATE_FLOAT, array('min_range' => 0))) ? $qty : 1;
$qty = (filter_var($qty, FILTER_VALIDATE_FLOAT));

 

the cart quantity continues to display an integer when updated. And even if the filter_validate_float line is not included then it still generates an interger.

 

The reason I want to use decimals is that I intend to write a nutrition program that will use decimals for various quantities. For instance if define a given food serving size as 100 grams and then I have one and a half servings then that equals 1.5.

Link to comment
Share on other sites

Check what type $qty is with gettype() or is_float(). You can use settype() or convert it to a float e.g $qty *= 1.0. Also change your filter_var statement to

$qty = (filter_var($qty, FILTER_VALIDATE_FLOAT, array('min_range' => 0))) ? $qty : 1.0;

Link to comment
Share on other sites

Thank you for your suggestions. It gave inspiration to tackcle this enquiry with a new direction. I've also been able to partially use decimals by tracking the $qty variable with an echo command in the cart script and by changing the mysql database attributes from tinyint to decimals. I still have much to do especially with the way the info is obtained from the previous page but I think I can do it eventually. Cheers!

Link to comment
Share on other sites

 Share

×
×
  • Create New...