1

I'm trying to query the db with the following query in the model:

function showSpecific(){

    $all = $this->session->all_userdata();
    $name = $all['u_name'];

    $id = $this->db
    ->select('u_id')
    ->from('users')
    ->where('u_name', $name)
    ->get()
    ->row(); 

    $id = $id->u_id; 

    $data = $this->db->query("SELECT messages.m_id, messages.post_id, messages.m_betreff, messages.an_user, messages.von_user, messages.m_time, users.u_id, users.u_name, posts.p_titel, users.u_id, messages.m_content
                FROM messages
                INNER JOIN users 
                ON messages.von_user=users.u_id
                INNER JOIN posts
                ON messages.post_id=posts.post_id
                WHERE messages.an_user='$id'
                ORDER BY messages.m_time DESC");




    return $data;
}

But when I try to output the result in the view:

<?php foreach ($query->result() as $row)
                {
                    echo '<tr><td>';
                    echo anchor('users/showUser/'.$row->u_id, $row->u_name);
                    echo '</td><td>';
                    echo $row->m_betreff;
                    echo '</td><td>';
                    echo $row->m_content;
                    echo '</td><td>';
                    echo anchor('posts/showSpecific/'.$row->post_id, $row->p_titel);
                    echo '</td><td>';
                    echo $row->m_time;
                    echo '</td></tr>';
                }
                ?>

Only the m_content gets this error: Why is it the only one? And how do I solve this?

A PHP Error was encountered
Severity: Notice

Message: Undefined property: stdClass::$m_content

Filename: views/specific_message.php

Line Number: 48

Here is my Controller:

function showSpecific(){

    $this->load->model('messages_model');
    $data['query'] = $this->messages_model->getMessages();

    $data['main_content'] = 'specific_message';
    $this->load->view('includes/login_template', $data);
}

Thanks a lot!

4
  • 1
    try var_dump($query->result()) and see what is problem with m_content. Another suggestion is dont use mysql queries inside view files, use them only for templating Commented Mar 20, 2012 at 18:37
  • do you get other values printed ? Commented Mar 20, 2012 at 19:14
  • Yes all the other values get printed. The query is inside of a model, sorry i forgot to copy that line of the code as well. Thanks for the suggestion, I'll try and answer again then. Thanks! Commented Mar 20, 2012 at 19:18
  • Even if $data['query'] ? Commented Mar 20, 2012 at 19:20

1 Answer 1

6

I think

$data['query'] = $this->db->query("...");

should be

$query=$this->db->query("...");

so you can

foreach ($query->result() as $row) {...}

reference: codeigniter

After question Update : You have $query variable inside your view because of $data['query'] ($data will be extracted), $query is your object now (inside your view) so you can loop the $query.

Wrong model name getMessages() has been called, it should be showSpecific()

Sign up to request clarification or add additional context in comments.

8 Comments

I changed it and all corresponding places but now I get the following error: Fatal error: Cannot use object of type CI_DB_mysql_result as array in /htdocs/findjou/application/controllers/messages.php on line 19
Are you using array notation anywhere ? i.e $row['u_id'] ? because it's returning an object so use it like $row->u_id;
Yes I do(did). I updatet my original post with my original controller, right now in my editor it is changed as you mentioned. (After I changed it the new error came.)
You are calling getMessages(); function but in your example you've showSpecific(), do you have getMessages() function in your model, also your controller name is showSpecific()?
Ohh man, that was it! I called the wrong model function. By the way, is it bad practice to name the controller and the model function the same? Thanks you so much! I tried to vote your answer as useful but it tells me I need 15 reputation first. How can I else vote for you to say thanks?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.