首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -如何在if isset条件内嵌套while循环?

问PHP -如何在if isset条件内嵌套while循环?
EN

Stack Overflow用户
提问于 2016-05-03 09:39:42
回答 2查看 794关注 0票数 0

我有以下代码的表元素:

代码语言:javascript
复制
<?php
if(isset($_POST["submit"])){
    if (strlen($cIdMsg = 0) && strlen($cFirstNameMsg = 0) && strlen($cLastNameMsg = 0) && strlen($pCodeMsg = 0)) {
        require_once("conn.php");
        $sql2 = "SELECT * FROM customer;";
        $results = mysqli_query($conn, $sql2)
        or die ('Problem with query' . mysqli_error($conn));
        echo "no errors found";
    }
}
?>

<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>

在这个表的上面,我有一个php代码,它在if isset条件下进行sql查询,这样它只能在表单上按submit之后加载。我也想对这张桌子做同样的事。也就是说,只有在按下submit之后才会加载。因为在页面加载时,它正在尝试在不存在的mysqli_fetch_array上执行$result。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-03 09:41:42

把整个桌子包在里面:

代码语言:javascript
复制
<?php if (isset($result)) { ?>
<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>
<?php } ?>

根据你所说的,我使用了isset($result)。您可以通过检查count($_POST)或类似的东西来检查POST值(检查isset($_POST["submit"])不是个好主意)。如果要获取AJAX响应,最好为此使用不同的单独文件。

票数 3
EN

Stack Overflow用户

发布于 2016-05-03 09:47:58

代码语言:javascript
复制
<?php if(mysqli_num_rows($result)!=0) { ?>
<table>
<tr>
    <th>Customer ID</th>
    <th>FIrst Name</th>
    <th>Last Name </th>
</tr>

<?php
    while ($row = mysqli_fetch_array($results)) { ?>
    <tr>
        <td><?php echo $row["customerID"]?></td>
        <td><?php echo $row["firstName"]?></td>
        <td><?php echo $row["lastName"]?></td>
    </tr>
<?php } ?>
</table>
<?php } ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37000663

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档