【发布时间】:2018-08-21 16:57:19
【问题描述】:
我正在尝试获取每个对象的分数,最后我需要添加这些分数。
我正在使用 foreach 循环,但结果只得到一个对象点。
请帮我把所有物体的积分作为总和
MODEL:
public function getTransactionPoint($transactionId)
{
$transaction = Transaction::find($transactionId);
$transactionCoupon = TCoupon::where('transaction_id', '=', $transactionId)->first();
$transactionPoint = TPoint::where('transaction_id', '=', $transactionId)->first();
$pointType = PointType::find($this->pointUser->point_type_id);
//to get transaction details
$trans_detail = DB::table('jocom_transaction_details AS a')
->select('a.*', 'b.*')
->leftJoin('jocom_products AS b', 'a.sku', '=', 'b.sku')
->where('a.transaction_id', '=', $transactionId)->get();
//for bpoints
if($pointType->type == 'BCard' )
{
foreach ($trans_detail as $value)
{
$date1=$value->valid_from;
$date2=$value->valid_to;
$startdate=strtotime($date1);
$enddate=strtotime($date2);
$in_range=false;
$out_of_range=false;
$nodate=false;
$today1= date("Y-m-d");
$today=strtotime($today1);
if(($today >= $startdate) && ($today <=$enddate))
{
$in_range=true;
}
else
{
$in_range=false;
$out_of_range=true;
}
if($in_range)
{
if($value->multibpoints)
{
$points = $value->multibpoints *$value->price;
}
elseif($value->bpoints)
{
$points = $value->bpoints;
}
}
if($out_of_range)
{
$points = ($value->price) * $pointType->earn_rate;
}
if ($points - floor($points) > 0.99) {
return floor($points) + 1;
} else {
return floor($points);
}
}//endforeach
}else
//for jpoints
foreach ($trans_detail as $key=>$value) {
$date3=$value->valid_from;
$date4=$value->valid_to;
$startdatee=strtotime($date3);
$enddatee=strtotime($date4);
$in_rangee=false;
$out_of_rangee=false;
$nodatee=false;
$today3= date("Y-m-d");
$today4=strtotime($today3);
if(($today4 >= $startdatee) && ($today4 <=$enddatee))
{
$in_rangee=true;
}
else
{
$in_rangee=false;
$out_of_rangee=true;
}
if($in_rangee)
{
if($value->multijpoints)
{
$points = $value->multijpoints * $value->price;
}
elseif ($value->jpoints) {
$points = $value->jpoints;
}
}
if($out_of_rangee)
{
$points = ($value->price) * $pointType->earn_rate;
}
if ($points - floor($points) > 0.99) {
return floor($points) + 1;
} else {
return floor($points);
}
}
}
}
}
假设产品 A 有 22 个 jpoints 和 33 个 bpoints,产品 b 有 10 个 jpoints 和 12 个 bpoints,我只得到 A 产品的点,但我需要输出作为产品 a 和产品 b 点的总和
我正在尝试获取每个对象点,最后我需要添加这些点。我正在使用 foreach 循环。但是我只得到一个对象点,因此请帮助我将所有对象点作为总和
【问题讨论】:
-
你使用的是什么版本的 Laravel?
-
我正在使用 laravel 4.2
-
我想这是因为
$pointType->type == 'BCard',根据上面的评论,是告诉它应该返回bpoinst还是jpoints。 -
它应该返回 bpoints
-
对不起,我错过了理解你的问题。我现在就回答。