justinh Posted January 23, 2009 Share Posted January 23, 2009 <?php if(isset($_GET['clearlist'])){ session_destroy(); }else{ $item_to_add = $_GET['item']; if ( ! isset ( $_SESSION['items'] ) ) { $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } $_SESSION['totalcost'] = $costofitem; $_SESSION['items'][$item_to_add] = 1; } else { if ( ! isset($_SESSION['items'][$item_to_add] ) ) { $_SESSION['items'][$item_to_add] = 1; $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } echo $_SESSION['totalcost'] = +$costofitem; } else { $_SESSION['items'][$item_to_add]++; $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } echo $_SESSION['totalcost'] = +$costofitem*$_SESSION['items'][$item_to_add]; } } if ( isset ( $_SESSION['items'] ) ) { echo "<table> <tr><td><b>Item Name</b></td><td><b>Quanity</b></td></tr>"; foreach($_SESSION['items'] AS $key => $qty){ echo "<tr><td>".$key . "</td><td>" . $qty ."</td></tr>"; } echo "</table>"; echo "<div id=\"totalcost\">Total Cost:" . $_SESSION['totalcost'] ."</div>"; } else { echo "No Items exist!"; } } ?> Trying to get as each Item is added, it adds together the total cost of all items. Works fine with one item, but once you start to enter a new item, it resets the price to just the price of that item. I'm stumped as to why it would do that. Any help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/ Share on other sites More sharing options...
premiso Posted January 23, 2009 Share Posted January 23, 2009 Why are you echoing it the same line...not sure if that is kosher, but this will solve your woes $_SESSION['totalcost'] = $_SESSION['totalcost']+$costofitem; A bit more code, but it works, I think the + would need to be next to the equals for it to work the other way: $_SESSION['totalcost'] += $costofitem; I am not sure if it is += or =+ but yea. Rinse and repeat for each time you need that done. Edit: Check out the Assignment operators for info on proper usage. Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743875 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 yeah sorry about the ECHO, was using it for debugging. I will see if your suggestion works Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743877 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 Okay that seem to do it, One more question. When the total cost is 479.70 or 479.80 etc. (anything ending in 0 basically) the zero is removed.. so it would display 479.7 or 479.8 etc.. any clue why this is happening? Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743879 Share on other sites More sharing options...
redarrow Posted January 23, 2009 Share Posted January 23, 2009 number_format() look it up m8. Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743883 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 Any 0 (zero) at the end or start of an integer or float will be removed, if you want to specify precision when outputing costs use the following; <?php $cost = 479.7; printf("%.2f", $cost); % - starts your specification . - working from the decimal place onwards 2f - ensure there's two floating-point numbers (after the .) Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743885 Share on other sites More sharing options...
Psycho Posted January 23, 2009 Share Posted January 23, 2009 Also, you are running the exact same query in three different places. You should design your code better so you are not duplicating so much. Otherwise, debuggin will become more difficult (e.g. fix a bug in one place and not another). The code below should do the exact same thing without all the unnecessary repetition (no guarantees). however, I think there is a bug in your logic. Total Cose is being saves as a session variable (for multiple items) and it get ADD to when the page runs. So, if you had one item the total cose would be (1 * the price of the item). Then if that item was added again it would calculate the total cose as Total Cost + (2 * price of the item). So you would have 3*the item price instead of 1* the item price. At least that's the way it looked to me from the code you posted. I made a modification for that in the code below. <?php if(isset($_GET['clearlist'])) { session_destroy(); } else { $item_to_add = $_GET['item']; $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)) { $costofitem = $getitemprice['item_price']; } if ( ! isset($_SESSION['items'][$item_to_add] ) ) { $_SESSION['items'][$item_to_add] = 0; $_SESSION['totalcost'] = 0; } $_SESSION['items'][$item_to_add]++; $_SESSION['totalcost'] += $costofitem; if ( isset ( $_SESSION['items'] ) ) { echo "<table> <tr><td><b>Item Name</b></td><td><b>Quanity</b></td></tr>"; foreach($_SESSION['items'] AS $key => $qty) { echo "<tr><td>".$key . "</td><td>" . $qty ."</td></tr>"; } echo "</table>"; echo "<div id=\"totalcost\">Total Cost:" . $_SESSION['totalcost'] ."</div>"; } else { echo "No Items exist!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743889 Share on other sites More sharing options...
justinh Posted January 23, 2009 Author Share Posted January 23, 2009 Thank you guys for all the quick replies. Don't know where I would be without you. (Probably somewhere secret, with my PHP and Mysql Web Developement book, and some dynamite) @mjdamato: I understand about the exact queries in three different places. I will fix it. As far as the bug in my logic, the code seems to do just what I intended for it do. Maybe its still the illogical way, but for right now it gets it done. I know this is probably the wrong way to look at coding. Once I get more experience problems like this should cease to exist. Here is the final code, with the whole 100.00 problem worked out. Thanks redarrow for the advice, it worked like a charm. <?php if(isset($_GET['clearlist'])){ session_destroy(); }else{ $item_to_add = $_GET['item']; if ( ! isset ( $_SESSION['items'] ) ) { $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } $_SESSION['totalcost'] = $_SESSION['totalcost']+$costofitem; $_SESSION['items'][$item_to_add] = 1; } else { if ( ! isset($_SESSION['items'][$item_to_add] ) ) { $_SESSION['items'][$item_to_add] = 1; $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } $_SESSION['totalcost'] = $_SESSION['totalcost']+$costofitem; } else { $_SESSION['items'][$item_to_add]++; $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$item_to_add'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } $_SESSION['totalcost'] = $_SESSION['totalcost']+$costofitem; } } if ( isset ( $_SESSION['items'] ) ) { echo "<table> <tr><td><b>Item Name</b></td><td><b>Quanity</b></td></tr>"; foreach($_SESSION['items'] AS $key => $qty){ $query = mysql_query("SELECT * FROM hrhuntitems WHERE item_name='$key'") or die(mysql_error()); while($getitemprice=mysql_fetch_array($query)){ $costofitem = $getitemprice['item_price']; } echo "<tr><td>".$key . "</td><td>" . $qty ."</td></tr>"; } echo "</table>"; echo "<div id=\"totalcost\">Total Cost:<b>" . number_format($_SESSION['totalcost'], 2, '.', '') ."</b></div>"; } else { echo "No Items exist!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743903 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.