Jump to content

Decimal is Rounding up to nearest number - I DONT WANT THAT!


ikon

Recommended Posts

This is the product table....  Its the pd_price thats having the problem.

 

CREATE TABLE `tbl_product` (
  `pd_id` int(10) unsigned NOT NULL auto_increment,
  `cat_id` int(10) unsigned NOT NULL default '0',
  `pd_name` varchar(100) NOT NULL default '',
  `pd_description` text NOT NULL,
  `pd_price` decimal(9,2) NOT NULL default '0.00',
  `pd_qty` smallint(5) unsigned NOT NULL default '0',
  `pd_image` varchar(200) default NULL,
  `pd_thumbnail` varchar(200) default NULL,
  `pd_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `pd_last_update` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`pd_id`),
  KEY `cat_id` (`cat_id`),
  KEY `pd_name` (`pd_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=402 ;

Here is the call function in php that is having the problem.

 

?>
<tr class="content"> 
  <td width="80" align="center"><a href="<?php echo $productUrl; ?>"><img src="<?php echo $pd_thumbnail; ?>" border="0"></a></td>
  <td><a href="<?php echo $productUrl; ?>"><?php echo $pd_name; ?></a></td>
   <td align="right"><?php echo displayAmount($pd_price); ?></td>
  <td width="75"><input name="txtQty[]" type="text" id="txtQty[]" size="5" value="<?php echo $ct_qty; ?>" class="box" onKeyUp="checkNumber(this);">
  <input name="hidCartId[]" type="hidden" value="<?php echo $ct_id; ?>">
  <input name="hidProductId[]" type="hidden" value="<?php echo $pd_id; ?>">
  </td>
  <td align="right"><?php echo displayAmount($pd_price * $ct_qty); ?></td>
  <td width="75" align="center"> <input name="btnDelete" type="button" id="btnDelete" value="Delete" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=delete&cid=$ct_id"; ?>';" class="box"> 
  </td>
</tr>
<?php
}
?>

 

Hope this helps,

 

I've also used it here...

 

<?php
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
	extract($cartContent[$i]);
	$pd_name = "$ct_qty x $pd_name";
	$url = "index.php?c=$cat_id&p=$pd_id";

	$subTotal += $pd_price * $ct_qty;
?>
<tr>
   <td><a href="<?php echo $url; ?>"><?php echo $pd_name; ?></a></td>
   
  <td width="30%" align="right"><?php echo displayAmount($ct_qty * $pd_price); ?></td>
</tr>
<?php

 

Cheers All

 

Link to comment
Share on other sites

erm, i think this is the code you mean...

 

This is to populate the cart:

 

function getCartContent()
{
$cartContent = array();

$sid = session_id();
$sql = "SELECT ct_id, ct.pd_id, ct_qty, pd_name, pd_price, pd_thumbnail, pd.cat_id
		FROM tbl_cart ct, tbl_product pd, tbl_category cat
		WHERE ct_session_id = '$sid' AND ct.pd_id = pd.pd_id AND cat.cat_id = pd.cat_id";

this is the query to get the totoal amount for the order

 

function getOrderAmount($orderId)
{
$orderAmount = 0;

$sql = "SELECT SUM(pd_price * od_qty)
        FROM tbl_order_item oi, tbl_product p 
	    WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId

		UNION

		SELECT od_shipping_cost 
		FROM tbl_order
		WHERE od_id = $orderId";
$result = dbQuery($sql);

if (dbNumRows($result) == 2) {
	$row = dbFetchRow($result);
	$totalPurchase = $row[0];

	$row = dbFetchRow($result);
	$shippingCost = $row[0];

	$orderAmount = $totalPurchase + $shippingCost;
}	

return $orderAmount;	
}

 

But its the point before this thats its displaying the wrong data....This is the cart

<?php
if (!defined('WEB_ROOT')) {
exit;
}

$cartContent = getCartContent();

$numItem = count($cartContent);	
?>
<table width="100%" border="1" cellspacing="0" cellpadding="2" id="minicart">
<?php
if ($numItem > 0) {
?>
<tr>
  <td colspan="2">Cart Content</td>
</tr>
<?php
$subTotal = 0;
for ($i = 0; $i < $numItem; $i++) {
	extract($cartContent[$i]);
	$pd_name = "$ct_qty x $pd_name";
	$url = "index.php?c=$cat_id&p=$pd_id";

	$subTotal += $pd_price * $ct_qty;
?>
<tr>
   <td><a href="<?php echo $url; ?>"><?php echo $pd_name; ?></a></td>
   
  <td width="30%" align="right"><?php echo displayAmount($ct_qty * $pd_price); ?></td>
</tr>
<?php
} // end while
?>
  <tr><td align="right">Sub-total</td>
  <td width="30%" align="right"><?php echo displayAmount($subTotal); ?></td>
</tr>
  <tr><td align="right">Shipping</td>
  <td width="30%" align="right"><?php echo displayAmount($shopConfig['shippingCost']); ?></td>
</tr>
  <tr><td align="right">Total</td>
  <td width="30%" align="right"><?php echo displayAmount($subTotal + $shopConfig['shippingCost']); ?></td>
</tr>
  <tr><td colspan="2"> </td></tr>
  <tr>
  <td colspan="2" align="center"><a href="cart.php?action=view"> Go To Shopping 
   Cart</a></td>
</tr>  
<?php	
} else {
?>
  <tr><td colspan="2" align="center" valign="middle">Shopping Cart Is Empty</td></tr>
<?php
}
?> 
</table>

 

This is the saving of the items ordered....

if ($orderId) {
		// save order items
		for ($i = 0; $i < $numItem; $i++) {
			$sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty)
					VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})";
			$result = dbQuery($sql);					
		}

