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.