Jump to content

[SOLVED] Cart Grand Total Calculation?


DarkPrince2005

Recommended Posts

OK...

 

I've got a really nice shopping cart script, but there seems to be a little problem....

 

As soon as the Grand total exceeds a thousand it bugs out.

 

echo "
  <tr>
    <td> </td><td colspan='4' align='left' class='cart-table'>
         Your total including shipping is:</td>
    <td align='center' class='cart-table'>R "; 
$E=0;
$result = mysql_query($query);
if (mysql_num_rows($result)==0)
{
   echo "0.00</td><td align='center' valign='center' class='cart-table'>";
  }
  else
  {
while ($row=mysql_fetch_array($result)){
$E +=$row['total'];
$total = number_format($E,2);
$total1=str_replace(".", "", $total);}
echo $total+85; // output: 78
echo "";
$value=str_replace(",","",$total1)+8500;
   echo "</td><td align='center' valign='center' class='cart-table'>";
}
echo "<form method=\"POST\" action=\"modcart.php?action=empty\">
        <input type=\"hidden\" name=\"carttemp_hidden\"
          value=\"";
if (isset($carttemp_hidden)) {
  echo $carttemp_hidden;
}
echo "\">";
echo "<br /><input type='image' src='images/empty.png' name='empty' value=\"Empty Cart\"  onclick=\"this.form.action='modcart.php?action=empty';\">
      </form>";

 

It is suppose to calculate the total and then add 85 for shipping. Can anyone see what I'm doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/172938-solved-cart-grand-total-calculation/
Share on other sites

Ok, I accidently left out a bit of code

 

 

while ($row = mysql_fetch_array($results)) {
  echo "<tr height='20px'>
		<td align='center' valign='center'>
			<form method='POST' action=''>
				<input type='hidden' name='modified_hidden' value='$row[cart_id]' class='box'>
				<input type='text' name='modified_quan' size='2' value='$row[a]' class='box'></td><td align='center' valign='center'><input type='image' name='delete' src='images/change.png' value=\"Change Qty\"  onclick=\"this.form.action='modcart.php?action=change';\">
		</td>
		<td align='center' valign='center' class='cart-table'>$row[id]</td>
		<td align='left' valign='center' class='cart-table'>$row[name]</td>
		<td align='center' valign='center' class='cart-table'>R $row[price]</td>
		<td align='center' valign='center' class='cart-table'>R ";
  //get extended price
  $extprice = number_format($row['price'] * $row['a'], 2);
  echo $extprice;
  echo "</td>
		<td align='center' valign='center'><input type='image' name='delete' src='images/delete.png' value=\"Delete Item\"  onclick=\"this.form.action='modcart.php?action=delete';\">
        </form></td>";
  echo "</tr>";
  //add extended price to total
  
}
echo "
  <tr>
    <td> </td><td colspan='4' align='left' class='cart-table'>
         Your total including shipping is:</td>
    <td align='center' class='cart-table'>R "; 
$E=0;
$result = mysql_query($query);
if (mysql_num_rows($result)==0)
{
   echo "0.00</td><td align='center' valign='center' class='cart-table'>";
  }
  else
  {
while ($row=mysql_fetch_array($result)){
$E +=$row['total'];
$total = number_format($E,2);}
echo $total+85; // output: 78
echo "";
   echo "</td><td align='center' valign='center' class='cart-table'>";
}
echo "<form method=\"POST\" action=\"modcart.php?action=empty\">
        <input type=\"hidden\" name=\"carttemp_hidden\"
          value=\"";
if (isset($carttemp_hidden)) {
  echo $carttemp_hidden;
}
echo "\">";
echo "<br /><input type='image' src='images/empty.png' name='empty' value=\"Empty Cart\"  onclick=\"this.form.action='modcart.php?action=empty';\">
      </form>";

 

it's still placing a comma in the first extended price....

Ok thats all good and well but now it's posting the amount incorrectly to paygate. Below is my code. the valuue variable basically needs to get a decimal and double zero's added to it.

 

while ($row=mysql_fetch_array($result)){
$E +=$row['total'];
$total = number_format($E, 2, '.', '');}
echo $total+85; // output: 78

