Wow... I am baffled.

I have a shopping cart script that works perfectly until I add in the following 3 lines of calculations
What happens when that is added is that it stops the processing at that point altogether, and doesn't display the balance of the page, and yet when I check the syntax in php, there are no issues.

Any fresh eyes that can point me in the right direction for a resolution?

\$cart_tax=round((\$cart_points * 6 / 100),0);
\$cart_ship=round((\$cart_points * 8 / 100),0)+225;
\$cart_total=\$cart_points + \$cart_tax + \$cart_ship;

I'm not sure if there is something staring at me that I'm missing or what, but I've been trying for hours with every variation of these variables to get it to work with no nuluck

All 11 Replies

Hi,

Well, I am nowhere close to a mathematician or physicist like my starbucks coffee drinking /free WIFI Buddies like Veedeoo and oop_php. These guys are pretty sick..

you can define your tax rate like this for simplicity

``````\$tax_rate = 0.06;
\$shipping_rate = 0.08;

\$cart_tax = round(\$cart_points * \$tax_rate);

\$cart_ship=round(\$cart_points * \$shipping_rate)+225;
\$cart_total = \$cart_points + \$cart_tax + \$cart_ship;
``````

Alternatively, we can also do it by isolating the percentage

``````\$cart_tax = round(\$cart_points * (6/100)); // this follows the PEMDAS rules

//the same thing is true on the other
\$cart_ship=round(\$cart_points * (8/100))+225;
``````

I tried it with cart_points = 600 and it gave me the expected result (909).
What did you expect?

Larry's code (both methods) gave the same answer (909).

UPDATE: I forgot to mention that the precision is a 0 default. So, we don't have to reflect on your script..

The math works... that isn't the issue.

The issue is when I plug those 3 lines into the script, when it displays the page, the processing stops at that point, and nothing else below it is displayed.

When I take them out of the script, the entire page displays as intended, but doesn't have those values calculated.

I'm trying to figure out why it is reacting like that, by simply adding those lines.

And just so you know... I have tried every variation including assigning the tax and shipping rates to variables, as well as using the rates in the equation, and doing the number divided by 100 as displayed...

That doesn't appear to be the issue.

If you show the rest of the script, it may help, otherwise we have nothing to work with.

Well, the script is 336 lines long, and I don't know what part of it you need to see...

I have a couple of html tables displayed on the monitor,
after closing the table,
I do a query to get information from the DB
and after the query, I have those three lines to do the calculations

Then go back to html to display another Table

But if I put those three lines in, then the last table doesn't display, nor the footer for the page...

Without those lines, the balance of the page display perfectly including the footer.

The question is only 'What could it be about those lines that would cause the balance of the page (HTML) not to display?

If you really want to see the entire script, I can put it in here, but I don't know what that will do for you.

``````\$cart_tax=round((\$cart_points * 6 / 100),0);
\$cart_ship=round((\$cart_points * 8 / 100),0)+225;
\$cart_total=\$cart_points + \$cart_tax + \$cart_ship;
``````

If you really want to see the entire script, I can put it in here, but I don't know what that will do for you.

Well, my crystal ball's a bit cloudy this time o' night. Can't really tell without seein' the ol' code.

With regard to the leaving off the end bit - may be due to tags not being formed properly - did you close the ?> php tag before the html? No errors?

So, I guess we need the code.

I did a little test...

Took those three lines of code and moved them up to the top of the script before the first tables were displayed, and ran it... And got what I expected as a result.

Nothing displayed on the screen after those three lines of code.

That is what is really baffling to me about it.

There has to be something in those 3 lines specifically that is causing the issue, and I just don't see what it is.

here is the script...

``````<?php //script name : redeem_cart.php
// Called from redeem script to display selections and do check out.
include "all_inc/config_site.php";
include "all_inc/config_pos.php";
include "all_inc/pgtop.php";
include "all_inc/logcheck.php";  // include on all member pages