here is the displayAmount function:

echo displayAmount($ct_qty * $pd_price);

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

common.php - this includes all the functions being called

 

<?php
/*
Contain the common functions 
required in shop and admin pages
*/
require_once 'config.php';
require_once 'database.php';

/*
Make sure each key name in $requiredField exist
in $_POST and the value is not empty
*/
function checkRequiredPost($requiredField) {
$numRequired = count($requiredField);
$keys        = array_keys($_POST);

$allFieldExist  = true;
for ($i = 0; $i < $numRequired && $allFieldExist; $i++) {
	if (!in_array($requiredField[$i], $keys) || $_POST[$requiredField[$i]] == '') {
		$allFieldExist = false;
	}
}

return $allFieldExist;
}

function getShopConfig()
{
// get current configuration
$sql = "SELECT sc_name, sc_address, sc_phone, sc_email, sc_shipping_cost, sc_order_email, cy_symbol 
		FROM tbl_shop_config sc, tbl_currency cy
		WHERE sc_currency = cy_id";
$result = dbQuery($sql);
$row    = dbFetchAssoc($result);

    if ($row) {
        extract($row);

        $shopConfig = array('name'           => $sc_name,
                            'address'        => $sc_address,
                            'phone'          => $sc_phone,
                            'email'          => $sc_email,
			    'sendOrderEmail' => $sc_order_email,
                            'shippingCost'   => $sc_shipping_cost,
                            'currency'       => $cy_symbol);
    } else {
        $shopConfig = array('name'           => '',
                            'address'        => '',
                            'phone'          => '',
                            'email'          => '',
			    'sendOrderEmail' => '',
                            'shippingCost'   => '',
                            'currency'       => '');    
    }

return $shopConfig;						
}

function displayAmount($amount)
{
global $shopConfig;
return $shopConfig['currency'] . number_format($amount);
}

/*
Join up the key value pairs in $_GET
into a single query string
*/
function queryString()
{
$qString = array();

foreach($_GET as $key => $value) {
	if (trim($value) != '') {
		$qString[] = $key. '=' . trim($value);
	} else {
		$qString[] = $key;
	}
}

$qString = implode('&', $qString);

return $qString;
}

/*
Put an error message on session 
*/
function setError($errorMessage)
{
if (!isset($_SESSION['plaincart_error'])) {
	$_SESSION['plaincart_error'] = array();
}

$_SESSION['plaincart_error'][] = $errorMessage;

}

/*
print the error message
*/
function displayError()
{
if (isset($_SESSION['plaincart_error']) && count($_SESSION['plaincart_error'])) {
	$numError = count($_SESSION['plaincart_error']);

	echo '<table id="errorMessage" width="550" align="center" cellpadding="20" cellspacing="0"><tr><td>';
	for ($i = 0; $i < $numError; $i++) {
		echo '&#8226; ' . $_SESSION['plaincart_error'][$i] . "<br>\r\n";
	}
	echo '</td></tr></table>';

	// remove all error messages from session
	$_SESSION['plaincart_error'] = array();
}
}

Link to comment
Share on other sites

And there you have your answer then. I thought as much.

 

Thus

function displayAmount($amount)
{
   global $shopConfig;
   return $shopConfig['currency'] . number_format($amount);
}

 

should be changed to

function displayAmount($amount)
{
   global $shopConfig;
   return $shopConfig['currency'] . number_format($amount,2);
}

Link to comment
Share on other sites

what do you mean see the value?

I mean echo the $sql variable inside the loop.

 

I'm not too sure how do that i'm afraid.....

 

PLEASE start using code blocks... I'm getting tired to re-formatting everything.

 

Change your "saving" block to this:

if ($orderId) {
		// save order items
		for ($i = 0; $i < $numItem; $i++) {
			$sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty)
					VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})";
                                echo $sql;
			$result = dbQuery($sql);					
		}

 

Then post the output from the echo call.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.