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

[eluser]lenwood[/eluser]
Hi All,
I'm trying to display a query result and I keep getting an error.

Model:
Code:
function load()
{
$this->db->select("*, date_format(date_time, '%W, %M %e, %Y') as date", FALSE);
$this->db->order_by('date_time', 'desc');
$query = $this->db->get('entries');

    if($query->num_rows() > 0) {
        foreach ($query->result() as $row) {
            $this->db->where('entry_id', $row->id);
            $this->db->join('categories', 'categories.id = post_cat.cat_id');
            $row->name = $this->db->get('post_cat');
            $data[] = $row;
        }
    }
    return $query->result();
}

View:
Code:
1: <?php foreach($query as $row): ?>
2: <h2>&lt;?php echo anchor('blog/post/'.$row->post_slug, "$row->post_title");?&gt;</h2>
3: <p>Posted in: &lt;?php echo $row->name; ?&gt;</p>
4: <p>&lt;?php echo $row->date; ?&gt;</p>
5: <div>&lt;?php echo $row->body; ?&gt;</div>
6: &lt;?php endforeach; ?&gt;

This code produces the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: views/blog_view.php
Line Number: 3

The result should be an array, as there are more than one category per post.

Thanks in advance,
Chris
#2

[eluser]Aken[/eluser]
On line one of your view, what is $query ?
#3

[eluser]lenwood[/eluser]
That's the assigned variable name. I should have included my controller.

Controller:
Code:
function index()
{
    $this->load->model('blog_model');
    $body['query'] = $this->blog_model->load();
    $this->load->view('includes/head');
    $this->load->view('blog_view', $body);
    $this->load->view('includes/foot');
}
#4

[eluser]Aken[/eluser]
Do the other $row variables work properly?

What happens if you var_dump($row) ? Basically you need to see why it's still declared as an object, and figure out where to fix that.
#5

[eluser]InsiteFX[/eluser]
Here is an example to get all categories in a table named categories.

Code:
function get_all_categories()
{
     $data = array();
     $query = $this->db->get('categories');

     if ($query->num_rows() > 0)
     {
       foreach ($query->result_array() as $row)
       {
         $data[] = $row;
       }
    }

    $query->free_result();
    return $data;
}

Return $data

InsiteFX
#6

[eluser]lenwood[/eluser]
[quote author="Aken" date="1273375068"]Do the other $row variables work properly?[/quote]

Yes, all of the other variables work just as they're supposed to.

[quote author="Aken" date="1273375068"]What happens if you var_dump($row) ?[/quote]

This was helpful, but I'm still lost. It looks to me like its not reading the table, because the only result I get is "could not be converted to string". Here's what I've done:

1. Verified that it's using the correct id. I echoed $row->id at a couple of different points in my code and it's showing the correct values.

2. Hard coded the entry_id to search for, instead of using the variable $row->id. I still got the "could not be converted to string".

[quote author="InsiteFX" date="1273376727"]Here is an example to get all categories in a table named categories.[/quote]

Your code will work to get all categories. My goal is to list only the categories associate with a particular blog entry.
#7

[eluser]InsiteFX[/eluser]
For listing categories for a particular blog you would need tagging.

Blog test

file under cat1 cat2 cat3 etc.

I think I have some code for it I'll check and get back to you.

InsiteFX
#8

[eluser]lenwood[/eluser]
Thanks InsiteFX, that would be great.




Theme © iAndrew 2016 - Forum software by © MyBB