// First check for active cart for this member
\$sql = "
SELECT cart_id
FROM redeem_cart
WHERE mem_id = '\$member_id'
AND status='A'
LIMIT 1
";
\$result=mysql_query(\$sql);
\$res=mysql_fetch_array(\$result);
\$cart_id=\$res[0];

if (\$qty>0){// if qty has a value update item record
\$sql_u="
UPDATE redeem_item
SET count=\$qty
WHERE cart_id = '\$cart_id'
AND item_id='\$item'
AND status='A'
";
mysql_query(\$sql_u);

}else{// if qty set to zero, then just update to Removed
\$sql_u="
UPDATE redeem_item
SET count=0, status='R'
WHERE cart_id = '\$cart_id'
AND item_id='\$item'
AND status='A'
";
mysql_query(\$sql_u);
}

}elseif(\$sub_act=='remove'){
\$sql_u="
UPDATE redeem_item
SET count=0, status='R'
WHERE cart_id = '\$cart_id'
AND item_id='\$item'
AND status='A'
";
mysql_query(\$sql_u);

}else{// must be either adding a list item or displaying the cart
// if no cart exists, insert record else if one exists, then get cart ID
if (\$cart_id <= 0){ // no active Cart so create one
\$sql_i = "
INSERT INTO redeem_cart(cart_id, mem_id, status, create_date, last_update)
VALUES('', '\$member_id', 'A', now(), now())
";
mysql_query(\$sql_i);

\$sql = "
SELECT cart_id
FROM redeem_cart
WHERE mem_id = '\$member_id'
AND status='A'
LIMIT 1
";
\$result=mysql_query(\$sql);
\$res=mysql_fetch_array(\$result);
\$cart_id=\$res[0];
}

// if product and points are set from redeem, record in redeem_item
// if not set, then simply display cart
if ((\$prod>0) && (isset(\$points_per))&&(\$qty>0)){
// Only process if count is > zero
//When recording, check first to see if same Item ID is already active on
// this cart, and if so, just update the quantity, else insert record
\$sql = "
SELECT count
FROM redeem_item
WHERE cart_id = '\$cart_id'
AND product_id='\$prod'
AND status='A'
LIMIT 1
";
\$result=mysql_query(\$sql);
\$res=mysql_fetch_array(\$result);
\$prev_ct=\$res[0];
if (isset(\$prev_ct)){  // if already in cart
\$new_ct=\$prev_ct+\$qty;
\$sql_u="
UPDATE redeem_item
SET count='\$new_ct'
WHERE cart_id = '\$cart_id'
AND product_id='\$prod'
AND status='A'
";
mysql_query(\$sql_u);
}else{
\$sql_i = "
INSERT INTO redeem_item(item_id, cart_id, product_id, count, points_each, status)
VALUES('', '\$cart_id', '\$prod', '\$qty', '\$points_per', 'A')
";
mysql_query(\$sql_i);
}
}
}

//######################## DISPLAY SHOPPING CART ########
\$sql = "SELECT cur_bal FROM point_track WHERE mem_id = '\$member_id' LIMIT 1";
\$result=mysql_query(\$sql);
\$points=mysql_fetch_array(\$result);

\$sql = "
SELECT sum(i.points_each * i.count)
FROM redeem_cart AS c, redeem_item AS i
WHERE c.mem_id = '\$member_id'
AND i.cart_id=c.cart_id
AND c.status='A'
";
\$result=mysql_query(\$sql);
\$cart_points=mysql_fetch_array(\$result);

//  Display Cart with all items related
// always have checkout option
?>
<!--Shopping Cart -->
<table align="center" width="850" border="0">
<tr>
<td align="center" valign="top" height="10" class="BlargeBL">
</td>
<td align="center" valign="middle" height="20">
<p class="C150bDR">Shopping Cart</p>
</td>
<td align="Center" valign="top" height="10" class="BlargeBL">
Current Cart Total<br><?php print \$cart_points[0];?>
</td>
</tr>
</table>

