在Codeigniter中的foreach循环中运行查询

时间:2012-01-03 11:39:05

标签: codeigniter

基本上我正在尝试建立一个评论系统。用户查看网站上的照片,然后可以查看其他成员发表的所有评论。

每个评论都将使用foreach(当前正常工作)循环,但我需要做的是对每个评论运行单独的查询以获取发布它的用户的用户详细信息。这些细节存储在一个单独的数据库中(否则我只是做一个连接)。

到目前为止,我的模型已经包含了这个内容:

public function get_comment($id)
{
    $db_photos = $this->load->database('photos', TRUE);
    $db_photos->select('id, comment, userid, photoid');
    $db_photos->from('comments');
    $db_photos->where('photoid', $id);

    return $db_photos->get()->result();
}

这是控制器:

public function view($id)
    {   

        $data['comment'] = $this->viewphoto_model->get_comment($id);
        if (empty($data['comment'])) { show_404(); }

        $this->load->view('templates/header', $data);
        $this->load->view('viewphoto/viewphoto', $data);
        $this->load->view('templates/footer', $data);
    }

然后是观点:

<?php foreach ($comment as $comments): ?>
        <div class="ViewPhoto-CommentsBox">
        <? echo $comments->comment; ?>
        </div>
        <?php endforeach ?>

所以基本上我需要从每个评论中获取'userid'值,然后在'users'数据库上运行查询,以获取发布的每条评论的用户详细信息。

非常感谢任何帮助:)

编辑:

仍然无法使用,这是最新版本。

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Viewphoto extends CI_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->model('viewphoto_model');
    }


    public function view($id)
    {
        $data['photo'] = $this->viewphoto_model->get_photo($id);
        if (empty($data['photo'])) { show_404(); }

        $data['user'] = $this->viewphoto_model->get_user($data['photo']->userid);
        if (empty($data['user'])) { show_404(); }


        $comment = $this->viewphoto_model->get_comment($id);
        if($comment->num_rows() > 0)
            {
                foreach ($comment->result() as $r)
                {
                    $data['reg'][$i]['comment']=$r->comment;
                    $data['reg'][$i]['id']=$r->id;           

                   // Get user details from user table
                    $user_profile = $this->viewphoto_model->get_comment_user($r->userid);
                    if($user_profile->num_rows() > 0)
                    {
                        foreach ($user_profile->result() as $row)
                        {
                            // user details whatever you have in your db.
                            $data['reg'][$i]['id']=$row->id;
                            $data['reg'][$i]['firstname']=$row->firstname;
                            $data['reg'][$i]['lastname']=$row->lastname;
                        }
                    }

                    $i++;
                }
            }

        $data['title'] = $data['photo']->title.' by '.$data['user']->firstname.' '.$data['user']->lastname;
        $data['meta_description'] = $data['photo']->description;
        $data['directory'] = 'sub';

        $this->load->view('templates/header', $data);
        $this->load->view('viewphoto/viewphoto', $data);
        $this->load->view('templates/footer', $data);
    }
}

型号:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Viewphoto_model extends CI_Model {


    public function get_photo($id)
    {
        $db_photos = $this->load->database('photos', TRUE);
        $db_photos->select('*');
        $db_photos->select("DATE_FORMAT(uploaddate, '%d/%m/%y') as uploaddate_formatted", FALSE);
        $db_photos->from('photos');
        $db_photos->where('approved', '1');
        $db_photos->where('id', $id);

        return $db_photos->get()->row();
    }

    public function get_user($userid)
    {
        $db_users = $this->load->database('users', TRUE);
        $db_users->select('id, firstname, lastname, email, type, type_staff, count_approved, count_sales, count_comments, count_editorial, featured, subscriber');
        $db_users->from('useraccounts');
        $db_users->where('id', $userid);

        return $db_users->get()->row();
    }


    public function get_comment($id)
    {
        $db_photos = $this->load->database('photos', TRUE);
        $db_photos->select('id, comment, userid, photoid');
        $db_photos->from('comments');
        $db_photos->where('photoid', $id);

        return $db_photos->get()->result();
    }


    public function get_comment_user($userid)
    {
        $db_users = $this->load->database('users', TRUE);
        $db_users->select('id, firstname, lastname');
        $db_users->from('useraccounts');
        $db_users->where('id', $userid);

        return $db_users->get();
    }

}

查看:

<?php foreach ($reg as $comments): ?>
        <div class="ViewPhoto-CommentsBox">
        <? echo $comments['comment']; ?> by <? echo $comments['firstname'];?>
        </div>
        <?php endforeach ?>

2 个答案:

答案 0 :(得分:4)

像这样改变你的控制器。

 public function view($id)
        {       
              $comment= $this->viewphoto_model->get_comment($id);

              if($comment->num_rows() > 0)
                {
                    foreach ($comment->result() as $r)
                    {
                        $data['reg'][$i]['comment']=$r->comment;
                        $data['reg'][$i]['id']=$r->id;           

                       // Get user details from user table
                        $user_profile=$this->viewphoto_model->get_user_profile_details($r->userid);
                        if($user_profile->num_rows() > 0)
                        {
                            foreach ($user_profile->result() as $row)
                            {
                                // user details whatever you have in your db.
                                $data['reg'][$i]['name']=$row->name;
                                $data['reg'][$i]['gender']=$row->gender;
                                $data['reg'][$i]['phone_no']=$row->phone_no;
                            }
                        }

                        $i++;
                    }
                }
            $this->load->view('templates/header', $data);
            $this->load->view('viewphoto/viewphoto', $data);
            $this->load->view('templates/footer', $data);
        }

查看文件:您可以使用以下详细信息。

<?php if(isset($reg)) { foreach ($reg as $comments): ?>
        <div class="ViewPhoto-CommentsBox">
        <? echo $comments['comment']; ?>
         <? echo $comments['name']; // user detail ?>
        </div>
        <?php endforeach ?>
        <? } ?>

答案 1 :(得分:0)

那是有效的! :

$searchquery = "SELECT * FROM your_table";
        $resultquery = $this->db->query($searchquery);
        $i = 0;
        if($resultquery->num_rows() > 0)
                {
                    foreach ($resultquery->result() as $query)
                    {
                        $data['reg'][$i]['id']=$query->id;
                        $data['reg'][$i]['name']=$query->name;
                        $data['reg'][$i]['pers']=$query->pers;
                        $data['reg'][$i]['sex']=$query->sex;
                        $i++;
                    }
            }

对于观点:

<?php if(isset($reg)) { foreach ($reg as $chambres): ?>
                    <li>
                        <input type="hidden" value="<?php echo $query['id']; ?>" />
                        <input type="text" value="<?php echo $query['name']; ?>" placeholder="numero de la chambre" />
                        <input type="text" value="<?php echo $query['pers']; ?>" placeholder="Nombre de personne" />
                        <textarea><?php echo $query['sex']; ?></textarea>
                    </li>
                <?php endforeach ?>
                <?php } ?>

谢谢srbhbarot。