没有获得多个记录会导致foreach

时间:2018-08-21 02:55:18

标签: php laravel

我试图获取每个对象的点,最后我需要添加这些点。

我正在使用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个jpooints和12 bpoints,我只得到A产品的积分,但是我需要输出作为产品a和产品b积分的总和

am试图获取每个对象点,最后我需要使用foreach循环将这些点添加到.am,但结果我只得到一个对象点,请帮助我获取所有对象点的总和''

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,则之所以只得到一种产品的结果,是因为您在循环中使用了return。相反,您可以定义一个名为$totalPoints之类的变量,将其添加到变量中,然后在循环后返回该值,例如

$totalPoints = 0;

foreach ($trans_detail as $value) {

    //The rest of the code

    if ($points - floor($points) > 0.99) {
        $totalPoints += floor($points) + 1;
    } else {
        $totalPoints += floor($points);
    }

}

return $totalPoints;

此外,您不必使用此功能,但是看起来您可以大大简化您的代码:

public function getTransactionPoint($transactionId)
{
    $pointType = PointType::find($this->pointUser->point_type_id);

    $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();

    $pointsName = $pointType->type == 'BCard' ? 'bpoints' : 'jpoints';

    $totalPoints = 0;

    $today = strtotime(date('Y-m-d'));

    foreach ($trans_detail as $value) {

        if ($today >= strtotime($value->valid_from) && $today <= strtotime($value->valid_to)) {
            $points = $value->multibpoints ? $value->multibpoints * $value->price : $value->$pointsName;
        } else {
            $points = ($value->price) * $pointType->earn_rate;
        }

        $totalPoints += $points - floor($points) > 0.99 ? floor($points) + 1 : floor($points);
    }

    return $totalPoints;
}