Jump to content

Adding an updating variable


will_1990

Recommended Posts

Hello all,

 

long time no post! I have missed php!!

 

Thanks in advance for any advice!!

 

I am trying to add an updating price variable to 0 to give a total price, eg someone selects how many items they want, these are listed and the total price is added, however it seems to keep giving me the final price of the last item in my flat file database - whether or not it is selected as an item....

 

here is my code!

 

<?php
$var=0; 
foreach ($_POST as $ID2 => $price) {
  if(!($lines = file('php_ffdb.txt'))) 
{echo 'ERROR: Unable to open file! </body></html>'; exit;} 
   foreach($lines as $theline) {
list($ID, $title, $author, $description, $price, $image) = split('\|',$theline); 
if($ID==$ID2) 
{ 
   echo " <table>
   <tr>
   <td>		  $price</td>
   <td>$ID</TD>
   </TABLE>

     ";


}

   }
   $sum=$var + $price;
     echo("$sum");
  
}


?>

 

Any advice would be great!!

 

Thanks!!!

 

Will

 

Link to comment
Share on other sites

I doubt that your entire $_POST array is just a list of id's and their prices. Post an example of what data your form submits.

 

You should move the code that is reading the file so that it is before the first foreach() loop. Where it is now causes it to re-read the file on every post value.

 

Your code is not accumulating or adding up the values inside of the conditional test when a match is found. You would need something like -

 

$var += $price;

 

And then $var would be the total at the end of the posted logic.

Link to comment
Share on other sites

cheers for the quick reply, im looking into moving the for loop just thought id post this first - i am posting just the id i thought id use a short cut and have the name of the check box the $ID tag as thats all i need as its unique to each entry....

 

<?php
if(!($lines = file('php_ffdb.txt'))) 
{echo 'ERROR: Unable to open file! </body></html>'; exit;} 
foreach($lines as $theline) {
list($ID, $title, $author, $description, $price, $image) = split('\|',$theline); 
echo " 

	  <tr align='center'>
	   
	   <td><img src='$image' width='82' height='125'></td>
	  
	   <td align='center'>$ID. $title <br><font face='arial' size='1'>$description</font>£$price  </td>   
	   
	   <td> <input type='checkbox' name='$ID'><br></td>
		</tr>
	   	

     "; 
}
?> 

 

 

Here is my flat file database - dont believe i have to use this sql would be far better.. but it cant be helped!

 

1|The Lovely The Lovely Bones|Alice Sebold|<b>RRP:</b>£1.97<br><b>You Save:</b> £3.02 (61%) <br><b>Price:</b> |3.00|images/TLB.jpg
2|The Girl with the Dragon Tattoo|Stieg Larsson|<b>RRP:</b>£3.48<br><b>You Save:</b>£4.51 (56%)<br><b>Price:</b>|3.00|images/TGWDT.jpg 
3|Mums Know Best|Dave Myers|<b>RRP:</b>  £20.00<br><b>You Save: </B> 4.13 (52%)<br><b>Price:</b> |3.50|images/MKB.jpg
4|The Girl Who Played with Fire|Stieg Larsson|<b>RRP:</b>   £7.99<br><b>You Save: </B> £4.51 (56%)<br><b>Price:</b> |1.98|images/TGWPWF.jpg
5|Breaking Dawn (Twilight Saga)|Stephenie Meyer|<b>RRP:</b>   £14.99  <br><b>You Save: </B> £7.50 (50%)<br><b>Price:</b>|7.50|images/BD.jpg
6|101 One-pot Dishes|Good Food Magazine|<b>RRP:</b>  £4.99   £1.97<br><b>You Save:</B>  £3.02 (61%)<br><b>Price:</b>|1.97|images/GF.jpg
7|Eclipse (Twilight Saga)|Stephenie Meyer|<b>RRP:</b>   £7.99  <br><b>You Save: </B> £4.53 (50%)<br><b>Price:</b>|3.46|images/Eclipse.jpg

 

Will

Link to comment
Share on other sites

Solved this! Great thanks for your help, i wasnt aware of the += operator, thanks! Is there a way to ensure that if 1.00 and 1.00 were added it would return 2.00 instead of 2?

 

Here is how i changed the code:

 

<?php
if(!($lines = file('php_ffdb.txt'))) 
{echo 'ERROR: Unable to open file! </body></html>'; exit;} 
foreach ($_POST as $ID2 => $price) {
foreach($lines as $theline) {
list($ID, $title, $author, $description, $price, $image) = split('\|',$theline); 
if($ID==$ID2) 
{ 
   echo " <table>
   <tr>
   <td>		  $price</td>
   <td>$ID</TD>
   </TABLE>

     ";
 $var += $price;
}
                              }        
   
     
  
                                   } echo ("$var");



?>

 

Thanks very much for your help!!

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.