Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Userid, voteid, vote if he already logged and if he have'nt vote before..
#1

[eluser]benfike[/eluser]
Hi!

How can I check if a user already voted for the active poll? And if he already voted, he only see the results, but if he have'nt voted before for aktualle poll he can vote.

I have a votes_votes table for control this with the following fields:
id
voteid ( Poll ID )
ansid ( Answer ID )
userid ( USER ID )

So when active a poll with voteid=1 and logged userID=2 go and search in the database. And if already have voteid=1 and userID=2 he only can view only results, but if we have'nt voteid=1 and userID=2 he can vote.

Help me please
#2

[eluser]Ben Edmunds[/eluser]
Just run a SQL query for the voteid and userid with count_all_results and return a true or false.

So something like:

Model:
Code:
$count = $this->db->select('id')
               ->where("voteid", $voteid)
               ->where("userid", $user_id)
               ->count_all_results("tableName");

if ($count > 0) {
   return true;
}
else {
   return false;
}

If there is something then they already voted and set a flag like

Controller:
Code:
$this->data['already_voted'] = $this->vote_model->check_voted($user_id, $vote_id);

Then in your view you can check $already_voted

or if your using separate views just do something like:

Code:
if ($this->vote_model->check_voted($user_id, $vote_id)) {
   $this->load->view('polls/view');
}
else {
   $this->load->view('polls/vote');
}


Hope that helps.
#3

[eluser]benfike[/eluser]
Its ok! Thank you!

But how can I get the voteid of last(date desc) poll from db for $voteid?

I try with this: $vote_id = $this->Mpoll->getPolls($poll['voteid']);

[code]function getPolls(){
$data = array();
$this->db->where('status','active');
$this->db->orderby('date','desc');
$this->db->limit(1);
$Q = $this->db->get('votes_question');
if($Q->num_rows() > 0){
$row = $Q->row_array();
$data['voteid'] = $row['voteid'];
$data['question'] = $row['question'];
$data['status'] = $row['status'];
$data['date'] = $row['date'];
}
$Q->free_result();
return $data;
}[code]

But i get this:

A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT COUNT(*) AS `numrows` FROM (`votes_votes`) WHERE `voteid` = Array AND `userid` = '2'
#4

[eluser]Ben Edmunds[/eluser]
OK, you are passing voteid to the getPolls but it is not taken as an argument in the getPolls function.

So you need getPolls($id).

Then do a where on the voteid.

Also you might need to free_result before doing all this as it looks like you have something hanging around with the voteid and userid.
#5

[eluser]benfike[/eluser]
Its now works fine! Big Grin

Code:
$poll = $this->Mpoll->getPolls();
            $user_id = $this->session->userdata('user_id');
            $vote_id = $poll['voteid'];

thanks mate




Theme © iAndrew 2016 - Forum software by © MyBB