[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><input type=\"radio\" name=\"poll\" value='".$list['id']."' /> ". $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.