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?  ;)

Link to comment
Share on other sites

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

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.