matttherat Posted August 24, 2010 Share Posted August 24, 2010 Hi ive got this code which echos out all the rows from an SQL table. while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['subject1'] . "</td>"; echo "<td>" . $row['subject2'] . "</td>"; echo "<td>" . $row['subject3'] . "</td>"; echo "<td>" . $row['subject4'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } echo "</table>"; All i want to do is add all the 4 subject together and echo it out. simple but not sure how to do it. any ideas please? Thanks Matt Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/ Share on other sites More sharing options...
kenrbnsn Posted August 24, 2010 Share Posted August 24, 2010 What do you mean by: All i want to do is add all the 4 subject together and echo it out Can you give an example? Ken Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103101 Share on other sites More sharing options...
shlumph Posted August 24, 2010 Share Posted August 24, 2010 What do you mean by "add all the 4 subject together"? Are they integers? Are they strings? Do you want to add them together for only each row, or the whole entire result set? Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103102 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2010 Share Posted August 24, 2010 Edit: Basically what they said ^^^ Could you clarify by providing an example of what you mean by 'add all the 4 subject together'? What you posted could mean any of - 1) Add all the same name field values together, 2) Concatenate all the same name field content together, 3) Add up the 'subject1', 'subject2', 'subject3', and 'subject4' field values for each row or for all the rows, 4) Concatenate the 'subject1', 'subject2', 'subject3', and 'subject4' field content for each row or for all the rows. Also, how does your title 'getting a variable from rows' relate to your question? Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103103 Share on other sites More sharing options...
matttherat Posted August 24, 2010 Author Share Posted August 24, 2010 Sorry I wasn't clear. The 4 rows being echoed subject 1, subject 2, 3 and 4 are all integers. I want to add these 4 rows together and echo out the total of the 4 fields combined. the full code is here. <?php $con = mysql_connect("localhost","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mainbase", $con); $result = mysql_query("SELECT * FROM agegame"); echo "<table border='1'> <tr> <th>subject1</th> <th>subject2</th> <th>subject3</th> <th>subject4</th> <th>email</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['subject1'] . "</td>"; echo "<td>" . $row['subject2'] . "</td>"; echo "<td>" . $row['subject3'] . "</td>"; echo "<td>" . $row['subject4'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103108 Share on other sites More sharing options...
shlumph Posted August 24, 2010 Share Posted August 24, 2010 To add the four columns together, you'd want to do something like this: <?php while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . ($row['subject1'] + $row['subject2'] + $row['subject3'] + $row['subject4']) . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103119 Share on other sites More sharing options...
matttherat Posted August 24, 2010 Author Share Posted August 24, 2010 sorry slumph. i had no luck with that code. page went blank when i added it. any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103131 Share on other sites More sharing options...
shlumph Posted August 24, 2010 Share Posted August 24, 2010 During development, you should turn errors on in your php.ini, error_reporting = E_ALL, so you can see what's barking. Just remember to turn it off if you put the code online. What do the errors say? Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103147 Share on other sites More sharing options...
matttherat Posted August 24, 2010 Author Share Posted August 24, 2010 sorry im just testing it straight online. so dont see any errors. Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103152 Share on other sites More sharing options...
shlumph Posted August 24, 2010 Share Posted August 24, 2010 Try adding this to the top of your .php page: <?php error_reporting(E_ALL); ini_set("display_errors", 1); Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103162 Share on other sites More sharing options...
matttherat Posted August 24, 2010 Author Share Posted August 24, 2010 that did nothing either. Im not sure what else to do Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103170 Share on other sites More sharing options...
kenrbnsn Posted August 24, 2010 Share Posted August 24, 2010 A blank screen is an indication of a problem somewhere in your script. Please post your current code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103173 Share on other sites More sharing options...
PFMaBiSmAd Posted August 24, 2010 Share Posted August 24, 2010 You should not be learning php, developing php code, or debugging php code on a live server. You waste a ton of time, both in constantly uploading changes just to see one result and in problems like the error_reporting/display_errors settings. You likely introduced a fatal parse error and you would need to set the error_reporting/display_errors settings in a local php.ini (when php is running as a CGI) or in a .htaccess file (when php is running as an Apache module.) You could post your current code so someone could determine what was wrong with it. You would be better off doing this on a local development system. Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103174 Share on other sites More sharing options...
matttherat Posted August 24, 2010 Author Share Posted August 24, 2010 ah ha. its working. i left a echo in that wasnt formatted properly. the only problem now is it isnt outputting the answers in new rows each time. is reading 103138126 etc when should be 103 138 126 with each answer inline with each row. eg sub1 sub2 sub3 sub4 total 25 + 25 + 25 + 25 = 100 Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103175 Share on other sites More sharing options...
shlumph Posted August 24, 2010 Share Posted August 24, 2010 Does this produce a blank page? <?php while($row = mysql_fetch_array($result)) { $sum = $row['subject1'] + $row['subject2'] + $row['subject3'] + $row['subject4']; echo 'Test sum: ' . $sum; exit; } Just try simple debugging, until you narrow it down... maybe you need to caste the subjects into ints. You may want to consider setting up a development environment on your local computer, so you can turn errors on and have complete control over everything. Once it's working perfectly, upload it to your hosting provider. Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103176 Share on other sites More sharing options...
matttherat Posted August 24, 2010 Author Share Posted August 24, 2010 YAY got it working. thank u everybody for your help. the final code was this. echo "<tr>"; echo "<td>" . $row['subject1'] . "</td>"; echo "<td>" . $row['subject2'] . "</td>"; echo "<td>" . $row['subject3'] . "</td>"; echo "<td>" . $row['subject4'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; $sum = $row['subject1'] + $row['subject2'] + $row['subject3'] + $row['subject4']; echo "<td>" . $sum . "</td>"; echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/211608-getting-a-variable-from-rows/#findComment-1103184 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.