<!--Shopping Cart Display-->
<table align="center" width="600" border="1" bordercolor="#B8860B" bordercolorlight="#B8860B" bordercolordark="#B8860B" background="images/screen5.png" cellpadding="3" cellspacing="0">
<tr>
<td align="left" colspan="4">
Current Selections
</td>
<td align="center" colspan="2">
<form name="shop" action="redeem.php" method="POST">
<input type="image" name="shop" src="images/15green_chrome_star.png" width="15" height="15" border="0" alt="Continue Shopping">
</form>
</td>
</tr>

<tr>
<td>Product Name</td>
<td>Points<br>Each</td>
<td>Qty</td>
<td>Update<br>Cart</td>
<td>Extended</td>
<td>Remove<br>From Cart</td>
</tr>
<?php
\$line_items=0;
\$item_count=0;
\$sql = "
SELECT i.item_id, i.product_id, i.count, i.points_each, p.product_name
FROM redeem_item AS i, redeem_product as p
WHERE i.cart_id='\$cart_id'
AND i.status='A'
AND p.product_id=i.product_id
";
\$result=mysql_query(\$sql);
\$C_ct=1;
while(\$C_prod[\$C_ct]=mysql_fetch_array(\$result)){
print"
<tr>
<td>".\$C_prod[\$C_ct][4]."</td>
<td>".\$C_prod[\$C_ct][3]."</td>
<td>
<input name=\"qty\" type=\"text\" size=\"2\" value=\"".\$C_prod[\$C_ct][2]."\" maxlength=\"2\">
</td>
<td align=\"center\">
<input type=\"hidden\" name=\"item\" value=\"".\$C_prod[\$C_ct][0]."\">
<input type=\"image\" name=\"update\" src=\"images/15green_chrome_star.png\" width=\"15\" height=\"15\" border=\"0\" alt=\"Update Cart\">
</td>
</form>
<td>".\$C_prod[\$C_ct][3]*\$C_prod[\$C_ct][2]."</td>

<td align=\"center\">
<form name=\"remove\" action=\"redeem_cart.php\" method=\"POST\">
<input type=\"hidden\" name=\"item\" value=\"".\$C_prod[\$C_ct][0]."\">
<input type=\"hidden\" name=\"sub_act\" value=\"remove\">
<input type=\"image\" name=\"Remove from cart\" src=\"images/15green_chrome_star.png\" width=\"15\" height=\"15\" border=\"0\" alt=\"Remove From Cart\">
</form>
</td>
</tr>
";

++\$line_item;
\$item_count=\$item_count+\$C_prod[\$C_ct][2];
++\$C_ct;
}
if (\$C_ct==1 && !isset(\$C_prod[\$C_ct][0])){
print "  <tr><td colspan=\"6\" align=\"center\"><b>Your Shopping Cart is empty</b></td></tr>";
}
?>
</table>

<?php
\$sql="
FROM members
WHERE user = '\$affiliate'
LIMIT 1
";
\$result=mysql_query(\$sql) or die(mysql_error()."<br><br>".\$sql);
\$row=mysql_fetch_row(\$result);
\$num_rows = mysql_num_rows(\$result);

if(\$num_rows < 1){

}else{
///  Assign values to variables, and construct display info based on dsp_pref(#5)
\$aff_mail=\$row[0];
\$aff_first=\$row[1];
\$aff_last=\$row[2];
\$aff_city=\$row[5];
\$aff_state=\$row[6];
\$aff_zip=\$row[7];
\$ccode=\$row[8];
\$aff_phone=\$row[9];
}

// convert ccode in member record into full country name
\$rResult = mysql_query("select * from countries");
\$sOptions = '';
while(\$aCountry = mysql_fetch_assoc(\$rResult)){
if(\$aCountry['ccode'] == \$ccode){
\$aff_country = \$aCountry[country];
Break;
}
}

//####   When Cart is submitted, update the fields in redeem_cart with
// current information as shown on ship to box...  Do Not Re-Read it from the file

