I given foreign keys to preferences table to questionid from questions table id and user_id from user table id. Now i'm inserting answers values into preferences. Can i know how to fetch questionid and user_id into preferences table. My questions are in _form.php file.
Thanks for reply. In my table having questions. Can i know how fetch questionid from database to _form.php file. And when i try for user id as you said the value is displaying on my page but its not stored in my database.
Can i know how to insert displaying value into database, Actually my user id is displaying my page, now how to insert this display value into database.
Hi, i’ve one more dought. Every time the questionid data is stored in new row. I mean now when i select option in dropdownlist it saving to my database like this
id:1
questionid:1
answers:haii
userid:1
Again if i select questionid is 1 with different answer it saving new row in database, but i need to store in the previous answer place my new answer in my table.
<?php echo $form->dropDownList($model, 'answers', array( 'select'=>'select',
'Not Really'=>'Not Really',
'A little bit'=>'A little bit',
'Light but regular'=>'Light but regular',
'Heavy but not regular'=>'Heavy but not regular',
'Regular, hardcore exercise'=>'Regular, hardcore exercise'
),
array('onchange'=>'this.form.submit()')
); ?>
<?php echo $form->dropDownList($model,'questionid', CHtml::listData(Questions::model()->findAll("id=2"), 'id', 'answers'), array('style'=>'visibility:hidden')) ?>
Hum I’m not sure I understand the question, do you mean when the user updates the answer to a particular question, you want to store the previous answer and not just update the answers field?
In that case, you could edit the actionUpdate to instead of just updating the record creating a new one.
You could create a new attribute in the model named
active
for example which would be a boolean.
When the user updates a determined record, you could set this boolean to
$model->active = FALSE
, save the record and then create a new record with the very same information as the previous one, but you would set the
$model->active = TRUE
.
This is a simple way to maintain the user’s answers history. Then to fetch the user’s answers, you could do something like:
$answers = Preferences::model->findAll("user_id=Yii::app()->user->id AND question_id=:question_id AND active = TRUE", array("question_id" => <question id here>));
You mean when the user tries to update the answer the first time its OK but when he updates again the previous answer is what he chose the first time instead of the second time?
If that is the case then it is because in your actionUpdate() you are not loading the model right.
Are you doing this like I sugested?
$answers = Preferences::model->findAll("user_id=Yii::app()->user->id AND question_id=:question_id AND active = TRUE", array("question_id" => <question id here>));
What do you mean "i need to save instead of "hiii" to "bye" when i select same questionid" ?
yes, i need to store second answer in the place of first answer. and where i need to write this code
$answers = Preferences::model->findAll("user_id=Yii::app()->user->id AND question_id=:question_id AND active = TRUE", array("question_id" => <question id here>));
You should take a look at the blog application that comes inside the yii framework folder in demos\blog, it really helps you out to understand those concepts.
Hi, here user_id is foreignkey from usertable and questionid is foreignkey from questions table. Now i’m inserting 4 questions values in preferences table as like this…
Above the values are storing in my db. But, when i select answers to questionid:1 first it saved like "hello" and again if I select different answer like "verygoog" its saving new row, but i need to store this new answer in the place of pld answer.no need to store in new row.
HI, Now am inserting my all question answers in db. But when i select different answer to each question in dropdown need to update this new answers in the place of previous answers. But here is nw answer is adding instead of previous one, but its delete previous and its inserting. I need only updation. Here is my controller code…