Welcome Guest, Not a member yet? Register   Sign In
Random Quote
#1

[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?
#2

[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-gui...ecord.html

Cheers
#3

[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">
            <div class="wrapper">
                <div id="panel_ident"></div>
                <div id="panel_memo">
                    <h3 class="memo_&lt;?php echo $panelbase['Object_ID']; ?&gt;"><span>&lt;?php echo $panelbase['Object_Desc']; ?&gt;</span></h3>
                </div>
            </div>
        </div>


Code:
&lt;?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['title'] = 'Home';
        
        $this->load->model('Panel');
        $data['panelbase'] = $this->Panel->get_memo();
        
        $this->load->view('meta', $data);
        $this->load->view('header');
        $this->load->view('panel', $data);
        $this->load->view('index');
        $this->load->view('footer');
    }
}

Code:
&lt;?php

class Panel extends Model {

    function Panel()
    {
        parent::Model();
    }
    
    function get_memo()
    {
        $sql = "SELECT Object_ID, Object_Desc FROM panelbase";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    
}
#4

[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?
#5

[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.
#6

[eluser]BrianDHall[/eluser]
Try running this:

Code:
$query = $this->db->get('panelbase');
$result = $query->result_array();
var_dump($result);

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.
#7

[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-gui...elper.html

Quote:random_element()

Takes an array as input and returns a random element from it. Usage example:

Code:
$quotes = array(
            "I find that the harder I work, the more luck I seem to have. - Thomas Jefferson",
            "Don't stay in bed, unless you can make money in bed. - George Burns",
            "We didn't lose the game; we just ran out of time. - Vince Lombardi",
            "If everything seems under control, you're not going fast enough. - Mario Andretti",
            "Reality is merely an illusion, albeit a very persistent one. - Albert Einstein",
            "Chance favors the prepared mind - Louis Pasteur"
            );

echo random_element($quotes);

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.
#8

[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...




Theme © iAndrew 2016 - Forum software by © MyBB