Note that there are some explanatory texts on larger screens.

plurals
  1. PORun query within foreach loop in Codeigniter
    primarykey
    data
    text
    <p>Basically i'm trying to build a comment system. The user looks at a photo on the site, and then can view all the comments made by other members. </p> <p>Each comment will be looped out using a foreach (currently working fine), but what I need to do is then run a seperate query for each comment to get the user details of the user who posted it. These details are stored on a seperate database (otherwise i'd just do a join). </p> <p>My model has this in it so far:</p> <pre><code>public function get_comment($id) { $db_photos = $this-&gt;load-&gt;database('photos', TRUE); $db_photos-&gt;select('id, comment, userid, photoid'); $db_photos-&gt;from('comments'); $db_photos-&gt;where('photoid', $id); return $db_photos-&gt;get()-&gt;result(); } </code></pre> <p>And here's the controller:</p> <pre><code>public function view($id) { $data['comment'] = $this-&gt;viewphoto_model-&gt;get_comment($id); if (empty($data['comment'])) { show_404(); } $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('viewphoto/viewphoto', $data); $this-&gt;load-&gt;view('templates/footer', $data); } </code></pre> <p>And then the view:</p> <pre><code>&lt;?php foreach ($comment as $comments): ?&gt; &lt;div class="ViewPhoto-CommentsBox"&gt; &lt;? echo $comments-&gt;comment; ?&gt; &lt;/div&gt; &lt;?php endforeach ?&gt; </code></pre> <p>So basically I need to grab the 'userid' value from each comment and then run a query on the 'users' database to get the user details for each comment posted. </p> <p>Any help is most appreciated :)</p> <p>EDIT:</p> <p>Still not working, here's latest version. </p> <p>Controller:</p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Viewphoto extends CI_Controller { public function __construct() { parent::__construct(); $this-&gt;load-&gt;model('viewphoto_model'); } public function view($id) { $data['photo'] = $this-&gt;viewphoto_model-&gt;get_photo($id); if (empty($data['photo'])) { show_404(); } $data['user'] = $this-&gt;viewphoto_model-&gt;get_user($data['photo']-&gt;userid); if (empty($data['user'])) { show_404(); } $comment = $this-&gt;viewphoto_model-&gt;get_comment($id); if($comment-&gt;num_rows() &gt; 0) { foreach ($comment-&gt;result() as $r) { $data['reg'][$i]['comment']=$r-&gt;comment; $data['reg'][$i]['id']=$r-&gt;id; // Get user details from user table $user_profile = $this-&gt;viewphoto_model-&gt;get_comment_user($r-&gt;userid); if($user_profile-&gt;num_rows() &gt; 0) { foreach ($user_profile-&gt;result() as $row) { // user details whatever you have in your db. $data['reg'][$i]['id']=$row-&gt;id; $data['reg'][$i]['firstname']=$row-&gt;firstname; $data['reg'][$i]['lastname']=$row-&gt;lastname; } } $i++; } } $data['title'] = $data['photo']-&gt;title.' by '.$data['user']-&gt;firstname.' '.$data['user']-&gt;lastname; $data['meta_description'] = $data['photo']-&gt;description; $data['directory'] = 'sub'; $this-&gt;load-&gt;view('templates/header', $data); $this-&gt;load-&gt;view('viewphoto/viewphoto', $data); $this-&gt;load-&gt;view('templates/footer', $data); } } </code></pre> <p>Model: </p> <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Viewphoto_model extends CI_Model { public function get_photo($id) { $db_photos = $this-&gt;load-&gt;database('photos', TRUE); $db_photos-&gt;select('*'); $db_photos-&gt;select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE); $db_photos-&gt;from('photos'); $db_photos-&gt;where('approved', '1'); $db_photos-&gt;where('id', $id); return $db_photos-&gt;get()-&gt;row(); } public function get_user($userid) { $db_users = $this-&gt;load-&gt;database('users', TRUE); $db_users-&gt;select('id, firstname, lastname, email, type, type_staff, count_approved, count_sales, count_comments, count_editorial, featured, subscriber'); $db_users-&gt;from('useraccounts'); $db_users-&gt;where('id', $userid); return $db_users-&gt;get()-&gt;row(); } public function get_comment($id) { $db_photos = $this-&gt;load-&gt;database('photos', TRUE); $db_photos-&gt;select('id, comment, userid, photoid'); $db_photos-&gt;from('comments'); $db_photos-&gt;where('photoid', $id); return $db_photos-&gt;get()-&gt;result(); } public function get_comment_user($userid) { $db_users = $this-&gt;load-&gt;database('users', TRUE); $db_users-&gt;select('id, firstname, lastname'); $db_users-&gt;from('useraccounts'); $db_users-&gt;where('id', $userid); return $db_users-&gt;get(); } } </code></pre> <p>View: </p> <pre><code>&lt;?php foreach ($reg as $comments): ?&gt; &lt;div class="ViewPhoto-CommentsBox"&gt; &lt;? echo $comments['comment']; ?&gt; by &lt;? echo $comments['firstname'];?&gt; &lt;/div&gt; &lt;?php endforeach ?&gt; </code></pre>
    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.
 

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