Welcome Guest, Not a member yet? Register   Sign In
How to create query function by this situation?
#1

[eluser]Pachara Chutisawaeng[/eluser]
I am going to create evaluation form.
I got 3 tables which is Score, Question and Evaluator.
I want to keep score and question in different table and Evaluator information as well.

1.Score
s_no s_score
1 0
2 0
3 0

2.Question
q_no q_detail
1 bla bla bla
2 bla bla bla
3 bla bla bla

3.Evaluator
e_id e_name e_date
1 Jack 03-05-2009

Now let see my HTML form code...

The maximum score for each choice is 5.
I simply use the radio button like

Code:
<input type="radio" name="1" value="1">
<input type="radio" name="1" value="2">
<input type="radio" name="1" value="3">
<input type="radio" name="1" value="4">
<input type="radio" name="1" value="5">
and so on.......

Suppost that my $_POST variable store this kind of data
Code:
Array("e_name"=>"Jack", "e_date"=>"03-05-2009","1"=>5,"2"=>4,"3"=>3,)

What i am going to do is to store my $_POST variable on to my database.
The score table must be updated with the average value always.

Now I have 2 Question :-)
1.How do i write the query function base on codeigniter framework?
2.Instead of updated the value in score table, What if i add one more table which is
"Question Log" which keep all the q_no, q_score and e_id then sum it later on, Which one is better?

PS. I can do this kind of thing easily with out using the framework, now i try to optimize my coding skill so PLEASE HELP I AM DUMP
#2

[eluser]Pachara Chutisawaeng[/eluser]
What i have done isusing this code
Code:
for($i=1;$i<=3;$i++){
  $score = array(“s_score”=>$_POST[$i]);
  $this->db->where(‘s_no’,$i);
  $this->db->update(“score”,$score);
}
But i think it's not a good way - -
#3

[eluser]Dam1an[/eluser]
:grrr: Why are you not carrying this on in this thread that you started earlier, and we asked for more information!

As for which approach is better, they both have pros and cons and it depends on how it will be used

If you update the average each time, you need to do a read (get) and a write (update), but fetching it later is just a read

If you just add another row each time, you only do the write, but when you get the average, you need to do AVERAGE
This woud be very quick on small/medium data sets, but could get a bit slow if you have huge data sets

So it depends if it will mostly be adding new scores or just getting the average

Also, you might lose precision if you do an average each time...
#4

[eluser]Pachara Chutisawaeng[/eluser]
Thank you for your reply but how to qrite the query function??
I am new to Codeigniter I just need a guide line
#5

[eluser]Dam1an[/eluser]
The Active Record part of the user guide should tell you pretty much everything you need to know

Tbh, you're not a million miles of with what you have already
And if as you said earlier you could do this easily with plain PHP/MySQL, then you can still use that with CI
#6

[eluser]Pachara Chutisawaeng[/eluser]
OK Thank you Dam1an.
Let me ask you a simple question
Code:
/*
    class Myclass {
        var $title = 'My Title';
        var $content = 'My Content';
        var $date = 'My Date';
    }
*/

$object = new Myclass;

$this->db->where('id', $id);
$this->db->update('mytable', $object);

If $id is an array, have multiple value what i should do?
Are there any function with out using loop?
#7

[eluser]Dam1an[/eluser]
There's a thread over here about doing multile inserts in a single query (works with MySQL)... might be a starting point to get it working with updates

If you was always inserting the same object, you could probably user where_in (see user guide for more info)
#8

[eluser]Pachara Chutisawaeng[/eluser]
WOW, That what i need
Thank a lot
^^




Theme © iAndrew 2016 - Forum software by © MyBB