Welcome Guest, Not a member yet? Register   Sign In
Poll Query
#1

[eluser]amipaxs[/eluser]
hi,

I'm creating a dynamic poll using two tables

table POLL fields(id,question,date) OPTIONS fields(id,option,votes,id_poll)

now in my model
Code:
function displayPoll(){
   // get last poll question and the related options
   $data=array();
   $this->db->select('poll.*,options.*');
   $this->db->orderby('poll.id', 'DESC'); // it actually don't get the  last poll inserted
   $this->db->from('poll');
   $this->db->join('options', 'poll.id = options.id_poll');
   $Q=$this->db->get('');
  
   if($Q->num_rows() >0) {
       foreach($Q->result_array() as $row)
       $data[] = $row;
    }
      
   $Q->free_result();
   return $data;
   }
I guess the problem happens here, i get the poll question repeated as many times as the number of options in the table Options.

controller :

Code:
$data['poll'] = $this->MPoll->displayPoll();

view :
Code:
if(count($poll)){
   echo "<p class='title2'>" . $poll['question'] . "</p>\n";

   echo "<ul class='enc'>\n";
   foreach($poll as $key=> $list) {
      
   echo "<li>&lt;input type=\"radio\" name=\"poll\" value='".$list['id']."' /&gt; ". $list['option']. "</li>\n";
   }
   echo "<li>". form_error('poll'). "</li>\n";
   echo "</ul>\n";

  
   echo form_hidden('id', $poll['id_poll']);
   $data=array('name'=>'submit','id'=>'submit','value'=>'Vote');
  
   echo form_submit($data,'');
I get the following error

Severity: Notice
Message: Undefined index: question
Filename: views/poll_box.php
Line Number: 13

I can see the options values correctly but not the the values outside the foreach loop, how do i extract them from the result set.
#2

[eluser]wowdezign[/eluser]
I don't know if this is the problem but I noticed in your model you have:

Code:
$this->db->orderby('poll.id', 'DESC');

and I think it should be:

Code:
$this->db->order_by('poll.id', 'DESC');
#3

[eluser]wowdezign[/eluser]
Are you wanting to get the question, the possible answers, or both?
#4

[eluser]amipaxs[/eluser]
thanks i had not noticed that about orderby was working ok even when order_by is the correct..
i noticed another problem, I can't get the last poll question that i saved on the table, should i use date instead to get the last record.'?
#5

[eluser]wowdezign[/eluser]
This should get you one row from the database and return it.

Code:
function displayPoll(){
   $data=array();
   $this->db->order_by('poll.id', 'DESC');
   $this->db->join('options', 'poll.id = options.id_poll');
   $Q=$this->db->get('poll',1); // the 1 sets the limit to 1 record
  
   if($Q->num_rows() >0) {
       return $Q->row();
    }
}

If you want the possible answers, you could run another query using the id to get the answers.
#6

[eluser]amipaxs[/eluser]
considering i'd make two queries one for the questions and another for the options
how can i pass the poll id into the displayOptions function, i must extract the id from the array $data['poll'] , how ?

$data['poll'] = $this->MPoll->displayPoll(); //


$data['options'] = $this->MPoll->displayOptions(????);
#7

[eluser]wowdezign[/eluser]
Code:
considering i’d make two queries one for the questions and another for the options
how can i pass the poll id into the displayOptions function, i must extract the id from the array $data[‘poll’] , how ?

$data[‘poll’] = $this->MPoll->displayPoll(); //
  
$data[‘options’] = $this->MPoll->displayOptions(????);

How about:

Code:
$data[‘poll’] = $this->MPoll->displayPoll();
$data[‘options’] = $this->MPoll->displayOptions($data['poll']->id);

Once you have the object stored in a variable, you can access its properties. In this case, $data is an array, the array key 'poll' refers to an object that has an id property. So, $data['poll']->id should work.

You have to be aware of what you are returning from your model. If it is an array, you'll need to do:

Code:
$data[‘poll’] = $this->MPoll->displayPoll();
$data[‘options’] = $this->MPoll->displayOptions($data['poll']['id']);

instead.




Theme © iAndrew 2016 - Forum software by © MyBB