Jump to content

Recommended Posts

<?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. :)

 

Link to comment
https://forums.phpfreaks.com/topic/142053-total-cost-problem/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743875
Share on other sites

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 .)

Link to comment
https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743885
Share on other sites

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!";
   }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743889
Share on other sites

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!";
   }
?>

 

Link to comment
https://forums.phpfreaks.com/topic/142053-total-cost-problem/#findComment-743903
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.