Welcome Guest, Not a member yet? Register   Sign In
count problem
#1

[eluser]alexaaaaaaaaaa[/eluser]
Hi all i've got a small problem i'm building a new poll sistem and i want to count how many times a choice has been voted
here's the schema of my database
poll_vote
id(auto increm) poll_id user_id choice_voted
1 2 81 100
2 2 88 101
3 2 23 100

as you can see the poll with the id 2 has 3 votes so far in witch choice_voted 100 has been voted twice .
How do i count how many votes?

so far i can only get how many votes with this function

controller
Code:
$cond = array('poll_vote.poll_id'=>$this->uri->segment(3));
        $data['poll'] = $this->friends_model->get_my_votes($cond);
        $data['poll_num_votes'] = $data['poll']->num_rows();

model
Code:
function get_my_votes($cond=array())
    {
        if(count($cond)>0)        
             $this->db->where($cond);
        
        $this->db->from('poll_vote');
        $this->db->join('a3m_account', 'poll_vote.user_id = a3m_account.id', 'left');
        $result =$this->db->get();
        return $result;
    
    }

view
Code:
Poll votes <p>&lt;?php echo $poll_num_votes; ?&gt;</p>
#2

[eluser]InsiteFX[/eluser]
Try this:
Code:
$data['poll_num_votes'] = count($data['poll']);

InsiteFX
#3

[eluser]alexaaaaaaaaaa[/eluser]
[quote author="InsiteFX" date="1301854781"]Try this:
Code:
$data['poll_num_votes'] = count($data['poll']);

InsiteFX[/quote]
it's not working ... it's showing 1 and it's not correct.
#4

[eluser]WanWizard[/eluser]
If you want to have a count per choice_voted, you need a query define a query that looks like
Code:
SELECT choice_voted, COUNT(*) as count GROUP_BY choice_voted WHERE ...
#5

[eluser]alexaaaaaaaaaa[/eluser]
[quote author="WanWizard" date="1301880064"]If you want to have a count per choice_voted, you need a query define a query that looks like
Code:
SELECT choice_voted, COUNT(*) as count GROUP_BY choice_voted WHERE ...
[/quote]

this is what i use and it doesn't work at all
Code:
$sql = "SELECT choice_voted, COUNT(*) as count GROUP_BY choice_voted WHERE poll_id = 105";

        $query = $this->db->query($sql);
        $row = $query->row();

        if ($row->count == 0)
        {
            echo "You must submit the word that appears in the image";
        }else { echo $row->count;}
#6

[eluser]alexaaaaaaaaaa[/eluser]
done i've managed to get it working Smile

here's the code
model
function get_v()
{
///SELECT choice_voted, COUNT(*) as count GROUP_BY choice_voted WHERE
$this->db->select('choice_voted, COUNT(*) AS count');
$this->db->group_by("choice_voted");
$this->db->where('poll_id = 105');
$this->db->from('poll_vote');
$result =$this->db->get();
return $result;
}

controller

$data['poll_v'] = $this->friends_model->get_v();
$data['poll_vs'] = $data['poll_v']->result();

view
<pre>&lt;?php print_r($poll_vs); ?&gt;<pre>




Theme © iAndrew 2016 - Forum software by © MyBB