PHP - 计算利润 - 1个订单 - >各种产品

时间:2015-10-13 20:18:02

标签: php html mysql

我正在迈出编程的第一步。 我决定从PHP开始......如果我的问题太简单,那就太好了。

我正在商店数据库上进行SQL查询,我得到这样的结果:

-- Reference -- Buying Cost -- Selling Cost ---- QUANTITY--Product Name--

-----GGHH-----------5---------------10--------------1------qwqwqwqwqwqw--

-----WEFJ-----------1----------------3--------------2------asasasasasas--

-----ERWW-----------20--------------25--------------1------zxzxzxzxzxzx--

-----GGHH-----------5---------------10--------------1------rtrtrtrtrtrt--

-----GGHH-----------10--------------20--------------2------fgfgfgfgfgfg--

-----GGHH-----------33--------------55--------------1------jkjkjkjkjkjk--

1个订单有1个参考,1个订单可能有1个或更多产品。

阅读上表,因为每一行=属于订单Z的X数量的某个乘积。

我想要做的是一个带有每个订单参考的HTML表格以及基于销售成本的以下利润 - 购买成本=利润。

我不知道如何做到这一点。

如果每个订单只有1个产品就很容易......但是由于这个结构,我不知道它。

注意:我无法改变MySQL数据库方面的任何内容。我只能读它。

谢谢!

2 个答案:

答案 0 :(得分:1)

感谢Andrew Coder。

我做了一点改变:

//DB
$dbhost = "localhost";
$dbusername = "XXXXXX";
$dbpassword = "YYYYYY";
$dbdatabase = "ZZZZZZ";


$conn = new mysqli($dbhost, $dbusername, $dbpassword, $dbdatabase);     
if($conn->connect_errno > 0){
    die('Error [' . $conn->connect_error . ']');
}

$sql = "Select distinct 
            ps_orders.reference
        From 
            ps_order_detail Inner Join ps_orders On ps_orders.id_order = ps_order_detail.id_order Inner Join ps_order_state On ps_orders.id_order = ps_order_state.id_order_state 
        Where 
            ps_order_state.delivery = 1";


if(!$result = $conn->query($sql)){ 
    die('Error [' . $conn->error . ']');
}

// You'd print your table header here
Print "<table>";
print "<tr><th>Reference</th><th>Profit</th>";

$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
    // Loop through each unique reference
    $thisref = $row['reference'];
    $profit_query = "   
    Select
          ps_orders.reference,
          ps_order_detail.original_product_price,
          ps_order_detail.original_wholesale_price,
          ps_order_detail.product_quantity
    From
          ps_orders Inner Join
          ps_order_detail On ps_orders.id_order = ps_order_detail.id_order Inner Join
          ps_order_state On ps_orders.id_order = ps_order_state.id_order_state
    Where
        ps_orders.reference ="."'".$thisref."'";

   $profit_raw = $conn->query($profit_query);


   // Reset profit value each parent iteration
   $profit_value = 0;
   while($profit_row = $profit_raw->fetch_assoc()) {
       // Loop through this references items
       $buying_cost    = $profit_row['original_wholesale_price'];
       $selling_cost   = $profit_row['original_product_price'];
       $quantity       = $profit_row['product_quantity'];

       // Do profit math
       $this_profit = ($selling_cost - $buying_cost) * $quantity;

       // Now add to profit value
       $profit_value += $this_profit;

       // Clear re-usable vars for good measure
       unset($buying_cost);
       unset($selling_cost);
       unset($quantity);
   }

   // Now print that items profit
   print "<tr>\n";
   print "\t<td>".$thisref."</td>\n";
   print "\t<td>".$profit_value."</td>\n";
   print "</tr>\n";
}

// Close table
print "</table>";

答案 1 :(得分:0)

我相信你想要这样的东西。

// Get list of References without duplicates
$query = "SELECT DISTINCT `Reference` FROM `table` ORDER BY `Reference` ASC";
$ref_raw = mysql_query($query);

// You'd print your table header here
Print "<table>";
print "<tr><th>Reference</th><th>Profit</th>";

while ($ref_row = mysql_fetch_array($ref_raw)){
    // Loop through each unique reference
    $thisref = $ref_row['Reference'];
    $profit_query = "SELECT * FROM `table` WHERE `Reference` = '$thisref'";
    $profit_raw = mysql_query($profit_query);

    // Reset profit value each parent iteration
    $profit_value = 0;

    while ($profit_row = mysql_fetch_array($profit_raw)){
        // Loop through this references items
        $buying_cost    = $profit_row['buying_cost'];
        $selling_cost   = $profit_row['selling_cost'];
        $quantity       = $profit_row['quantity'];

        // Do profit math
        $this_profit = ($selling_cost - $buying_cost) * $quantity;

        // Now add to profit value
        $profit_value += $this_profit

        // Clear re-usable vars for good measure
        unset($buying_cost);
        unset($selling_cost);
        unset($quantity);
    }

    // Now print that items profit
    print "<tr>\n";
    print "\t<td>".$thisref."</td>\n";
    print "\t<td>".$profit_value."</td>\n";
    print "</tr>\n";
}

// Close table
print "</table>";