[英]Php table template for sql queries
In a php inventory management application let's say I'm having two sql tables : tblAcq and tblInvoice . 在php库存管理应用程序中,假设我有两个sql表: tblAcq和tblInvoice 。 Both identical (ID, Number, Note, Date)
but with different data. 两者相同(ID, Number, Note, Date)
但数据不同。
I have two pages : acq.php and invoice.php . 我有两个页面:acq.php和invoice.php。 Both pages display a html table from a query (SELECT * FROM tblX)
. 这两个页面都显示查询的html表(SELECT * FROM tblX)
。
Because the table is identical on both pages and the only difference between the two is in the FROM tblX (tblAcq or tblInvoice)
, is there any way I can make a template
for the table (for example orders.php
) and include it in acq.php
and invoice.php
? 因为该表在两个页面上都是相同的,并且两者之间的唯一区别是在FROM tblX (tblAcq or tblInvoice)
,所以有什么方法可以为该表创建template
(例如orders.php
)并将其包含在acq.php
和invoice.php
?
I'm new to php and I can't figure this out because the table must be in a while clause 我是php的新手,我无法弄清楚这是因为该表必须位于while子句中
The code is something like this for acq.php page: 对于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
}
}
you can embed html in php using echo statements. 您可以使用echo语句将html嵌入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()) {
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>";
}
}
?>
EDIT: 编辑:
In your case, $rowacq
is an assoc array. 在您的情况下, $rowacq
是一个assoc数组。 So your final code will be, 所以您的最终代码将是
<?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);
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.