Welcome Guest, Not a member yet? Register   Sign In
Having problems with the table helper
#1

[eluser]sundancekid[/eluser]
Hi, I'm new to CI and just playing around with it trying to figure it out. Anyhow, I tried an example given in the user guide for generating a table from a database query, but it doesn't work for me.

I am trying this:

Code:
$this->load->library('table');

$query = $this->db->query("SELECT * FROM my_table");

echo $this->table->generate($query);

But I get this error:
Code:
A PHP Error was encountered

Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: libraries/Table.php

Line Number: 270

I know the db settings are correct I can loop through the query and display the results. The table I'm querying has about 5 fields and 3 rows.

Thanks for any help and I lookforward to learning more about CI and getting to know the community here.
#2

[eluser]imzyos[/eluser]
have you load the db class ?
#3

[eluser]sundancekid[/eluser]
I think so.

Model:

Code:
class Examplemodel extends Model {

    function Exampleemodel()
    {
        //Call the Model constructor
        parent::Model();
    }

    function get_trade_results()
    {
        // Produces: SELECT * FROM mytable
        $query = $this->db->query("SELECT * FROM example");
        return $query->result();
    }

Controler:

Code:
function examplesearch()
        {
            $this->load->model('Examplemodel', '', TRUE);
            $query = $this->Examplemodel->get_trade_results();
            $this->load->library('table');
                echo $this->table->generate($query);

          }
#4

[eluser]Derek Allard[/eluser]
I believe he means, do you have

Code:
$this->load->database();
#5

[eluser]Chris Newton[/eluser]
The error you describe is what you'd get if you were using echo $this->table->generate($query->result()); rather than echo $this->table->generate($query); OR what you could get if you forget to load (or autoload) $this->load->database();
#6

[eluser]briguy[/eluser]
As mahuti said, from your Model, do "return $query;" not "return $query->result()"
#7

[eluser]sundancekid[/eluser]
[quote author="briguy" date="1198475884"]As mahuti said, from your Model, do "return $query;" not "return $query->result()"[/quote]


Ok, it works now. Thanks!

I am a little confused as to when you need to use

$this->load->database(); and when you don't because I didn't and it's working.
#8

[eluser]Chris Newton[/eluser]
I think it may be autoloaded in the config by default.
#9

[eluser]briguy[/eluser]
sundance,

Libraries have to be loaded before use, unless they are listed in your ../config/autoload.php in which case they are autoloaded and available throughout your application without need to explicitly load.

Look for this line in your autoload.php.
$autoload['libraries'] = array('database','session');
I beleive the sandbox download from the ci site has database already in the library autoload array.

Brian
#10

[eluser]imzyos[/eluser]
yeah, buy database is not a light library, autoload its a nice feature but give it a little help




Theme © iAndrew 2016 - Forum software by © MyBB