开发者社区> 问答> 正文

格式化SQL PHP脚本

因此,我处于自动取款机上,并且被要求从SQL表中检索信息并将其显示为发票,但是我掌握了基础知识,但是现在我被要求格式化php页面以使其看起来像这样的东西

我对如何执行此操作甚至从何处开始感到困惑。如果您可以就如何开始格式化这样的页面为我指明正确的方向,那很好,我在下面留下了我的代码,任何帮助将不胜感激

require("connect.php");
$inNo = $_POST["inNo"];

$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    //open table
    echo '<table class="table table-striped" id="outTable">';
    echo "<tr><th>Invoice no.</th><th>Date</th><th>Customer ID</th><th>Employee ID</th><th>Product ID</th><th>Qty</th><th>Price</th><th>Total cost</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "
        <tr>
        <td>" . $row["invoice_no"]. "</td>
        <td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
        <td>" . $row["emp_id"]. "</td><td>" . $row["prod_id"]. "</td>
        <td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
        <td>" . $row["(product.cost_price * invoice_line.qty)"]. "</td>
        </tr>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
PHP的 SQL 格式
分享改善这个问题
2小时前 问

帐篷人8001
311枚青铜徽章
这个问题是关于html / css而不是关于php / sql。请在代码段中向我们显示生成的html,并说明它与您想要的内容有何不同 –  Denis Sheremet 1小时前
添加评论
1个回答
活跃的最古老的选票

0

尝试这个:

<?php
require("connect.php");
$inNo = $_POST["inNo"];

$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);

echo"<div style='width:50em'>";

if ($result->num_rows > 0) {
    $count =0;
    while($row = $result->fetch_assoc()) {
        if ($count==0){
          echo '<h2 style="text-align: center">Invoice no. " . $row["invoice_no"]. "</h2>';
          echo '<table width=100%>';          
          echo "<tr style='text-align: left'><th>Date</th><th>Customer ID</th><th>Employee ID</th></tr>";
          echo '<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
            <td>" . $row["emp_id"]. "</td></table>';
          echo '<table class="table table-striped" id="outTable" width=100% style="text-align: left">';
          echo '<th>Product ID</th><th>Qty</th><th>Price</th></tr>';
        }

        echo "
        <tr>
        <td>" . $row["prod_id"]. "</td>
        <td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
        </tr>";
        if ($count==$result->num_rows-1){
          echo '<div style="text-align: right">Total cost: " . $row["(product.cost_price * invoice_line.qty)"]. "</div>';
        }
        $count++;
    }
    echo "</table></div>";
} else {
    echo "0 results";
}

展开
收起
社区秘书 2019-12-10 09:22:19 608 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
SQL Server在电子商务中的应用与实践 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载