yii:if和else在ajax和foreach中

时间:2014-06-13 13:28:18

标签: php yii

当我在代码中添加 else 如果无法正常工作并且只会处理数组的第一部分

Array ( [0] => php [1] => yii9 [2] => milsss [3] => msa [4] => klk )

在这个只有PHP被检查!

   foreach ($columnsArray as $u)
          {
              $masterCommand = Yii::app()->db->createCommand();
              $gname=$this->group;
              if($this->group == $u)
              {
                 $this->addError('group' ," $gname already registered");
                 $masterCommand->reset();
                 $masterCommand->insert('group',array(
                     $this->group=>'1',
                 ));
                 $masterCommand->reset();
                 $this->newId=   $masterCommand->select('max(id)')->from('group')->queryAll();
                 $this->newId= $this->newId[0]['max(id)'];
                  break;
              }

这是其他

  else {
       $this->addError('group' ," $gname not registered");
       break;
       $masterCommand->reset();
       $masterCommand->addColumn('group' , $gname , 'int(1)');
       $masterCommand->reset();
       $masterCommand->insert('group', array(
            $this->group=>'1',
       ));
       $masterCommand->reset();
       $this->newId = $masterCommand->select('max(id)')->from('group')->queryAll();
       $this->newId=   $maxId[0]['max(id)'];
      }  
}

在这两部分中我如何进行更改?

1 个答案:

答案 0 :(得分:2)

必须先清除错误  然后再添加AddError显示

 if ($this->group == $u)
                {
                 $this->clearErrors('group');
                 $this->addError('group' ," $gname already registered");
                 $masterCommand->reset();
                 $masterCommand->insert('group',
                          array
                                  (
                                       $this->group=>'1',
                                   ));
                  $masterCommand->reset();
                  $this->newId=$masterCommand->select('max(id)')->from('group')->queryAll();
                  $this->newId=$this->newId[0]['max(id)'];
                  $test_status=TRUE;
                      continue;
                   }
if($test_status==FALSE)
{
     $this->clearErrors('group');
     $this->addError('group' ," $gname not registered");
     continue;
}