简体   繁体   English

PHP构建的html表

[英]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. $column是用户的$_GET值。

    $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. 它在第一次运行时不会回显任何内容,因此不会回显起始的tr和td,因此您最终得到一个错误的行(以/ tr标记结尾),该行仅包含一个td值。 Did you mean to put brackets around the two echo statements so that they only happen when firstrun is false? 您是否要在两个echo语句之间加上方括号,以使它们仅在firstrun为false时出现?

first of all, where is $column defined ? 首先, $column在哪里定义?

$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. 这意味着第一次在while循环中,您没有创建表行<tr> ,尽管我猜测浏览器能够“修复”缺少的标记,但这就是-32722-32722出现的原因第一列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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