简体   繁体   中英

PHP built html table

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

在此处输入图片说明

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

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM