Record casuale dal database mysql con CodeIgniter
-
06-07-2019 - |
Domanda
Ho fatto ricerche su Internet, ma non sono riuscito a trovare nulla ...
Ho un db mysql e record a una tabella e ho bisogno di ottenere record casuali da questa tabella a ogni caricamento della pagina. Come lo posso fare? C'è qualche funzione per quello?
Apprezzare! grazie
DIFFERENZIATA: link: http: //www.derekallard. com / blog / Post / Ordine-Database-risultati-by-random-in-codeigniter /
$this->db->select('name');
$query = $this->db->get('table');
$shuffled_query = $query->result_array();
shuffle ($shuffled_query);
foreach ($shuffled_query as $row) {
echo $row['name'] . '<br />';
}
Soluzione
Codeigniter offre la possibilità di ordinare i risultati per "RANDOM" quando si esegue una query. Ad esempio
function get_random_page()
{
$this->db->order_by('id', 'RANDOM');
or
$this->db->order_by('rand()');
$this->db->limit(1);
$query = $this->db->get('pages');
return $query->result_array();
}
L'ho già usato prima e l'ho trovato funzionante. Spero che aiuti
Altri suggerimenti
Non conosco codeigniter, ma ottenere un set di dati casuale è
SELECT * FROM table ORDER BY RAND() LIMIT 1
La parte pertinente è " ORDER BY RAND ()
" ;, ovviamente.
Sai quanti record ci sono nella tabella? Potresti fare qualcosa del genere:
$count=mysql_exec('select count(*)-1 from some_table');
$count=rand(1,$count);
quindi:
select * from
some_Table
limit $count,1
Questo frammento di codice ha funzionato bene per me.
$this->db->select('name');
$this->db->order_by('rand()');
$this->db->limit(1);
$query = $this->db->get('<table>'); //<table> is the db table name
return $query->result_array();
Ottenere record casuali da una grande tabella è molto costoso.
Non utilizzare ORDER BY RAND ()
.
Questa è una cattiva idea, ma se hai un tavolino non c'è problema. In un enorme database questo tipo di query è molto lento.
Uso codeigniter con datamapper. Questo è il codice che utilizzo per ottenere un record in modo casuale dalla tabella Advertiser
:
$ad = new Advertiser();
$ad->limit(3);
$ad->order_by('id', 'RANDOM');
$ad->get();
SELECT product_id, title, description
FROM products
WHERE active = 1
AND stock > 0
ORDER BY RAND()
LIMIT 4
La clausola ORDER BY RAND () restituisce record casuali! Puoi limitare i record anche usando LIMIT.
Pensiamo che abbiamo una tabella in cui abbiamo eliminato alcune righe. È possibile che l'ID non continui correttamente. Per ID campione: 1,5,24,28,29,30,31,32,33 (9 righe)
mysql_num_rows restituisce 9
Un altro metodo restituirà righe non esistenti: $ Count = 9; // perché mysql_num_rows () == 9 $ Count = rand (1, $ count); // restituisce 4 per il campione, ma non abbiamo remato con id = 4
Ma con il mio metodo ottieni sempre righe esistenti. Puoi separare il codice e utilizzare il primo codice 2 ovunque sul sito.
// Inside of Controller Class
function _getReal($id,$name_of_table)
{
$Q=$this->db->where('id',$id)->get($name_of_table);
if($Q->num_rows()>0){return $Q;}else{return FALSE;}
}
function _getLastRecord($name_of_table)
{
$Q=$this->db->select("id")->order_by('id DESC')->limit("1")->get($name_of_table)->row_array();
return $Q['id'];
}
function getrandom()
{
$name_of_table="news";
$id=rand(1,$this->_getLastRecord($name_of_table));
if($this->_getReal($id,$name_of_table)!==FALSE)
{
echo $id;
// Here goes your code
}
else
{
$this->getrandom();
}
// END
Ottenere record casuali da una grande tabella è molto costoso. Ma sotto questo codice è molto efficace ..
$count=mysql_num_rows(mysql_query("select * from table_name WHERE SOME_OF_YOUR_CONDITION"));
$nums=rand(1,$count);
mysql_query(" select * from table_name WHERE SOME_OF_YOUR_CONDITION LIMIT $count,1");
Questo sarà utile ...
Penso che questo non sia il modo migliore. Ad esempio, hai eliminato il record che è now == $ count
. È necessario ripetere questa procedura per mysql_num_rows()
Questa funzione recupera tutte le righe della tabella in ordine casuale
public function get_questions(){
$this->db->select('*');
$this->db->order_by('rand()');
$this->db->from('multiple_choices');
$query = $this->db->get();
return $query->result_array();
}