【问题标题】:How to move table column?如何移动表格列?
【发布时间】:2018-07-05 04:57:21
【问题描述】:

我想在表格上显示一个文本文件并为每条记录分配一些代码。

  • 我可以显示文本文件
  • 我可以为每条记录分配代码(尚未完成,仍在开发中。)

但是,我遇到了一个问题。由于某种原因,表格列不匹配。

This is what happened. Heading 3 (Pink colour) the fields do not match the table.

下面是我的代码:

<table border='1'>
<tr>
  <th>Heading 1</th>
  <th>Heading 2</th>
  <th>Heading 3</th>
</tr>

<?php           
$file=file('number.txt');       
foreach($file as $value){   
    $items = explode(" ", $value);
    echo "<tr>";
    echo "<td>$items[0]</td>";
    echo "<td>$items[1]</td>";
}

include("connect.php");//connect to the database.
$sql = mysql_query("select * from code5 where Status='Active' LIMIT 8");

if (mysql_num_rows($sql) > 0)
{
    while ($row = mysql_fetch_array($sql))
    {
        echo "<td>$row[Code]</td>";         
        echo "</tr>";
    }
}
?>
</table>

有人能发现我错过了什么吗?或者帮我移动表格列?

我必须实现一个 CSS 文件来手动调整它吗?

【问题讨论】:

  • 我不太清楚您要做什么。是否有什么东西可以将文件中的行与数据库中的行联系起来?
  • 将最后一个echo "&lt;/tr&gt;";移到while循环外
  • 如上图所示,粉红色的列没有到达顶部。 (你可以看到空白)有没有办法强制这个粉红色的柱子回到顶部?
  • @EXphpworld:您需要先将echo "&lt;tr&gt;"; 也移动到外部foreach 循环中
  • 很多时候查看您的 PHP 输出的 HTML 是有益的。在这里,它类似于this。看到任何问题?也许把 HTML 应该是什么样子(静态地,例如在 jsFiddle 上)放在一起,然后从那里构建 PHP。

标签: php html css html-table


【解决方案1】:

我认为问题在于您没有正确打开和关闭标签。

 foreach($file as $value){   
$items = explode(" ", $value);
echo "<tr>";
echo "<td>$items[0]</td>";
echo "<td>$items[1]</td>";
}

在那部分它会打印出这样的东西。

    <tr>
   <td>something</td>
  <td>something</td>
<tr>
  <td>something</td>
 <td>something</td>
<tr>
   <td>something</td>
<td>something</td>

您需要在循环内部或外部打开和关闭 tr 标签。

【讨论】:

  • 我认为代码实际上需要进入 foreach 循环内部
【解决方案2】:

如果文件中的行与数据库中的行匹配,这应该可以工作

<table border='1'>
<tr>
  <th>Heading 1</th>
  <th>Heading 2</th>
  <th>Heading 3</th>
</tr>

<?php           
$file=file('number.txt');       

include("connect.php");//connect to the database.
$result = mysql_query("select * from code5 where Status='Active' LIMIT 8");

foreach($file as $value)
{   
    $items = explode(" ", $value);
    echo "<tr>";
    echo "<td>$items[0]</td>";
    echo "<td>$items[1]</td>";
    if($row = mysql_fetch_array($result))
    {
        echo "<td>$row[Code]</td>";         
    }
    else
    {
        echo "<td>Empty?</td>";         
    }
    echo "</tr>";
}
?>
</table>

【讨论】:

  • 感谢您的帮助!你大大加快了我的进步!这是修复该表后我的待办事项列表(迁移代码)。我可以问你一个附带问题吗?我将在该 foreach 中添加一个 IF。 $items[1]。 $items[1] = 177,如果 $items[1] == $row['serial']。如果记录的序列号是 177,则回显该代码。如果 $items[1] = 277 则回显匹配记录
  • 这可行吗?还是我必须重写整个事情?
  • 我不确定我是否理解您想要做什么。是否要将文件中的行与数据库中的行匹配?在这种情况下,我们需要知道表中的哪些列被调用,以及数据库中的哪些列应该与文件中的哪些列相匹配。
  • |序列|代码|状态|分配,|177|165156516165|活动|,|277|555444984849|活动|,|377|870045608670|活动|,|177|780807888800|活动|,如果$item[1] = 177 比它分配 165156516165 给它。如果 $item[1] = 277,那么它将分配 555444984849。这可行还是我必须重新考虑结构?顺便说一句,我应该为此提出一个问题,因为这是另一个问题。
猜你喜欢
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 2019-10-19
  • 1970-01-01
  • 1970-01-01
  • 2010-12-06
  • 2021-11-03
相关资源
最近更新 更多