【问题标题】:Set fields in Structure HTML with PHP7 [duplicate]使用 PHP7 在结构 HTML 中设置字段 [重复]
【发布时间】:2018-03-29 06:43:21
【问题描述】:

我在 htlm5 中使用结果查询设置字段时遇到问题。

{
    $query = "SELECT nome,cognome FROM utente";
    $result = $mysqli->query($query);
    while($rows = $result->fetch_all(MYSQLI_ASSOC)){
        print_r($rows);
    };
}

我怎样才能在这个结构中得到这个结果?

<tr>
  <td>??? field 1</td>
  <td>??? field 2</td>
</tr>

【问题讨论】:

    标签: php html mysqli html-table


    【解决方案1】:

    嗯,$rows 只是一个数组,其中包含您在查询中请求的字段,因此您可以使用索引来访问它的字段。

    <?php
    $rows = mysqli_query($conn,$query);
    mysqli_fetch_all($rows,MYSQLI_ASSOC);
    $html_string = "<table> <thead>....whatever optional... </thead> <tbody>";
    foreach( $rows as $r){
      $html_string .= "<tr> <td>" . $r[0] . "</td><td>" . $r[1] . "</td></tr>";
    }
    $html_string .= "</tbody></table>";
    echo $html_string; 
    ?>
    

    对于表格的行应该足够了。

    【讨论】:

    • 谢谢!!!另一个问题,如果我在 html5 中有一个引导结构,我必须把这个结构放在 echo 中的 foreach 中吗?
    • 不,您不希望标签 为每一行重复...您应该将 echo "
      " 放在 foreach 之前。让我编辑答案,也许你可以接受。
    • 您注意到代码中的 MYSQLI_ASSOC 了吗?您认为它的用途如何?
    • 只是想提供一些帮助,尽管这很容易......如果需要,我什至可以删除答案。
    • 不需要删除,但如果能保持一致就好了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2018-12-25
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多