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.


Messages In This Thread
Poll Query - by El Forum - 11-08-2009, 10:40 PM
Poll Query - by El Forum - 11-08-2009, 10:47 PM
Poll Query - by El Forum - 11-08-2009, 10:50 PM
Poll Query - by El Forum - 11-08-2009, 10:59 PM
Poll Query - by El Forum - 11-08-2009, 11:05 PM
Poll Query - by El Forum - 11-09-2009, 06:00 PM
Poll Query - by El Forum - 11-09-2009, 10:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB