2015-11-07 56 views
1

在一个php库存管理应用程序中,假设我有两个sql表:tblAcqtblInvoice。两者相同(ID, Number, Note, Date)但具有不同的数据。用于sql查询的PHP表模板

我有两页:acq.phpinvoice.php。这两个页面都显示来自查询(SELECT * FROM tblX)的html表格。

因为该表是在FROM tblX (tblAcq or tblInvoice)两个页面以及两者之间唯一的区别是相同的,有没有什么办法可以使该表(例如orders.php)一template,并将其纳入acq.phpinvoice.php

我新的PHP和我不知道这一点,因为该表必须在一段时间条款

的代码是这样的acq.php页:

​​
+0

onblur中'saveToDatabase()'函数的内容是什么?这是否也必须根据页面进行调整? – Thaillie

+0

保留一个公共页面并添加一个函数以从表格中检索数据,将表格名称传递给函数 –

+0

@Thaillie'saveToDatabase()'函数使用javascript和单独的php文件进行数据库行的内联更新。我认为只需要指向一个不同的php文件来更改已更新的表格 –

回答

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是ASSOC阵列。所以你的最终代码将是,

<?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); 
      } 
     } 
?> 
+0

好吧,但我仍然无法理解如何生成两个html表(来自两个sql语句),而无需重复代码。假设我希望他们在同一页面上展示以简化此事。如何从两个mysql表生成两个html表而不重复html代码。我需要完全相同的表两次,唯一不同的是在选择FROM tblAcq和FROM tblInvoice。 –

+0

用循环创建一个函数并传递assoc数组作为参数。 –

+0

我真的不知道......在这种情况下,assoc数组是什么? –