Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving data from other tables in the view
    primarykey
    data
    text
    <p>I'm passing a set of objects to the view, including some foreign IDs to other tables in the database. Say objects of comments that have an attribute 'writer_id' which refers to the id of the user.</p> <p>How can I display user's data (name for example) from the view ?</p> <p>Here's the controller 'comments' :</p> <pre><code>function index() { $this-&gt;load-&gt;model('comment_model'); $data['comments'] = $this-&gt;comment_model-&gt;get_comments(); $this-&gt;load-&gt;view('comments',$data); } </code></pre> <p>and this is inside comment_model :</p> <pre><code>function get_comments() { $id = $this-&gt;uri-&gt;segment(3); // ignore this , it's given $q = $this-&gt;db-&gt;query("SELECT * FROM comments WHERE post_id=$id"); if ($q-&gt;num_rows() &gt; 0 ) { foreach ($q-&gt;result() as $row){ $data[] = $row; } return $data; } } </code></pre> <p>All what I can use in the view is the 'writer_id' and not able to display his name or any other data.</p> <p><strong>UPDATE</strong></p> <p>here's the view file</p> <pre><code> &lt;?php if (is_array($comments)){ ?&gt; &lt;?php foreach ($comments as $comment) : ?&gt; &lt;p&gt; &lt;?php echo $comment-&gt;text;?&gt; - written by &lt;?php echo $comment-&gt;writer_id ?&gt; &lt;/p&gt; &lt;?php endforeach ;?&gt; &lt;?php } ?&gt; </code></pre> <p>This will print:</p> <blockquote> <p>bla bla bla - written by 4</p> </blockquote> <p>where it should be </p> <blockquote> <p>bla bla bla - written by John (whose id is 4 as in user table)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload