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

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程