【问题标题】:Not getting multiple records result in foreach没有得到多条记录导致foreach
【发布时间】: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-&gt;type == 'BCard',根据上面的评论,是告诉它应该返回bpoinst还是jpoints
  • 它应该返回 bpoints
  • 对不起,我错过了理解你的问题。我现在就回答。

标签: php laravel


【解决方案1】:

如果我正确理解了您的问题,那么您只获得一种产品的结果的原因是因为您在循环中使用了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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多