![]() |
Random Quote - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Random Quote (/showthread.php?tid=22308) |
Random Quote - El Forum - 09-04-2009 [eluser]georgerobbo[/eluser] Hello, I'm starting to develop a random quote system. So as you could guess I will have all the quotes in a database and then randomly select a quote from the database. But, I do not know how to do this? Is there a MYSQL feature where you can randomly select a row based on your primary key or must you select all the fields and then choose a random result from the array using PHP? Random Quote - El Forum - 09-04-2009 [eluser]LuckyFella73[/eluser] You can set order by random when using the active record class. For example: Code: $this->db->order_by("id", "random"); Have a look at the user guide / active record page: http://ellislab.com/codeigniter/user-guide/database/active_record.html Cheers Random Quote - El Forum - 09-04-2009 [eluser]georgerobbo[/eluser] Okay. Thank you. But I'm doing something really stupid which I can't quite figure out. I can't seem to select this data from the database, I keep getting an error. Code: <div id="panel"> Code: <?php Code: <?php Random Quote - El Forum - 09-04-2009 [eluser]jedd[/eluser] [quote author="georgerobbo" date="1252101969"] Okay. Thank you. But I'm doing something really stupid which I can't quite figure out. I can't seem to select this data from the database, I keep getting an error. [/quote] Do you reckon the error might help other people work out what your problem is? Random Quote - El Forum - 09-04-2009 [eluser]georgerobbo[/eluser] Sorry, <h4>A PHP Error was encountered</h4> <p>Severity: Notice</p> <p>Message: Undefined index: Object_Desc</p> <p>Filename: views/panel.php</p> <p>Line Number: 5</p> It works when I use a foreach function on my view. Random Quote - El Forum - 09-04-2009 [eluser]BrianDHall[/eluser] Try running this: Code: $query = $this->db->get('panelbase'); It would seem to me that your problem is the database structure of 'panelbase'. Your query states that you want the info in the columns named 'Object_ID' and 'Object_Desc'. You might export the structure of that particular table and post that. Ensure you don't have any unnecessary keys defined on the table, that the naming and case are correct (if you don't have a case-insensitive structure of database), and that there is a column named 'Object_Desc'. The SQL itself is correct, so I think either there is a key problem in the DB or you are looking for info that columns that aren't defined. Random Quote - El Forum - 09-04-2009 [eluser]BrianDHall[/eluser] Oh, I just stumbled into something else you might find useful as far as doing the random quote thing: http://ellislab.com/codeigniter/user-guide/helpers/array_helper.html Quote:random_element() With this in ActiveRecord you'd use a call to $query->result_array() after fetching your quotes, then run random_element() on that resulting array. Random Quote - El Forum - 09-05-2009 [eluser]Hitesh Chavda[/eluser] To do this you must have id on each row of quote table you can first catch the quotes table's total row i.e. $total_row = $this->db->count_all('table_name') then, $random_row_no = random(1,$total_row); $this->db->select('quote_text'); $this->db->where('quote_id',$random_row_no); $q = $this->db->get('table_quote'); etc... |