Hello,
I need counseling to help me complete a function codeigniter.
Let me explain, with this sql function, I get a ranking of "total_stats".
Code:
SELECT a1.from_user_id, a1.total_stats, COUNT(a2.total_stats) total_stats_Rank
FROM score a1, score a2
WHERE a1.total_stats <= a2.total_stats OR (a1.total_stats = a2.total_stats AND a1.from_user_id = a2.from_user_id)
GROUP BY a1.from_user_id, a1.total_stats
ORDER BY a1.total_stats DESC, a1.from_user_id DESC
Result
With this sql function, I get the ranking "total_stats" with "from_user_id"
Code:
SELECT a1.from_user_id, a1.total_stats, COUNT(a2.total_stats) position
FROM score a1, score a2
WHERE a1.total_stats <= a2.total_stats AND a1.from_user_id=25030
GROUP BY a1.from_user_id, a1.total_stats
ORDER BY a1.total_stats DESC, a1.from_user_id DESC
Result :
I use this function to calculate the position of a user based on the total of "total_stats". If "from_user_id" exists then update position. If "from_ser_id" does not exist create it. Then display the result in my view.
PHP Code:
public function position($posit_score) // Position user
{
$this->db->select('a1.from_user_id, a1.total_stats, COUNT(a2.total_stats) position');
$this->db->from('score');
$this->db->where('a1.total_stats <= a2.total_stats AND a1.from_user_id=?');
$this->db->group_by('a1.from_user_id, a1.total_stats');
$this->db->order_by('a1.total_stats DESC, a1.from_user_id DESC');
$result = $this->db->get()->row_array();
print_r($result);
$total_posit = $result['position'];
if ($this->ttl_position($user_id) > 0) // Check si exists dans 'score'
{
$data = array(
'position' => $total_posit
);
$this->db->where('from_user_id', $user_id);
$this->db->update('score', $data);
}
else
{
$data = array(
'from_user_id' => $user_id,
'position' => $total_posit
);
$this->db->insert('score', $data);
}
return $total_posit;
}
public function ttl_position($user_id)
{
$this->db->where('from_user_id', $user_id);
$this->db->from('score');
return $this->db->count_all_results();
}
But this returns me an error message :
Error Number: 1054
Unknown column 'a1.from_user_id' in 'field list'
Someone would have an idea of where I was wrong ?
Thank you in advance for your precious help
Violette