【问题标题】:Php code to dynamically display products report动态显示产品报告的php代码
【发布时间】:2019-02-04 15:20:36
【问题描述】:

我正在将我的 POS 软件迁移到基于 Web 的版本,一切都很顺利,直到我想动态显示销售产品。

产品列表

销售表

我使用 implode 插入我的记录,当我爆炸时效果很好。

这里的问题是我想显示基于产品的所有销售,即从销售表中分解 product_id 并使用每个 id 来获取产品名称。

还可以根据产品ID分解数量并将所有数量加在一起,还可以分解实际价格并显示它等等。

我得到的不是我想要的

这就是我想要的

 <table id="salestable" class="table table-bordered table-striped table-hover">
                    <thead>
                      <tr class="titlerow" role="row">
                      <th >Products</th>
                      <th >Sold</th>
                      <th >Cost</th>
                      <th >Income</th>
                      <th >Profit</th>  
                      </tr>
                    </thead>
                    <tbody id="get_product">
                 <?php
                 try{       
    $query = $con->prepare("Select  s.product_id, s.adjust_price, s.real_price, s.quantity,  s.sales_date from tbl_sales s where s.status ='Paid' and  s.store_id = '$store'");
    $query->execute();
    while($row = $query->fetch(PDO::FETCH_ASSOC)){
        ?>      
            <tr>
            <td ><?php foreach(explode("|", $row['product_id']) as  $pID){ get_product_by_id($con, $pID); } ?></td>
            <td ><?php foreach(explode("|", $row['quantity']) as  $qty){ echo $qty; } ?></td>
            <td ><?php foreach(explode("|", $row['real_price']) as  $cost){ echo $cost; } ?></td>
            <td ><?php foreach(explode("|", $row['adjust_price']) as  $income){ echo $income; } ?></td>
            <td ><?php echo '2'; ?></td>
            </tr><?php


                                }                                


    } catch (PDOException $e){
                $_SESSION['error'] = $e->getMessage();
                $log = date('Y-m-d H:m:s').' '.$_SERVER['REQUEST_URI'].' '.$e->getMessage(). "\r\n";
            file_put_contents('../errorlog.txt', $log, FILE_APPEND);
}
                    ?>   </tbody>
                    <tfoot>
                      <tr class="totalColumn">
                        <th >Products</th>
                      <th class="totalCol" >Sold</th>
                      <th class="totalCol" >Cost</th>
                      <th class="totalCol" >Income</th>
                      <th class="totalCol" >Profit</th>
                      </tr>
                      <tr class="footersearch">
<td colspan="8" ><input type="text" class="form-control" name="search_table" id="search_table" title="Type & hit enter to search the table" data-toggle="tooltip" placeholder="Type & hit enter to search the table" style="width:100%;"></td>
</tr>
                    </tfoot>
                  </table>

【问题讨论】:

  • 不,不,不。您不想在数据库中存储序列化数据 (11|12|14|3)。这违背了使用关系数据库的目的。 Normalize 你的表并使用FOREIGN KEY 约束。创建、读取、更新和删除数据将变得更容易管理
  • 为什么要在一条线上存放多个产品?
  • 这是销售点软件,多个客户将购买多个产品。我只是为了在网上进行特定的交易。我们一天可以有超过 100 笔交易。如果 100 个客户购买 100 件商品,那将是假的。使用它可以更轻松地更新销售和编辑销售。报告给我带来了问题
  • 抱歉,@Cid 在这里是正确的。请不要构建这样的系统。
  • @Opeyemiademon 我很想知道您用于更新 id 为 11 的产品价格的查询

标签: php pdo


【解决方案1】:

扩展上面@Cid 的评论,以防你不明白他所说的标准化。

如果您想将销售或发票链接到客户,您需要一个客户表和一个发票表。如果您不想跟上客户信息,只需使用 Invoice 表。将出售的商品链接到发票的一种方法是使用名为 ProductInInvoice 的关系,如下所示。此伪代码不是有效的 SQL,但您会明白的。

Products (
  productId,
  name,
  description,
  price,
  ...
);

Invoice (
  invoiceId
);

ProductInInvoice (
  productId,
  invoiceId,
  quantitySold
);

这里,ProductInInvoice 表中的productId 是 Products 表的外键,invoiceId 是 Invoice 表的外键。

如果您有一个带有 customerId 列的 Customer 表来存储客户信息,您可以将外键 customerId 添加到引用 Customer 表的 Invoice 表中。然后,您可以通过在 SELECT 语句中连接表来查询不仅在该交易中出售了每件商品的数量,还可以查询购买者。

【讨论】:

  • 我完全理解他的意思。事实上,我可以用外键来实现,没有任何问题。只是我几乎完成了这个项目,我已经打破了我的记录。恐怕我不想重新开始
  • 如果它让您感觉好些,我已经在几周内一遍又一遍地重构了我当前的项目。今天终于第一次运行了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多