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!

Linus
  • 4,643
  • 8
  • 49
  • 74
  • 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 – safarov Mar 20 '12 at 18:37
  • do you get other values printed ? – The Alpha Mar 20 '12 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! – Linus Mar 20 '12 at 19:18
  • Even if $data['query'] ? – The Alpha Mar 20 '12 at 19:20

1 Answers1

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()

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 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 – Linus Mar 20 '12 at 19:25
  • Are you using array notation anywhere ? i.e $row['u_id'] ? because it's returning an object so use it like $row->u_id; – The Alpha Mar 20 '12 at 19:30
  • 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.) – Linus Mar 20 '12 at 19:37
  • 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()? – The Alpha Mar 20 '12 at 19:40
  • 1
    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? – Linus Mar 20 '12 at 19:51
  • I think it is confusing so why should you do that! – The Alpha Mar 20 '12 at 19:53
  • Thanks ! I can understand your limitations that you can't vote me up but it's ok. :-) – The Alpha Mar 20 '12 at 20:00
  • You're right, I will give them other names in the future :-) Ok :-) I'll vote for you when I have reached the level! :-) – Linus Mar 20 '12 at 20:06