2017-01-30 4 views
0

私は500の結果を持つテーブルを持っています。order_by&limitでCodeIgniterを10で5つのランダムな結果を選択しますか?

私はDESCオプションのプレーヤーで注文し、10個の結果を限る必要があり、CodeIgniterで10個の結果を5つ選択する必要があります。

私が持っているもの:

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; 
} 
+1

ようこそスタックオーバーフロー!あなたは[ツアーをすることができます](http://stackoverflow.com/tour) –

答えて

1

私が正しくあなたを理解していれば、あなたは降順で列の選手で注文したデータベース内の10件のトップの結果を、選択し、ランダムにこれらの5を選択するようにしたいですか?

だけにあなたのコードの1行を変更します。

$this->db2->limit(10); 

そして同様にメソッドを呼び出します。そして、また、あなたのコードは次のようになりますhttp://php.net/manual/en/function.mt-rand.php

0

を使用することを検討して

$my_random_five = array_rand($this->getServers(), 5); 

上記のマークで示唆されるように

  $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; 
      } 
関連する問題