duanlan3598 2014-12-03 16:45
浏览 25
已采纳

使用PHP创建动态HTML表

I'm trying to create a dynamic html table while querying the DB using PHP. I'm having a bit of difficulty understanding how to append two different loops to it to ensure the table I want to create is generated correctly.

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

As you can see I have to append two rows worth of information in to one HTML table row and on the 3rd row I need to append a


and create a new HTML table while continuing my query.

Currently my PHP code looks something like this.

    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?

    }

Please note that I am using Codeigniter, however I am just looking for some help in logic.

Let me know if I can help clear things up.

Thank you for taking your time to read this. Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dtnd30892 2014-12-03 17:07
    关注

    First, do just one query:

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

    Set a counter

    $i = 1;
    

    Then start your table code

    $html_output .= '<table>';
    

    Now loop through the results, create the rows and look for that counter

    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++;
    }
    
    • The $i % 2 == 1 part starts a new table row before each odd item (1, 3, 5, ...).

    • The $i % 2 == 0 part ends the table row after each even item (2, 4, 6, ...).

    • The $i % 6 == 0 part adds a line after each third row (actually after each sixth item and since there are two items per row it's after three rows).

    Finally close the table but first check if we have an even number of items. If not, add an empty table cell.

    if ($i % 2 == 1) {
        $html_output .= '<td></td></tr>';
    }
    $html_output .= '</table>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型