merylvingien Posted October 5, 2009 Share Posted October 5, 2009 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? Quote Link to comment Share on other sites More sharing options...
.josh Posted October 5, 2009 Share Posted October 5, 2009 It's a notice simply stating that in the first iteration, $total does not exist as a variable. PHP is fine with this, but it's letting you know. To make it go away, simply define it before the loop: $total = 0; Quote Link to comment Share on other sites More sharing options...
merylvingien Posted October 5, 2009 Author Share Posted October 5, 2009 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted October 5, 2009 Share Posted October 5, 2009 It's a notice simply stating that in the first iteration, $total does not exist as a variable. PHP is fine with this, but it's letting you know. To make it go away, simply define it before the loop: $total = 0; Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 5, 2009 Share Posted October 5, 2009 <?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. Quote Link to comment Share on other sites More sharing options...
merylvingien Posted October 5, 2009 Author Share Posted October 5, 2009 Thanks Andy, still no fix. Still displays last row price only This has to be something really simple that is escaping me.. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 5, 2009 Share Posted October 5, 2009 move it above your foreach loop... Quote Link to comment Share on other sites More sharing options...
merylvingien Posted October 5, 2009 Author Share Posted October 5, 2009 Doh! Idiot prize of the evening goes to me Thanks Crayon Violent Working like a charm Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 5, 2009 Share Posted October 5, 2009 Lmao, can't believe I missed that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.