• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New in codeigniter! poll problem

#1
[eluser]benfike[/eluser]
Hi!
I now work on a poll system.
I make 2 sql table for this:

#vote_answers
-vote_id
-answer_id
-answer
#vote_question
-vote_id
-question
-status('active','inactive','finished')

For Get answers I make an function with this name :
Code:
function getAnswers(){
        $data = array();
        $this->db->where('vote_id', 'vote_question.vote_id');
        $Q = $this->db->get('vote_answers');
        if ($Q-> num_rows() > 0){
            foreach ($Q-> result_array() as $row){
            $data['answer'] = $row['answer'];
        }
        }
        $Q-> free_result();
        return $data;
    }

And only want to list that answer where the vote_answers.vote_id = vote_question.vote_id
How can i do this?

My other problem:
if i make some answers for a Question it list only 1!
Code:
<?php foreach($answers as $answer):?>
            <li>
                &lt;input id="answer_&lt;?php echo $answer['answer_id'] ?&gt;" name="poll_answer_id" type="radio" value="&lt;?php echo $answer['answer_id'] ?&gt;" /&gt;
                <label for="answer_&lt;?php echo $answer['answer_id'] ?&gt;">
                    <strong>&lt;?php echo ($answer['answer']) ?&gt;</strong>
                    (&lt;?php echo $answer['votes'] ?&gt;)
                </label>
            </li>
            &lt;?php
            endforeach;
            ?&gt;

#2
[eluser]Dark Preacher[/eluser]
There is an error in your function:
Code:
$data['answer'] = $row['answer'];
should be:
Code:
$data[] = array('answer' => $row['answer']);
Or smth like that, because you overwrite your old value 'answer' in array with a new one, while you need to add it to array.

#3
[eluser]benfike[/eluser]
Its work, but my first problem still here :S

#4
[eluser]larsjeh[/eluser]
[quote author="benfike" date="1251070481"]Its work, but my first problem still here :S[/quote]
I think that you need a MySQL JOIN for this problem.

I think this should help, for preventing that your array is printed on the screen, comment or remove print_r($data).

Code:
function getAnswers(){
        $data = array();
    $this->db->select('*');
    $this->db->from('vote_answers')
    $this->db->join('vote_question', 'vote_question.vote_id = vote_answers.vote_id');
          $Q = $this->db->get('vote_answers');
        if ($Q-> num_rows() > 0){
            foreach ($Q-> result_array() as $row){
            $data['answer'] = $row['answer'];
        }
        }
        $Q-> free_result();
        return $data;
        //DEBUGGING MKKAAYY :)
        print_r($data);


    }

#5
[eluser]benfike[/eluser]
Error Number: 1066

Not unique table/alias: 'votes_answers'

SELECT * FROM (`votes_answers`, `votes_answers`) JOIN `votes` ON `votes`.`id` = `votes_answers`.`pid`

update: when I delete that: $this->db->from('votes_answers'); the page work, but again list all answers, not only where vote_id = other vote_id

#6
[eluser]larsjeh[/eluser]
[quote author="benfike" date="1251076276"]Error Number: 1066

Not unique table/alias: 'votes_answers'

SELECT * FROM (`votes_answers`, `votes_answers`) JOIN `votes` ON `votes`.`id` = `votes_answers`.`pid`

update: when I delete that: $this->db->from('votes_answers'); the page work, but again list all answers, not only where vote_id = other vote_id[/quote]

Sry m8 made a mistake with $this->db->get Wink, i think this should work.
Code:
function getAnswers(){
        $data = array();
    $this->db->select('*');
    $this->db->from('vote_answers')
    $this->db->join('vote_question', 'vote_question.vote_id = vote_answers.vote_id');
    $Q = $this->db->get();
        if ($Q-> num_rows() > 0){
            foreach ($Q-> result_array() as $row){
            $data['answer'] = $row['answer'];
        }
        }
        $Q-> free_result();
        return $data;
        //DEBUGGING MKKAAYY :)
        print_r($data);
    }

#7
[eluser]benfike[/eluser]
Dont work, I dont know now what is the problem..:S

Now I add this but still dont work :/
Code:
function getAnswers(){
        $data = array();
        $this->db->select('*');
        $this->db->from('votes_answers');
        $this->db->join('votes', 'votes.id = votes_answers.pid');
        $Q = $this->db->get();
        if ($Q-> num_rows() > 0){
            foreach ($Q-> result_array() as $row){
        $data[] = array('answer' => $row['answer'], 'id' => $row['id'], 'votes' => $row['votes'], 'pid' => $row['pid']);
        }
        }
        $Q-> free_result();
        return $data;
    }

Some table name new, because i changed something.

#votes
-id
-name
-status
#votes_answers
-id
-pid (pollID)
-answer
-votes (for results)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.