0

I'm using the code below to build a table, but because the values in my database table are constantly incrementing, I'm doing some math to work out differences in values (numerically) but this has screwed up the table layout somehow. I've included a screenshot so you can see that the first row beneath the table header is just not right.

$column is a $_GET value from the user.

    $sql = "select * from (select * from mash order by tstamp desc limit 10) s order by s.id";
                   $result = mysql_query($sql);
                   $previous = 0;
                   $firstRun = true;
                   echo "<table id='dataTable' border='1'>";
                   echo "<tr><th>Date</th>
                         <th>Value</th></tr>";
                   while($row = mysql_fetch_array($result)){
                        $difference = $row[$column] - $previous;
                        if (!$firstRun)
                        echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";
                        echo "<td>" . $difference . "</td></tr>";
                        $previous = $row[$column];
                        $firstRun = false;
                   }
              echo "</table>";

enter image description here

My question: Can anyone spot from the code, why the first row would come out like this?

5
  • Is this live? It might be good to see what HTML it produces Commented Jan 24, 2011 at 16:59
  • @Chris, afraid not fella,its sensitive client data, thats why i'm limited to putting a screenshot :( Commented Jan 24, 2011 at 17:01
  • What do you want to do "if (!$firstRun)" ?? In this case you're just removing the start of the line and the first cell, that's why your first line is messed up Commented Jan 24, 2011 at 17:02
  • If i have 3 values: 1,3,4 then you havent got anything before 1 to do the math with, so it misses the first calculation so i get expected results Commented Jan 24, 2011 at 17:03
  • can you try $previous = "0" ? when you use $previous = 0 it's a boolen. Commented Jan 24, 2011 at 17:04

3 Answers 3

3

The problem comes from this line:

 if (!$firstRun)
   echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";

If you don't want to display the first line, use the brackets:

 if (!$firstRun){
   echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";
   echo "<td>" . $difference . "</td></tr>";
 }
Sign up to request clarification or add additional context in comments.

Comments

0

It's your "if" statement. It doesn't echo anything on the first run, so the starting tr and td don't get echoed, so you end up with an incorrect row (ended with the /tr tag) containing only a single td value. Did you mean to put brackets around the two echo statements so that they only happen when firstrun is false?

Comments

0

first of all, where is $column defined ?

$difference = $row[$column] - $previous;

second, this is only executed as of the second iteration

if (!$firstRun)
echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";

This means that the first time in the while loop, you are not creating the table row <tr> , although I'm guessing the browser is able to "fix" the missing tag, but this would be the reason why -32722 appears in the first column.

1 Comment

$column is a $_GET value from the user. Read the question dude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.