Hi I am making a polling functionality. In the below code I am trying to get count of answers by storing the key of array $count as id of the answer and += to count how many times it was chosen as an answer.
The problem that I am having is that $count[$option_id] += 1; doesn't work. Not sure what I am doing wrong with the logic. In the database the records exist and I can confirm that query run fine and gets the result.
There are 2 answers possible per vote. For eg: lets take answer with Id 1 and 2. So, in the end, it should output something like
details: {1: 20, 2: 50}
This means that 20 people have voted for Answer 1 and 50 people for 2.
Any help will be great, Thanks
$answer_count = 0;
$count = array();
$query = "SELECT * FROM `votes` where ques_id = 55";
$stmt = $this->db_obj->query_exec($query);
while($info = mysqli_fetch_array($stmt)){
$answer_count++;
$option_id = $info['option_id'];
$count[$option_id] += 1;
}
return json_encode(array("success" => "1","total" => $answer_count,"details" => $count));