如何将php echo与html表连接起来

时间:2017-03-27 19:31:25

标签: php html mysql

我想在html表中显示mysql数据... php echo显示了我的内容,但是如何将其转移到html-table中?我已经手动将一些虚拟数据添加到html表中。

<?php
    $sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
         echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
     }
} else {
     echo "00 results";
}

$conn->close();
?>

<h1>Bestellungen</h1>
<div class="countdown" data-role="countdown" data-days="2"></div>

<div class="container" style="padding: 25px 0px;">
    <table class="table striped hovered border" data-role="datatable" data-searching="true">
        <thead>
            <tr>
                <th>id.</th>
                <th>Datum</th>
                <th>Kunde</th>
                <th>Produkt</th>
                <th>Produktversion</th>
                <th>Menge</th>


            </tr>   
        </thead>

        <tbody>
                    <tr><td>2</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Filet</td><td>50</td></tr>
            <tr><td>3</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Frisch</td><td>50</td></tr>
            <tr><td>4</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Lebend</td><td>50</td></tr>

        </tbody>
    </table>
</div>

2 个答案:

答案 0 :(得分:0)

你应该在表内迭代并添加html进入echo

  <div class="container" style="padding: 25px 0px;">
      <table class="table striped hovered border" data-role="datatable" data-searching="true">
          <thead>
              <tr>
                  <th>id.</th>
                  <th>Datum</th>
                  <th>Kunde</th>
                  <th>Produkt</th>
                  <th>Produktversion</th>
                  <th>Menge</th>


              </tr>   
          </thead>

          <tbody>
          <?php 
            while($row = $result->fetch_assoc()) {

             echo "<tr>
                      <td>".$row["id"]. "</td>
                      <td>". $row["Datum"]. "</td> 
                      <td>". $row["Kunde"] . "</td>
                      <td>". $row["Produkt"] .  "</td>
                      <td>". $row["Produktversion"] . "</td>
                      <td>". $row["Menge"] ."</td>
                    </tr>";
             }
           ?>

          </tbody>
      </table>
  </div>

答案 1 :(得分:0)

记住你必须循环每个来为表创建行。

$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        ?>
        <table>
            <thead>
                <tr>
                    <th>id.</th>
                    <th>Datum</th>
                    <th>Kunde</th>
                    <th>Produkt</th>
                    <th>Produktversion</th>
                    <th>Menge</th>
                </tr>   
            </thead>
            <tbody>
                <?php
                while($row = $result->fetch_assoc()) {
                 //echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
                    ?>
                    <td><?php echo $row["id"]; ?></td>
                    <td><?php echo $row["Datum"] ?></td>
                    <td><?php echo $row["Kunde"]; ?></td>
                    <td>---</td>
                    <td>---</td>
                    <td>---</td>
                    <?php
                }
                ?>
            </tbody>
         </table>
         <?php 
    } else {
         echo "00 results";
    }

    $conn->close();

您需要在while循环中创建每一行。