Hello guys hope you are well,
I’ve been doing a small timetable for a school. I am using tabular input for this where inside a HTML table i have distributed my attributes
part of my view is shown below
<?php
for($i=0;$i<8;$i++):?>
<tr>
<td>
<div class="row" id="updateClassId">
<?php
$sql='SELECT c.classId
FROM schoolclasses c';
$connection=Yii::app()->db;
$command=$connection->createCommand($sql);
?>
<?php
echo $form->dropDownList($model,"[$i]classId", CHtml::listData($command->query(), 'classId', 'classId'),
array('empty'=>'---Select Class---',
));
?>
<?php echo $form->error($model,"[$i]classId"); ?>
</div>
</td>
<td>
<?php
echo $form->dropDownList($model,"[$i]subjectName", CHtml::listData(Subjects::model()->findAll(), 'subjectName', 'subjectName'), array('empty'=>'---Select Subject---')); ?>
<?php echo $form->error($model,'subjectId'); ?>
</td>
inside a for loop I iterate until I get 8 classId dropdown lists and subjectName dropdown lists. Once i give all the inputs i have a batch create function which updates all the 8 models to the database.
The problem is i can figure out a way to use AJAX with my dropdown lists. I have done it using two drop downs, with distinct names for each drop down.
In my controller i implemented this for the same scenario with two drop downs,
public function actionDynamicmake()
{
$class_id = $_POST['Classtimetable']['classId']; // IMPORTANT .. this is how you access previously entered data
$records1 = Subjects::model()->findAll('taughtinClass=:id',
array(
':id'=>$class_id ,
));
$data1 = CHtml::listData($records1,'subjectName','subjectName');
foreach($data1 as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
}
Can it be done without changing the logic in my view?
It is possible if i have several dropdowns with different names but then i’ll have to change my batch create function as well.
If there is a possibility please let me know…!
Many thanks!!
Cheerz!