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? Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/ 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; Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931080 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 Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931091 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; Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931092 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. Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931096 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.. Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931101 Share on other sites More sharing options...
.josh Posted October 5, 2009 Share Posted October 5, 2009 move it above your foreach loop... Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931102 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 Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931107 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. Link to comment https://forums.phpfreaks.com/topic/176614-solved-almost-there-just-need-some-advice-with-this-code/#findComment-931111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.