繁体   English   中英

用PHP动态创建HTML表

[英]Dynamic HTML table creation with PHP

我正在尝试使用PHP查询数据库时创建动态html表。 我在理解如何附加两个不同的循环以确保正确创建要创建的表方面有些困难。

<table><tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr></table>
<hr>
<table><tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr>
<tr>
   <td>$ID and $ITEM_NAME</td>
   <td>$ID and $ITEM_NAME</td>
</tr></table>
<hr>

如您所见,我必须在一个HTML表格行中添加两行有价值的信息,在第三行中,我需要添加一个


并在继续查询的同时创建一个新的HTML表。

目前,我的PHP代码看起来像这样。

  foreach($item_ID as $x => $x_value) { $sql = "SELECT item_name FROM item_table WHERE id = '$x_value'"; $query = $this -> db -> query($sql); foreach($query->result() as $row){ $item_name = $row->item_name; } $html_output .= '<tr><td>'; $html_output .= $x_value. " ".$item_name; $html_output .= '</td>'; //Not sure how to proceed with closing the rows and opening new one on 3rd query item? } 

请注意,我正在使用Codeigniter,但是我只是在逻辑上寻求帮助。

让我知道是否可以帮助您解决问题。

感谢您抽出宝贵的时间阅读本文。 任何帮助将不胜感激。

首先,只执行一个查询:

$sql = "SELECT id, item_name FROM item_table WHERE id IN ('" . implode("', '", $item_ID) ."')";
$query = $this -> db -> query($sql);

设置柜台

$i = 1;

然后启动您的表代码

$html_output .= '<table>';

现在循环搜索结果,创建行并查找该计数器

foreach($query->result() as $row){
    if ($i % 2 == 1) {
        $html_output .= '<tr>';
    }
    $html_output .= '<td>' . $row->id .' and' . $row->item_name . '</td>';
    if ($i % 2 == 0) {
        $html_output .= '</tr>';
    }
    if ($i % 6 == 0) {
        $html_output .= '</table><hr><table>';
    }
    $i++;
}
  • $i % 2 == 1部分在每个奇数项(1、3、5 ...)之前开始一个新的表行。

  • $i % 2 == 0部分在每个偶数项(2,4,6,...)之后结束表行。

  • $i % 6 == 0部分在每第三行之后添加一行(实际上是在每第六项之后,并且由于每行有两个项目,因此在三行之后)。

最后关闭表格,但首先检查我们是否有偶数个项目。 如果不是,请添加一个空的表格单元格。

if ($i % 2 == 1) {
    $html_output .= '<td></td></tr>';
}
$html_output .= '</table>';

换个说法,这听起来像您想为每个奇数行创建一个新表。 另外,您想用HR标记分隔表。

$i = 0; //initialize as 0 rows
foreach($item_ID as $x => $x_value) {

    $sql = "SELECT item_name FROM item_table WHERE id = '$x_value'";
    $query = $this -> db -> query($sql);

    foreach($query->result() as $row){
        $item_name = $row->item_name;

        $row_output  = '<tr><td>';
        $row_output .= $x_value. " ".$item_name;
        $row_output .= '</td>';
       //presumably you would close the row here too, unless you have more table cells you aren't showing us

        $i++;//we have a row, so increment
        if ( !($i % 2) ) { // if this has a remainder, then the current row counter is odd
          if ( $i != 1 ) {
            //beginning of a table that isn't the first table, add the hr
            $html_output .= '<hr>'; 
          }
          $html_output .= '<table>'; // open table
          $html_output .= $row_output;
        } else { //this is an even numbered row, the last row of the table
          $html_output .= $row_output;
          $html_output .= '</table>'; // close table
        }
    }
}

暂无
暂无

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

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