用于SQL查询的Php表模板

时间:2015-11-07 14:19:40

标签: php mysql templates

在php库存管理应用程序中,我们假设我有两个sql表: tblAcq tblInvoice 。两者都相同(ID, Number, Note, Date)但数据不同。

我有两页: acq.php invoice.php 。两个页面都显示查询(SELECT * FROM tblX)中的html表。

因为两个页面上的表格相同,而且两者之间的唯一区别在于FROM tblX (tblAcq or tblInvoice),我是否有办法为表格制作template(例如orders.php })并将其包含在acq.phpinvoice.php

我是php新手,我无法解决这个问题,因为该表必须在while子句中

acq.php 页面的代码如下:

<?php
$sqlacq ="SELECT * FROM tblAcq";
$resultacq = $conn->query($sqlacq);
?>
       <table id="acq-tbl" class="tbl-qa" border="1">
          <thead>
              <tr>
                <th class="table-header" width="10%">AcqID</th>
                <th class="table-header">Data (an-luna-zi)</th>
                <th class="table-header">AcqNumber</th>
                <th class="table-header">AcqSupplier</th>
                <th class="table-header">AcqNote</th>
                <th class="table-header">InStocLa</th>

              </tr>
          </thead>
          <tbody>
          <?php
        if ($resultacq->num_rows > 0) {
            while($rowacq = $resultacq->fetch_assoc()) {
           ?>
              <tr class="table-row" ondblclick="mySelection(event)">
                <td><?php echo $rowacq["AcqID"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'AcqDate','<?php echo $rowacq["AcqID"]; ?>')" onClick="showEdit(this);"><?php echo $rowacq["AcqDate"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'AcqNumber','<?php echo $rowacq["AcqID"]; ?>')" onClick="showEdit(this);"><?php echo $rowacq["AcqNumber"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'AcqSupplier','<?php echo $rowacq["AcqID"]; ?>')" onClick="showEdit(this);"><?php echo $rowacq["AcqSupplier"]; ?></td>
                <td contenteditable="true" onBlur="saveToDatabase(this,'AcqNote','<?php echo $rowacq["AcqID"]; ?>')" onClick="showEdit(this);"><?php echo $rowacq["AcqNote"]; ?></td>
                <td><?php echo $rowacq["InStocLa"]; ?></td>
                <td align="center"><a href="javascript:delete_id(<?php echo $rowacq["AcqID"]; ?>)"><img src="b_drop.png" alt="Delete" /></a></td>
              </tr>
        <?php
            }
        }

1 个答案:

答案 0 :(得分:0)

你可以使用echo语句在php中嵌入html。

<?php
$sqlacq ="SELECT * FROM tblAcq";
$resultacq = $conn->query($sqlacq);
?>
       <table id="acq-tbl" class="tbl-qa" border="1">
          <thead>
              <tr>
                <th class="table-header" width="10%">AcqID</th>
                <th class="table-header">Data (an-luna-zi)</th>
                <th class="table-header">AcqNumber</th>
                <th class="table-header">AcqSupplier</th>
                <th class="table-header">AcqNote</th>
                <th class="table-header">InStocLa</th>

              </tr>
          </thead>
          <tbody>
          <?php

        if ($resultacq->num_rows > 0) {
            while($rowacq = $resultacq->fetch_assoc()) {
              echo '<tr class="table-row" ondblclick="mySelection(event)">';
              echo '<td>'.$rowacq["AcqID"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqDate\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqDate"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqNumber\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqNumber"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqSupplier\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqSupplier"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqNote\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqNote"].'</td>';
              echo '<td>'.$rowacq["InStocLa"].'</td>';
              echo '<td align="center"><a href="javascript:delete_id('.$rowacq["AcqID"].')"><img src="b_drop.png" alt="Delete" /></a></td>'
              echo "</tr>";
            }
          }

?>

编辑:

在您的情况下,$rowacq是一个关联数组。所以你的最终代码将是,

<?php
$sqlacq ="SELECT * FROM tblAcq";
$resultacq = $conn->query($sqlacq);
?>
       <table id="acq-tbl" class="tbl-qa" border="1">
          <thead>
              <tr>
                <th class="table-header" width="10%">AcqID</th>
                <th class="table-header">Data (an-luna-zi)</th>
                <th class="table-header">AcqNumber</th>
                <th class="table-header">AcqSupplier</th>
                <th class="table-header">AcqNote</th>
                <th class="table-header">InStocLa</th>

              </tr>
          </thead>
          <tbody>
       <?php
        function table_print($rowacq) {
              echo '<tr class="table-row" ondblclick="mySelection(event)">';
              echo '<td>'.$rowacq["AcqID"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqDate\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqDate"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqNumber\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqNumber"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqSupplier\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqSupplier"].'</td>';
              echo '<td contenteditable="true" onBlur="saveToDatabase(this,\'AcqNote\',\''.$rowacq["AcqID"].'\')" onClick="showEdit(this);">'.$rowacq["AcqNote"].'</td>';
              echo '<td>'.$rowacq["InStocLa"].'</td>';
              echo '<td align="center"><a href="javascript:delete_id('.$rowacq["AcqID"].')"><img src="b_drop.png" alt="Delete" /></a></td>'
              echo "</tr>";

        }
       if ($resultacq->num_rows > 0) {
            while($rowacq = $resultacq->fetch_assoc()) {
                table_print($rowacq);
            }
       }
?>