PHP在条件上忽略了foreach

时间:2018-04-01 14:25:31

标签: php yii2

我有一些记录,如果它不是空的,我想在foreach中迭代。

我的代码是这样的:

$count =2;
if($count > 0){     
foreach($userchild as $userchild){?>
<?php } ?>

//some html fields

?php
if($count > 0){
        }
    }

上面的代码在空的时候会产生错误,但是如果我评论foreach行就可以了。

例如:

$count =2
if($count > 0){     
   // foreach($userchild as $userchild){?>
    <?php } ?>

    //some html fields

    ?php
    if($count > 0){
   //         }
        }

我在这里缺少什么以及如果算数&gt;我如何可以忽略foreach 0

更新_form代码:

$count =2;
if($count > 0){

foreach($userchild as $userchild){?>
<?php } ?>

    <div id="kids">
    <div class="r-group">  
    <div class="form-group form-material"> 

 <?= $form->field($userchild,  '['.$id.']'.'child_name')->textInput( ['placeholder' => 'Kids Name','class' => 'col-sm-4','data-pattern-name' => 'UserChildren[++][child_name]', 'data-pattern-id' => 'userchildren-child_name-++'])->label(FALSE)?>



<?= $form->field($userchild, '['.$id.']'.'child_birth_date')->textInput( ['placeholder' => 'Kids Birth Date', 'class' => 'col-sm-4','data-pattern-name' => 'UserChildren[++][child_birth_date]', 'data-pattern-id' => 'userchildren-child_birth_date-++'])->label(FALSE)?>

 <?= $form->field($userchild, '['.$id.']'.'child_gender')->textInput( ['placeholder' => 'Kids Gender', 'class' => 'col-sm-4','data-pattern-name' => 'UserChildren[++][child_gender]', 'data-pattern-id' => 'userchildren-child_gender-++'])->label(FALSE)?>
        </div>            
            </div>

<?php
if($count > 0){
}
}

 ?>  

控制器代码:

public function actionUpdateProfile() {
        $user_id = Yii::$app->user->identity->id;
        $model = User::find()->where(['id' => $user_id])->one();

    $UserProfile = UserProfile::find()->where(['user_id' => $model->id])->one();
            $userbillinginfo = UserBillingInfo::find()->where(['user_id' => $model->id])->one();
            $userchildren = UserChildren::find()->where(['user_id' => $model->id])->all();
            //var_dump($userchildren);

        if($userchildren){
        $profile = $UserProfile;
                    $billinginfo= $userbillinginfo; 
                    $userchild = $userchildren;                      
            } 

            else {
        $profile = new UserProfile;                        
        $profile->user_id = $model->id;
                    $billinginfo = new UserBillingInfo;
                    $billinginfo->user_id = $model->id;                        
                    $userchild = New UserChildren;
                    $userchild->user_id = $model->id;

    }
    if (Yii::$app->request->isAjax && $model->load($_POST)) {
        Yii::$app->response->format = 'json';
        return \yii\bootstrap\ActiveForm::validate($model);
    }

            if (Yii::$app->request->isAjax && $profile->load($_POST)) {
        Yii::$app->response->format = 'json';
        return \yii\bootstrap\ActiveForm::validate($profile);
    }

            if (Yii::$app->request->isAjax && $billinginfo->load($_POST)) {
        Yii::$app->response->format = 'json';
        return \yii\bootstrap\ActiveForm::validate($billinginfo);
    }

            if (Yii::$app->request->isAjax && $userchild->load($_POST)) {
        Yii::$app->response->format = 'json';
        return \yii\bootstrap\ActiveForm::validate($userchild);
    }

    if ($model->load(Yii::$app->request->post()) && $profile->load(Yii::$app->request->post()) && $billinginfo->load(Yii::$app->request->post()) ) {
        //UserChildren::deleteAll('user_id = '. $model->id);
        $model->username = $model->email;
        $model->save();
        $profile->save();
        $billinginfo->save();


if (!empty($_POST['UserChildren']) && !is_null($_POST['UserChildren'])) {
           foreach($_POST['UserChildren'] as $rows){ 
$userchild = New UserChildren;
                         Yii::$app->db->createCommand()->update('user_children', ['child_name' => $rows['child_name'],'child_birth_date' =>$rows['child_birth_date'],'child_gender' =>$rows['child_gender']])->execute();
            $userchild->user_id = $model->id; 
            $userchild->attributes=$rows;  
            $userchild->save();
                        }
                }             

            return $this->redirect(['view']);
        } else {
            return $this->render('update-profile', [
                'model' => $model,
                'profile' => $profile,
                 'billinginfo' => $billinginfo,
                 'userchild' => $userchild,
            ]);
        }
    }

现在我使用$userchild as userchild而不是array as item 所以当忽略你的时候,foreach默认是来自控制器的字段名中的$ userchild。

3 个答案:

答案 0 :(得分:1)

您可以简单地使用所需代码的正确回声,避免混合代码,例如:

<?php  
    $count =2;
    if($count > 0){

      foreach($userchild as $userchild){

        echo '<div id="kids">
        <div class="r-group">  
        <div class="form-group form-material"> ';

         echo  $form->field($userchild,  '['.$id.']'.'child_name')
                 ->textInput( ['placeholder' => 'Kids Name','class' => 'col-sm-4',
                       'data-pattern-name' => 'UserChildren[++][child_name]',
                        'data-pattern-id' => 'userchildren-child_name-++'])->label(FALSE);

        echo  $form->field($userchild, '['.$id.']'.'child_birth_date')
                 ->textInput( ['placeholder' => 'Kids Birth Date', 'class' => 'col-sm-4',
                         'data-pattern-name' => 'UserChildren[++][child_birth_date]', 
                         'data-pattern-id' => 'userchildren-child_birth_date-++'])->label(FALSE);

        echo $form->field($userchild, '['.$id.']'.'child_gender')
                  ->textInput( ['placeholder' => 'Kids Gender', 'class' => 'col-sm-4',
                         'data-pattern-name' => 'UserChildren[++][child_gender]', 
                         'data-pattern-id' => 'userchildren-child_gender-++'])->label(FALSE);
         echo '</div>            
                    </div>';

      } // end foreach  
  } // end if($count > 0){
?>  

答案 1 :(得分:1)

您可以使用一个查询: https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#lazy-eager-loading

$model = User::find()->with(['userprofile','userbillinginfo','userchildren'])->where(['id' => $user_id])->one();

if(isset($model->userchildren)) {
   foreach($model->userchildren as $child) {
       ....
   }
 }

答案 2 :(得分:0)

我认为您的PHP语法不正确。 请试试这样。

$count =2;
if($count > 0){     
foreach($userchild as $userchild){
<?php } ?>

//some html fields
<?php } ?>