我使用php提取了值,但表已重复

时间:2019-01-10 11:06:28

标签: php mysqli

我使用PHP从数据库(MySQLi)中获取了值,然后将它们呈现在HTML表格中,但是我看到了每一行的表格。

我希望它们在同一张桌子中

$sql = "SELECT val1, val2, val3 FROM TABLE_NAME";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row_inbox = $result->fetch_array()) {
        $var1_id=$row_inbox["val1"];
        $var2_id=$row_inbox["val2"];
        $var3_id=$row_inbox["val3"];
        echo "
        <div class='container'>

        <table class='table table-bordered'>
            <thead>
                <tr>
                    <th>Title 1 </th>
                    <th>Title 2 </th>
                    <th>Title 3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>$var1</td>
                    <td>$var2</td>
                    <td>$var3</td>
                </tr>
            </tbody>
          </table>
          ";
    }
} else {
    echo "0 results";
}

我希望结果将在一个表中,但它们在许多表中(这取决于MySQL表中的许多行)

5 个答案:

答案 0 :(得分:0)

table中删除 while

echo "<table class='table table-bordered'>
    <thead>
      <tr>
        <th>Title 1 </th>
        <th>Title 2 </th>
        <th>Title 3</th>
      </tr>
    </thead>
    <tbody>";

$sql = "SELECT val1, val2, val3 FROM TABLE_NAME";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row_inbox = $result->fetch_array()) {
        $var1_id=$row_inbox["val1"];
        $var2_id=$row_inbox["val2"];
        $var3_id=$row_inbox["val3"];
        echo "
      <tr>
        <td>$var1</td>
        <td>$var2</td>
        <td>$var3</td>
      </tr>

        ";
    }
} else {
    echo "0 results";
}
echo '</tbody></table>';

答案 1 :(得分:0)

尝试一下

$sql = "SELECT val1, val2, val3 FROM TABLE_NAME";
$result = $conn->query($sql);

$createTable = "
<div class='container'>

<table class='table table-bordered'>
<thead>
<tr>
<th>Title 1 </th>
<th>Title 2 </th>
<th>Title 3</th>
</tr>
</thead>
<tbody>";

if ($result->num_rows > 0) {
    // output data of each row
    while($row_inbox = $result->fetch_array()) {
        $var1_id=$row_inbox["val1"];
        $var2_id=$row_inbox["val2"];
        $var3_id=$row_inbox["val3"];

        $createTable .= "         
            <tr>
                <td>$var1</td>
                <td>$var2</td>
                <td>$var3</td>
            </tr>
        ";
    }

    $createTable .= "</tbody></table>";

    echo $createTable;
} else {
    echo "0 results";
}

答案 2 :(得分:0)

您需要从行循环中删除表的打开(和关闭)HTML:

$sql = "SELECT val1, val2, val3 FROM TABLE_NAME";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "
      <table class='table table-bordered'>
        <thead>
          <tr>
            <th>Title 1 </th>
            <th>Title 2 </th>
            <th>Title 3</th>
          </tr>
        </thead>
        <tbody>";

    // output data of each row
    while($row_inbox = $result->fetch_array()) {
        $var1_id=$row_inbox["val1"];
        $var2_id=$row_inbox["val2"];
        $var3_id=$row_inbox["val3"];
        echo "
          <tr>
            <td>$var1</td>
            <td>$var2</td>
            <td>$var3</td>
          </tr>";
    }

    echo "
        </tbody>
      </table>";
} else {
    echo "0 results";
}

答案 3 :(得分:0)

只是简单起见,如果可能的话,表和标头应该是循环的,并且应该不在php标记之内,因此与其呼应,您可以简单地编写html标记。并在循环中写入与数据一起组成的主体元素。

<div class='container'>
    <table class='table table-bordered'>
        <thead>
            <tr>
                <th>Title 1 </th>
                <th>Title 2 </th>
                <th>Title 3</th>
            </tr>
        </thead>
        <tbody>

            <?php 
            $sql = "SELECT val1, val2, val3 FROM TABLE_NAME";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                // output data of each row
                while($row_inbox = $result->fetch_array()) {
                    $var1_id=$row_inbox["val1"];
                    $var2_id=$row_inbox["val2"];
                    $var3_id=$row_inbox["val3"]; ?>
                    <tr>
                        <td><?=$var1?></td>
                        <td><?=$var2?></td>
                        <td><?=$var3?></td>
                    </tr>
                <?php }
            } else { ?>
                <tr>
                    <td colspan=3 align='center'>No Data Found</td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
</div>

答案 4 :(得分:0)

因为您在while loop中添加了所有表格标签 请参考此链接以使用while

这是为什么使用while循环的正确方法

<?php 
    $data_result = "";
    $sql = "SELECT val1, val2, val3 FROM TABLE_NAME";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row_inbox = $result->fetch_array()) {
            $var1_id=$row_inbox["val1"];
            $var2_id=$row_inbox["val2"];
            $var3_id=$row_inbox["val3"]; ?>
            $data_result .= "<tr><td>".$var1."</td><td>".$var2."</td><td>".$var3."</td></tr>";
        <?php }
    } else { ?>
        $data_result .= "<tr><td colspan=3 align='center'>No Data Found</td></tr>";
    <?php } ?>

<div class='container'>
    <table class='table table-bordered'>
        <thead>
            <tr>
                <th>Title 1 </th>
                <th>Title 2 </th>
                <th>Title 3</th>
            </tr>
        </thead>
        <tbody>
            <?php echo $data_result; ?>
        </tbody>
    </table>
</div>