$value=$total+85;

Give this a shot. I haven't been working through your code the whole time, but I'm implementing Garethp's code, MasterACE14's code, and the original script... while adding in a few comments, and cleaning up messy coding.

 

echo "
<tr>
	<td> </td><td colspan='4' align='left' class='cart-table'>
	   Your total including shipping is:</td>	
";

$E=0; //Not sure what this variable is, or why it's being used (as it doesn't change).
$result = mysql_query($query); //Check shopping cart.

if (mysql_num_rows($result)==0) { //If shopping cart is empty, 
echo "<td align='center' class='cart-table'>0.00</td><td align='center' valign='center' class='cart-table'>";
} else {
while ($row = mysql_fetch_assoc($result)) { //Loop through result-set.
	$E += $row['total'];                    //Add shopping cart prices to $E.
	$total = number_format($E, 2, '.', ''); //Format total ($E) to $total.
	echo "<td align='center' class='cart-table'>";
	echo $total;
	echo "</td>";
}
}

echo "
<form method=\"POST\" action=\"modcart.php?action=empty\">
<input type=\"hidden\" name=\"carttemp_hidden\ value=\"
";

if (isset($carttemp_hidden)) {
echo $carttemp_hidden;
}

echo "
\">
<br />
<input type='image' src='images/empty.png' name='empty' value=\"Empty Cart\"  onclick=\"this.form.action='modcart.php?action=empty';\">
</form>
";

I don't see a problem on the site...

 

Updated code:

 

echo "
<tr>
	<td> </td><td colspan='4' align='left' class='cart-table'>
	   Your total including shipping is:</td>	
";

$E=0; //Not sure what this variable is, or why it's being used (as it doesn't change).
$result = mysql_query($query) or die(mysql_error()); //Check shopping cart.

if (mysql_num_rows($result)==0) { //If shopping cart is empty, 
echo "<td align='center' class='cart-table'>0.00</td><td align='center' valign='center' class='cart-table'>";
} else {
while ($row = mysql_fetch_assoc($result)) { //Loop through result-set.
	$E += $row['total'];                    //Add shopping cart prices to $E.
	$total = number_format($E, 2, '.', ''); //Format total ($E) to $total.
	echo "<td align='center' class='cart-table'>";
	echo $total;
	echo "</td>";
}
}

echo "
<form method=\"POST\" action=\"modcart.php?action=empty\">
<input type=\"hidden\" name=\"carttemp_hidden\ value=\"
";

if (isset($carttemp_hidden)) {
echo $carttemp_hidden;
}

echo "
\">
<br />
<input type='image' src='images/empty.png' name='empty' value=\"Empty Cart\"  onclick=\"this.form.action='modcart.php?action=empty';\">
</form>
";

Give this a shot:

 

echo "
<tr>
	<td> </td><td colspan='4' align='left' class='cart-table'>
	   Your total including shipping is:</td>	
";

$E=0; //Not sure what this variable is, or why it's being used (as it doesn't change).
$result = mysql_query($query) or die(mysql_error()); //Check shopping cart.

if (mysql_num_rows($result)==0) { //If shopping cart is empty, 
echo "<td align='center' class='cart-table'>0.00</td><td align='center' valign='center' class='cart-table'>";
} else {
while ($row = mysql_fetch_assoc($result)) {      //Loop through result-set.
	$E += $row['total'];                     //Add shopping cart prices to $E.
	$E .= 00;                                //Add trailing zeroes.
	$total = number_format($E, 2, '.', '');  //Format total ($E) to $total.
	echo "<td align='center' class='cart-table'>";
	echo $total;
	echo "</td>";
}
}

echo "
<form method=\"POST\" action=\"modcart.php?action=empty\">
<input type=\"hidden\" name=\"carttemp_hidden\ value=\"
";

if (isset($carttemp_hidden)) {
echo $carttemp_hidden;
}

echo "
\">
<br />
<input type='image' src='images/empty.png' name='empty' value=\"Empty Cart\"  onclick=\"this.form.action='modcart.php?action=empty';\">
</form>
";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.