Confusion in inserting data

If i want to insert data entry in a table from 2 tables.

Table 1-StudentCourseRelation

Table 2-Course

Table 3-User

I want to display contents from table 2 and table 3 in table 1.It shows data but if student registered already in Table-1,data should not be entered again.But in my code,it is registering same course multiple times.

My code is given below:


 public function actionRegister() 

        {

         

             $id=Yii::app()->session['course_id'];

             echo $id;

             $login_id=yii::app()->session['login_id'];

             echo $login_id;

             $model1=new StudentCourseRelation;

             $model_login=  StudentCourseRelation::model()->findAll(array('condition'=>"user_id='$login_id'"));

              $model_course=  StudentCourseRelation::model()->findAll(array('condition'=>"course_id='$id'"));

     

         

              

              if(empty($model_course->course_id)&& empty($model_login->user_id))

              {

                    $model1->course_id=$id;

                    $model1->user_id=$login_id;

                    $model1->save();

                    echo "successfully registered...";

                    

                    

                  

              }

              elseif($model_login->user_id==$login_id)

              {

                  if($model_course->course_id==$id)

                  {

                      echo 'Already kiya hai!!';

                  }

                  else

                  {

                      $model1->course_id=$id;

                    $model1->user_id=$login_id;

                    $model1->save();

                    echo "successfully registered kela ha";

                  }

              }

 else {

     $model1->course_id=$id;

                    $model1->user_id=$login_id;

                    $model1->save();

                    echo "successfully registered..!!";

 }

              

              

       }

              

VIEW-




<htm>

    <head>

         <link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->theme->baseUrl; ?>/css/styles.css" />

    </head>

    <body>


        

            <?php $form=$this->beginWidget('CActiveForm', array(

                    'id'=>'course-form',

                    'enableAjaxValidation'=>TRUE,

                    'clientOptions' => array(

                    'validateOnSubmit'=>true,),

            )); ?>

            <div class="coursediv">

          

              

                <table class="ctable">

                    <tr>

                        <td class="coursetd">Select Course</td>

                        <td><div class="dropdown">

                              <?php 

                    echo $form->errorSummary($model);                 

                    $type_list=CHtml::listData(course::model()->findAll(),'id','course_name');

                    echo CHtml::activeDropDownList($model,'course_name',$type_list,array('empty'=>'Select Course',

                        'onChange'=> Chtml::ajax(array(

                        'type'=>'get',

                        'url'=>  CController::createUrl('Getdata'),

                        'update'=>'#course_details',

                        'data'=>array('id'=>'js:this.value'),

                        'success' => 'function(data)

                            {

                                var obj = JSON.parse(data);

                                var detail = obj.course_details; 

                                var session= obj.session;

                                document.getElementById("drop2").innerHTML=detail;

                                document.getElementById("drop3").innerHTML=session;

                             }',))));

              ?>

              <?php echo $form->error($model,'course_name'); ?>

                            </div>

                        </td>

                    </tr>

                    <tr>

                        <td class="coursetd">Course Details:</td>

                        <td><p id="drop2" class="details"></p></td>

                    </tr>

                    <tr>

                        <td class="coursetd">Course Duration:</td>

                        <td><p id="drop3" class="course"></p></td>

                    </tr>

            

              

                </table>

                                  

                <?php echo CHtml::link('Register',array('Register',)); ?>


<!-- result goes in this div-->

<div id="save_result"></div>

            

            </div><!-- form -->

            <?php $this->endWidget(); ?>

    </body>

</htm>                  






My dropdown is working properly.

I’m confused at what you’re trying to do.

Questions:

Why are you echoing items in your function…this does nothing and you should remove them.

You said you want it in a table…like cgridview?

You will need to show the action for the view because it’s not action register.

You will need to show the action getData (used in your ajax for the drop down).

Why are you using an ajax drop down? It doesn’t seem necessary but i could be missing something.

I would assume you are trying to show a table of courses the user is registered in and also be able to add courses to it…is this correct?

You can create a custom validation rule in your studencourserelation model to prevent from registering more than once for a course.


public function checkIfRegistered($attribute, $params) {

    	$studenCourseModel = StudentCourseRelation::model()->find(array(

        	'condition' => 'user_id=:user_id && course_id=:course_id',

        	'params' => array(':user_id' => $this->user_id, ':course_id' => $this->course_id),

        	'limit' => 1

    	));


    	if (!empty($studenCourseModel) && isset($studenCourseModel)) {

        	$this->addError($attribute, 'You are already registered for this course!');

    	}

	}


	public function rules() {

    	return array(

        	array('course_id', 'checkIfRegistered'),

    	);

	}

also you are doing two queries to find the same thing


$model_login=  StudentCourseRelation::model()->findAll(array('condition'=>"user_id='$login_id'"));

$model_course=  StudentCourseRelation::model()->findAll(array('condition'=>"course_id='$id'"));

 	



you can make it one like this




$studenCourseModel = StudentCourseRelation::model()->findall(array(

        	'condition' => 'user_id=:user_id && course_id=:course_id',

        	'params' => array(':user_id' => $login_id, ':course_id' => $id)

    	));

You should also do checks to make sure the session values exist or your program could throw errors for users. Why are you storing them in sessions as well? I feel this could be greatly reduced if you explained what you are trying to do better.

Thanks I find the solution .I make unique Course_id and user_id.and i am using Try catch to catch the error when user register the same course.again thanks a lot you advice help in reducing the line of codes.

Actually i am new in php and yii as well so i do some silly mistakes.