\$cart_tax=round((\$cart_points * 6 / 100),0);
\$cart_ship=round((\$cart_points * 8 / 100),0)+225;
\$cart_total=\$cart_points + \$cart_tax + \$cart_ship;

?>
<br><br>
<!--Display the ship to information and the totals table-->
<table align="center" width="850" border="0">
<tr>
<td align="center" valign="top" height="320">
<table align="center" width="400" height="320" border="1" bordercolor="#B8860B" bordercolorlight="#B8860B" bordercolordark="#B8860B" background="images/screen5.png" cellpadding="3" cellspacing="0">
<tr>
<td align="left" valign="top" height="10" class="BlargeBL">Ship To :</td>
</tr>
<tr><td>
<?php print \$aff_first." ".\$aff_last;?><br>
<?php print \$aff_city.", ".\$aff_state." &nbsp;&nbsp;".\$aff_zip;?><br>
<?php print \$aff_country;?><br>
<?php print \$aff_phone;?><br>
<?php print \$aff_mail;?>
<br>
<p>
By Submitting this order, you understand and agree that all products will be shipped to the above listed address. (recorded upon Cart Submission) Items may be shipped separately, depending on delivery method for each item.
</p>
</td>
</tr>

</table>
</td>

<td align="Center" valign="top" height="320">
<table align="center" width="400" height="320" border="1" bordercolor="#B8860B" bordercolorlight="#B8860B" bordercolordark="#B8860B" background="images/screen5.png" cellpadding="3" cellspacing="0">
<tr>
<td align="left" valign="top" height="10" class="BlargeBL" colspan="2">
Cart Totals
</td>
</tr>
<tr>
<td align="right">Line Items : </td>
<td align="left"><?php print \$line_items;?></td>
</tr>
<tr>
<td align="right">Item Count : </td>
<td align="left"><?php print \$item_count;?></td>
</tr>
<tr>
<td align="right">Sub Total : </td>
<td align="left"><?php print \$cart_points;?></td>
</tr>
<tr>
<td align="right">ND Sales/Use Tax : </td>
<td align="left"><?php print \$cart_tax;?></td>
</tr>
<tr>
<td align="right">Shipping : </td>
<td align="left"><?php print \$cart_ship;?></td>
</tr>
<tr>
<td align="right">Total : </td>
<td align="left"><?php print \$cart_points;?></td>
</tr>
<tr>
<td align="center" valign="middle" colspan="2">Submit Order Button</td>
</tr>
</table>

</td>
</tr>
</table>

<?php include 'all_inc/pgbottom.php';?>
``````
``````\$cart_points=mysql_fetch_array(\$result);
//  Display Cart with all items related
// always have checkout option
?>
<!--Shopping Cart -->
<table align="center" width="850" border="0">
<tr>
<td align="center" valign="top" height="10" class="BlargeBL">
</td>
<td align="center" valign="middle" height="20">
<p class="C150bDR">Shopping Cart</p>
</td>
<td align="Center" valign="top" height="10" class="BlargeBL">
Current Cart Total<br><?php print \$cart_points[0];?>
</td>
</tr>
</table>
``````

From this we are to assume that \$cart_points is an array. So this:

``````\$cart_tax=round((\$cart_points * 6 / 100),0);
\$cart_ship=round((\$cart_points * 8 / 100),0)+225;
\$cart_total=\$cart_points + \$cart_tax + \$cart_ship;
``````

Should be:

``````\$cart_tax=round((\$cart_points[0] * 6 / 100),0);
\$cart_ship=round((\$cart_points[0] * 8 / 100),0)+225;
\$cart_total=\$cart_points[0] + \$cart_tax + \$cart_ship;
``````

?

commented: Excellent Catch. thank you +2

Gotta Tell you... I never imagined that sending the script would resolve it...
But then again I never imagined that referencing an array variable like that would cause the page not to be displayed either.

But you were right, on both counts diafol.

Thank you very much.

It always seems to be the simple things that blind me the most.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.