schmidt82 Posted June 22, 2011 Share Posted June 22, 2011 Hi, I'm creating a table in PHP with data from my MYSQL database. The last column of my table is supposed to be the result of a calculation of three of the previous columns. The problem is, it only calculates the first row, and then puts that result into the last column in all of the following rows, instead of calculating each row. Here's my code: echo "<table>"; echo "<tr><th>Antal</th> <th>Type</th> <th>Mærke</th> <th>Str.</th> <th>Pris</th> <th>RESULT</th></tr>"; while($result = mysql_fetch_assoc($findBarId_query)){ $resultCalc = ($pris / ($str * $antal)) * 0.5; echo "<tr><td>"; echo $result ['antal']; echo "</td><td>"; echo $result ['type']; echo "</td><td>"; echo $result ['maerke']; echo "</td><td>"; echo $result ['stoerrelse']; echo "</td><td style='text-align:right'>"; echo $result ['normalpris']; echo "</td><td>"; echo $resultCalc; echo "</td></tr>"; } echo "</table>"; Can anyone help? Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 22, 2011 Share Posted June 22, 2011 There is nothing in your loop to define the variables you are using to calculate $resultCalc. So whatever those values are when the loop starts the result will be using those values on each iteration of the loop. I assume you meant to redefine those values based upon each record. I'm not sure if all three of those variables are supposed to be redefined for each record or not - I only see two that appear to match up to the DB fields listed, but here is an idea of how it might look (make corrections as needed). I also cleaned up the code to be easier to read. echo "<table>\n"; echo "<tr><th>Antal</th> <th>Type</th> <th>Mærke</th> <th>Str.</th> <th>Pris</th> <th>RESULT</th></tr>\n"; while($result = mysql_fetch_assoc($findBarId_query)) { $resultCalc = ($result['normalpris'] / ($str * $result['antal'])) * 0.5; echo "<tr>\N"; echo "<td>{$result['antal']}</td>\n"; echo "<td>{$result['type']}</td>\n"; echo "<td>{$result['maerke']}</td>\n"; echo "<td>{$result['stoerrelse']}</td>\n"; echo "<td style='text-align:right'>{$result['normalpris']}</td>\n"; echo "<td>{$resultCalc}</td>\n"; echo "</tr>\n"; } echo "</table>\n"; Quote Link to comment Share on other sites More sharing options...
schmidt82 Posted June 22, 2011 Author Share Posted June 22, 2011 Thank you very. This worked perfectly. I do have one question about the changes you made though. What does the /n at the end of cell mean? Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 23, 2011 Share Posted June 23, 2011 The "/n", when used in a double quoted string, will create a line break. It makes the HTML source code much more logical and readable. Instead of: <tr><td>Value 1</td><td>Value 2</td><td>Value 3</td><td>Value 4</td><td>Value 5</td><tr><tr><td>Value 1</td><td>Value 2</td><td>Value 3</td><td>Value 4</td><td>Value 5</td><tr><tr><td>Value 1</td><td>Value 2</td><td>Value 3</td><td>Value 4</td><td>Value 5</td><tr> You would have <tr> <td>Value 1</td> <td>Value 2</td> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> <tr> <tr> <td>Value 1</td> <td>Value 2</td> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> <tr> <tr> <td>Value 1</td> <td>Value 2</td> <td>Value 3</td> <td>Value 4</td> <td>Value 5</td> <tr> 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.