【问题标题】:Vertical Table, data fetched dynamically垂直表,动态获取数据
【发布时间】:2018-01-01 19:33:09
【问题描述】:

我正在尝试制作一张桌子,其中有:

  • 左侧的列名。
  • 数据垂直。
  • 在下一个 sql 行上水平扩展而不是垂直扩展。

简而言之,与普通桌子完全相反。

此外,水平展开的数据在顶部会有一个标题,名为“解决方案 X”(X 为实际列的编号,图片说明一切)。

我添加了一张图片,它应该可以清楚地说明我试图实现的目标,并附有指导方针。!

图片:

执行此操作的代码是:

<table border="1">
  <tr>
    <th></th>
    <th>Solution 1</th>
    <th>Solution 2</th>
    <th>Solution 3</th>
  </tr>
  <?
 while($result = mysql_fetch_array( $data )) 
 { ?>
  <tr>
    <td>SHIPPINGLINE</td>
    <th><? echo $result["SHIPPINGLINE"]; ?></th>
  </tr>
  <tr>
    <td>POL</td>
    <th><? echo $result["POL"]; ?></th>
  </tr>
  <?
 } 

 //This counts the number or results - and if there wasn't any it gives them a little     message explaining that 
 $anymatches=mysql_num_rows($data); 
 if ($anymatches == 0) 
 { 
 echo "Sorry, but we can not find an entry to match your query.<br><br>"; 
 } 

 //And we remind them what they searched for 
 echo "<b>Searched For:</b> " .$findone. " and " .$findtwo;
 } 
 ?>
    </table>

我被卡住了,因为正如您所看到的,不断发生的事情是,获取的下一个“sql 行”总是出现在第一个下方。我想要的是下一个“sql 行”必须位于下一个“解决方案编号”之下。

如果您能帮我解决这个问题,我将不胜感激。如果您需要更多详细信息,请随时询问:)。

【问题讨论】:

  • 每个后面可能有一个循环,所以假设你有三行那么你必须有三个循环。
  • 如果可能,请编辑我的代码,我不知道应该如何循环它。

标签: php sql dynamic html-table


【解决方案1】:
<?php
$output = array();
$c=1;
while($result = mysql_fetch_array( $data )) 
{
  $output[$c]['shippingline'] = $result['shippingline'];
  $output[$c]['pol'] = $result['pol'];
      $c++;
}
?>
<table border="1">
<tr>
<th></th>
<?php
foreach ($output as $key => $html)
{
echo "<th>Solution ".$key."</th>";
}
?>
</tr>
<tr>
    <td>Shipping Line</td>
<?php
foreach ($output as $key => $html)
{
echo "<td>".$html['shippingline']."</td>";
}
?>
</tr>
<tr>
    <td>POL</td>
<?php
foreach ($output as $key => $html)
{
echo "<td>".$html['pol']."</td>";
}
?>
</tr>   
</table>

【讨论】:

  • 非常感谢!完美运行,并且足够干净,我可以理解和重新编辑以添加更多字段/列。你明白了:)。
猜你喜欢
相关资源
最近更新 更多
热门标签