Author Topic: Adding an updating variable  (Read 511 times)

0 Members and 1 Guest are viewing this topic.

Offline will_1990Topic starter

  • Enthusiast
  • Posts: 131
    • View Profile
Adding an updating variable
« on: February 26, 2010, 08:48:36 AM »
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

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Adding an updating variable
« Reply #1 on: February 26, 2010, 09:00:07 AM »
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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.

Offline will_1990Topic starter

  • Enthusiast
  • Posts: 131
    • View Profile
Re: Adding an updating variable
« Reply #2 on: February 26, 2010, 09:15:51 AM »
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>&pound;$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!

Code: [Select]
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

Offline will_1990Topic starter

  • Enthusiast
  • Posts: 131
    • View Profile
Re: Adding an updating variable
« Reply #3 on: February 26, 2010, 09:30:56 AM »
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!!

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.