Jump to content

Calculation in table


schmidt82

Recommended Posts

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

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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>

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.