[英]Select 5 random results in 10 with order_by & limit CodeIgniter?
I have a table with 500 results. 我有一张有500个结果的表。
I need to order by Players with DESC option, then limit for 10 results and select 5 results aleatory of the 10 with CodeIgniter. 我需要按带DESC选项的Players进行订购,然后限制10个结果,并选择10个具有CodeIgniter的结果。
What I have: 我有的:
public function getServers(){
$this->db2->from('server');
$this->db2->order_by("players", "desc");
$this->db2->limit(5);
$query = $this->db2->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
If I understand you correctly, you want to select the 10 top results in your database, ordered by column players in descending order, and then select 5 of these in random? 如果我对您的理解正确,您想选择数据库中排名前10位的结果,按列播放器的降序排列,然后随机选择其中5个?
Just change one line of your code to: 只需将代码的一行更改为:
$this->db2->limit(10);
And then call the method like: 然后调用如下方法:
$my_random_five = array_rand($this->getServers(), 5);
Then also consider using http://php.net/manual/en/function.mt-rand.php 然后还要考虑使用http://php.net/manual/en/function.mt-rand.php
Your code should look something like this as suggested by mark above 您的代码应类似于上面标记所建议的内容
$my_random_five = array_rand($this->getServers(), 5);
public function getServers(){
$this->db2->from('server');
$this->db2->order_by("players", "desc");
$this->db2->limit(10);
$query = $this->db2->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.