简体   繁体   中英

Yii Parse error: syntax error, unexpected 'if' (T_IF), expecting function (T_FUNCTION)

Can you guys please help me with this random error? I am using yii and getting the above error. This is the form from my view file:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'symptomhistory-form',
'enableAjaxValidation'=>false,


)); ?>
    <?php echo $form->errorSummary($model); ?>
    <br/>
    <?php echo $form->textField($model,'symptomCode', array('id'=>'symptomToBeSearchedCode')); ?>
    <?php echo $form->error($model,'symptomCode'); ?>
    <br/>
    <?php echo $form->textField($model,'symptomTitle', array('id'=>'symptomToBeSearchedTitle')); ?>
    <?php echo $form->error($model,'symptomTitle'); ?>
    <br/>
    <?php $this->widget('zii.widgets.jui.CJuiDatePicker',
        array(
            'model'=>$model,
            'attribute'=>'dateSymptomFirstSeen',
            'id'=>'dateSymptomSeen',
            'options'=>array(
                            'showAnim'=>'fold',
                            'dateFormat'=>'yy-mm-dd', //date format set to be compatible with database
                        ),
            'htmlOptions'=>array(   
                            'style'=>'height:20px;'
                        ),


    )); ?>
    <?php echo CHtml::submitButton('Search Symptom(s)', array('name'=>'search'));  ?>
    <?php echo CHtml::submitButton('Add Another Symptom to Search', array('id'=>'addSymptom', 'name'=>'add'));  ?>
</div>

And this is my controller's action:

public function actionSearch()
{
    //initial model creation
    if(!isset($model))
    {
        //initiliaze varaiable to keep count of active records to be created
        $modelCounter=0;
        //initialize empty model array for SymptomHistory ActiveRecords 
        $model=array();
        //initialize empty array for Symptom titles
        $symptomTitles=array();
        $model[$modelCounter]=new Symptomhistory;
    }


    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);


    if(isset($_POST['search']))
    {

        //populate symptom search model attributes with user id, current date, and form input
        $model[$modelCounter]->setAttributes(array(
                                'user_id'=>Yii::app()->user->id,
                                'dateSearched'=>date('Y-m-d'),
                                'symptomCode'=>$_POST['Symptomhistory']['symptomCode'],
                                'dateSymptomFirstSeen'=>$_POST['Symptomhistory']['dateSymptomFirstSeen'],
                                'symptomTitle'=>$_POST['Symptomhistory']['symptomTitle'],
                                 ));

        //save models
        foreach($model as $symptomHistoryModel)
        {
            $symptomHistoryModel->save();
        }

        $this->redirect(array('disease/index', 'symptomCode'=>$_POST['Symptomhistory']['symptomCode'])); 
        }
    }

    if(isset($_POST['add']))
    {
        //populate symptom search model attributes with user id, current date, and form input
        $model[$modelCounter]->setAttributes(array(
                                'user_id'=>Yii::app()->user->id,
                                'dateSearched'=>date('Y-m-d'),
                                'symptomCode'=>$_POST['Symptomhistory']['symptomCode'],
                                'dateSymptomFirstSeen'=>$_POST['Symptomhistory']['dateSymptomFirstSeen'],
                                'symptomTitle'=>$_POST['Symptomhistory']['symptomTitle'],
                                 ));
        //increase counter
        $modelCounter++;
        $model[$modelCounter]=new Symptomhistory;
        $this->refresh();
    }

    $this->render('search',array(
        'model'=>$model[$modelCounter],'symptomTitles'=>$symptomTitles
    ));
}

I am getting the error in the controller on line:

if(isset($_POST['add']))

Thank you for your help.

Take out the curly bracket after:

$this->redirect(array('disease/index', 'symptomCode'=>$_POST['Symptomhistory']['symptomCode']));

... inside the if(isset($_POST['search'])) statement. Looks like the remnants of a previous version of your foreach loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM