【问题标题】:echo values and add together inside a loop to make a total [duplicate]回显值并在循环内加在一起以形成总数[重复]
【发布时间】:2018-07-04 12:05:50
【问题描述】:

我最初需要从系统中的单个预订中将 x 金额 [trip_price] 的数组总和相加。这是一个单一的预订,在同一个预订中包含 2 种类型的金额。

总金额为$booking_total = $post_metas['fcb_booking_total'][0];,这反过来又产生了一个静态数字,因此当对预订进行更改时,价格永远不会更新。

$get_price = $wpdb->get_results("select trip_price from fcb_booking where order_id = " . $order_id . " order by start_date_time asc");

print_r($get_price);

Array
(
    [0] => stdClass Object
        (
            [trip_price] => 0
        )

    [1] => stdClass Object
        (
            [trip_price] => 5000
        )

)

编辑。

从下面的答案中我使用了这个。这使得更新将价格更改为用户的新价格等。这需要在电子邮件确认中显示。

$get_price = $wpdb->get_results("select trip_price from fcb_booking where order_id = " . $order_id . " order by start_date_time asc");
$booking_total = 0;
foreach($get_price as $key => $value) {
    $booking_total += $value->trip_price;
};

如果用户在网站上支付了 100% 或 50% 的选项、任何管理员折扣和 5% 的额外费用,我用它来返回价格

$total = $booking_total + $extra_total;
$total = $total * 1.05;


if($need_to_pay){
    if($amounttopay == 50){
        $total = $total / 2;
    }
}

if($admin_discount>0){
    $total = $total - $admin_discount;
}

return $total;

【问题讨论】:

    标签: php


    【解决方案1】:

    我不确定您的目标是打印出每个元素和/或计算总和。

    有几种方法可以实现:

    1. 全SQL:依靠SQL引擎求和

      $totalAmount = $wpdb->get_results("
          SELECT SUM(trip_price) AS total_amount
          FROM fcb_booking
          WHERE order_id = " . $order_id . " ORDER BY start_date_time ASC");
      

      在这种情况下,您无法真正显示要添加的每个元素。

    2. 在 PHP 中循环

      $prices = $wpdb->get_results("
          SELECT trip_price
          FROM fcb_booking
          WHERE order_id = " . $order_id . "
          ORDER BY start_date_time ASC");
      
      // Array map gives $arr = [0, 5000];
      $arr = array_map(function ($el) {
          return $el->trip_price;
      }, $prices);
      
      // Adding up prices: 0 + 5000 = 5000
      $sum = array_sum($arr);
      
      var_dump($sum);
      // => int(5000)
      

      如果您想打印每个价格,只需在 array_map 的可调用对象中添加一个 echo $el

    【讨论】:

      【解决方案2】:

      你可以在php中使用array_sum()

      array_sum — 计算数组中值的总和

      $arr = ["trip_price" => 1, "trip_price" => 5000];
      $sum = array_sum($arr);
      echo $sum;
      

      【讨论】:

        【解决方案3】:

        使用循环foreach 回显和求和:

        $sum = 0;
        foreach ($get_price as $price) {
            echo "price =" . $price->trip_price . "\n";
            $sum += $price->trip_price;
        }
        
        echo "Total sum=" . $sum;
        

        【讨论】:

        • 请检查代码。那里解析错误。
        • 我更正了代码@Pupil
        • 不客气。
        【解决方案4】:

        检查数组是否不为空。

        为此使用 PHP 的 empty() 函数。

        如果我们不为此添加检查,我们最终会出错。

        遍历数组。

        取一个变量来保存总计。

        在每次迭代中添加总和。

        $get_price = $wpdb->get_results("select trip_price from fcb_booking where order_id = " . $order_id . " order by start_date_time asc");
        $total = 0;
        if (! empty($get_price)) {
         foreach($get_price as $key => $value) {
          $total += $value->trip_price;
         }
        }
        echo $total;
        

        【讨论】:

        • 不错的答案和 cmets,谢谢。
        【解决方案5】:

        试试这个代码:

        $total = 0;
        foreach($get_price as $key => $value) {
            $total += $value->trip_price;
        }
        
        echo $total;
        

        【讨论】:

        • 这看起来像是我自己试图实现的目标。但是失败了:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-08
        • 1970-01-01
        • 2018-02-18
        • 2021-06-18
        • 2017-07-31
        • 2021-10-19
        • 1970-01-01
        相关资源
        最近更新 更多