Obtain data randomly from the database with CodeIgniter

0

It turns out that I need to get random data from the database with the CodeIgniter framework.

in database I have a table called phrases and within it two fields:

Type and phrase

In the field type I have 1 which has 5 sentences and the same with type 2 type 3 type 4 type 5

Then I want to do a select phrases ramdon where type = 1

and to show me random phrases of type 1. Looking for I found this code:

function Getramdon(){

    $this->db->order_by('tipo','RANDOM');
    $this->db->limit(1);
    $query = $this->db->get('frases');
    return $query->result_array();
  } 

but it shows me aleatorial phrases of other types and not only of the 1 as I asked.

    
asked by Hernan Humaña 23.12.2016 в 16:07
source

1 answer

3

Notice that you are passing as an argument the string RAMDOM and not RANDOM , which would be correct. Change it like this:

$this->db->order_by('tipo','RANDOM');

To obtain only those of type 1 you are missing the WHERE clause. Try adding this instruction:

$this->db->where('tipo', 1);
    
answered by 23.12.2016 в 16:22