Jump to content

[SOLVED] Almost there, just need some advice with this code!


merylvingien

Recommended Posts

Hey guys, i hope someone can help me out here.

 

I almost have this code sorted.

 

<?php
if(isset($_POST['selected'])) {
   foreach($_POST['selected'] as $item) {
      $sql = "SELECT * FROM Postcode WHERE postcodeID=$item";
      mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
      $result = mysql_query($sql);
  while ($db_field = mysql_fetch_assoc($result)) {
$total += $db_field['priceb'];
print "<div class='redisplay'>" . $db_field['postcodename']. ", ". $db_field['town']. " <a href='#'>Towns Covered<span> {$db_field['stowns']}</span></a> ". $db_field['county']. ", ". $db_field['country']. " £". $db_field['priceb']. "</div> <br />";

}
	  
   }
  
}

print " Total = £$total <br />";

mysql_close($con);
?>



 

The total adds up ok, but i am getting a Notice message stating:Undefined variable: total on line 96

Which is this part: $total += $db_field['priceb'];

 

I know i am missing something here

 

Anyone see where i am wrong?  ;)

Thanks for the reply, but i tried that and the result is then the total only equals the last row from the database.

 

so for example:

 

bla bla bla 1 = £20.00

 

bla bla bla 2 = £15.00

 

bla bla bla 3 = £10.00

 

total = £10.00

 

:shrug:

<?php
if(isset($_POST['selected'])) {
   foreach($_POST['selected'] as $item) {
      $sql = "SELECT * FROM Postcode WHERE postcodeID=$item";
      mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR);
      $result = mysql_query($sql);
     $total = 0;
     while ($db_field = mysql_fetch_assoc($result)) {
$total += $db_field['priceb'];
print "<div class='redisplay'>" . $db_field['postcodename']. ", ". $db_field['town']. " <a href='#'>Towns Covered<span> {$db_field['stowns']}</span></a> ". $db_field['county']. ", ". $db_field['country']. " £". number_format($db_field['priceb'], 2) . "</div> <br />";

}
      
   }
  
}

print " Total = £" . number_format($total, 2) . " <br />";

mysql_close($con);
?>

 

Try that, also I heard echo is faster than print as print displays an error on failure.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.