Php 从CodeIgniter(或任何MVC平台)中的数据库中提取数据?

Php 从CodeIgniter(或任何MVC平台)中的数据库中提取数据?,php,mysql,sql,model-view-controller,codeigniter,Php,Mysql,Sql,Model View Controller,Codeigniter,我有这个模型: public function index_loop() { $post_user = $this->db->query("SELECT posts.post_title, posts.post_content, posts.post_date, users.username FROM posts LEFT JOIN users ON posts.user_id = users.user_id ORDER BY posts.post_ID DESC"

我有这个模型:

public function index_loop() { 
        $post_user = $this->db->query("SELECT posts.post_title, posts.post_content, posts.post_date, users.username FROM posts LEFT JOIN users ON posts.user_id = users.user_id ORDER BY posts.post_ID DESC");
        //$categories = $this->db->query("SELECT categories.cat_name, categories.cat_id FROM  
        //$comments = $this->db->query("SELECT COUNT(comment_id) FROM comments WHERE
        return $post_user->result_array();
    }
我需要的是显示每个帖子和评论的类别(尽管我想如果我计算出传递的类别,那么评论也是一样的)

在视图文件中:

 <?php foreach($posts as $zz) { ?>
         <div class="article">
           <h2><?php echo $zz['post_title']; ?></h2>
           <p>Posted by <a href=""><?php echo $zz['username']; ?></a> | Filed under <a href="#">templates</a>, <a href=>internet</a></p>
            <p><?php echo $zz['post_content']; ?></p>
           <p><a href=>Read more</a> | <a href=>Comments (5)</a> | <?php echo $zz['post_date']; ?></p>
        </div> <?php } ?>

发布人|存档于

||

所以,若我想在每个博客上循环分类,我需要那个博客ID,那个么若我从模型中进行所有查询,我如何获得它? 评论也一样

我可以做一个复杂的大型数据库查询(这很难,但我可以做),或者我可以做两个或三个单独的小查询,这样好吗?

你可以做一件事

制作不同的分类表和注释表

类别id将与post表中的类别id列链接

在注释表中,创建列post_id,该列将与post表中的post_id链接

然后,您可以通过此扩展查询一次检索所有信息

$post_user = $this->db->query("SELECT posts.post_title, posts.post_content, posts.post_date, users.username FROM posts LEFT JOIN users ON posts.user_id = users.user_id LEFT JOIN category ON category.category_id = post.category_id ORDER BY posts.post_ID DESC");
这是你的分类


作为评论,我不确定您希望输出如何。一种方法是,正如严所说。如果您可以更具体一些,我可以建议一些更简单的方法。

Codeigniter允许您将数据库结果作为对象(例如,模型对象)返回,这使数据更易于处理。您可以向
Posts
表发出初始查询,在结果集中包含
Posts.id
字段,并将
Post\u模型
类的名称传递给
$db->query->result()
函数,告诉codeigniter您希望将结果作为
Post\u模型
类的实例返回

然后,您可以通过
Post\u id
Post\u id
类上定义方法,以
GetCategories
GetComments
Post\u id
,然后调用这些方法来填充查询返回的每个
Post\u模型的
$categories
$comments
集合

下面是一个例子,我希望它能有所帮助:

    public class Post_model extends CI_Model
    {

        // All the properties in the Posts table, as well as a couple variables to hold the categories and comments for this Post:
        public $id;
        public $post_title;
        public $post_content;
        public $post_date;
        public $username;        
        public $categories;
        public $comments;

        public function index_loop() 
        { 
                return $this->GetAllPosts();
        }

        // function to get all posts from the database, including comments and categories.
        // returns an array of Post_model objects
        public function GetAllPosts()
        {
                // define an empty array to hold the results of you query.
                $all_posts = array();

                // define your sql query. NOTE the POSTS.ID field has been added to the field list
                $sql = "SELECT posts.id, 
                               posts.post_title, 
                               posts.post_content, 
                               posts.post_date,
                               users.username
                        FROM posts LEFT JOIN users ON posts.user_id = users.user_id
                        ORDER BY posts.post_id DESC";

                // issue the query
                $query = $this->db->query($sql);

                // loop through the query results, passing a string to result() which represents a class to instantiate 
                //for each result object (note: this class must be loaded)

                foreach($query->result("Post_model") as $post)
                {
                        $post->categories = $this->GetPostCategories($post->id);
                        $post->comments = $this->GetPostComments($post->id);
                        $all_posts[] = $post;
                }

                return $all_posts;
        }

        // function to return categories for a given post_id.
        // returns an array of Category_model objects.
        public function GetPostCategories($post_id)
        {
                $sql = "SELECT category.id, ... WHERE post_id = ?";
                $query = $this->db->query($sql, array($post_id));
                $categories = array();
                foreach($query->result("Category_model") as $category)
                {
                        $categories[] = $category;
                }
                return $categories;
        }

        // function to return comments for a given post_id. 
        //returns an array of Comment_model objects
        public function GetPostComments($post_id)
        {
                $sql = "SELECT comment.id, ... WHERE post_id = ?";
                $query = $this->db->query($sql, array($post_id));
                $comments = array();
                foreach($query->result("Comment_model") as $comment)
                {
                        $comments[] = $comment;
                }
                return $comments;
        }    
}
然后,在视图中,您可以将$posts数组作为Post_模型对象而不是结果_数组进行访问:

<?php foreach($posts as $zz) { ?>
         <div class="article">
           <h2><?php echo $zz->post_title; ?></h2>
           <p>Posted by <a href=""><?php echo $zz->username; ?></a> | 

              Filed under 
               <?php foreach($zz->categories as $category) {
                        echo '<a href="#">{$category->name}</a>, ';
                     }
               ?>
           </p>
           <p><?php echo $zz->post_content; ?></p>
           <p><a href=>Read more</a> | <a href=>Comments (5)</a> | <?php echo $zz->post_date; ?></p>
        </div> <?php } ?>

张贴人|
归档

||


至于效率问题,这将取决于很多因素(数据库是否与web服务器位于同一台机器上?有多少帖子?等等)。单个大型查询的执行速度通常比几个较小的查询快,但需要进行分析才能真正确定性能增益是否值得追求。我总是喜欢尝试编写可读/可理解的代码,而不是以增加复杂性为代价进行优化。

我有单独的类别表,其中类别id和帖子id同时存在,所以只有关系表中的关系id、类别id、帖子id非常感谢您理解我的问题,这就是我需要的,但我不知道它有这么复杂,但我认为我可以处理它,只是我需要更多的努力:)我会再回答你一个小问题,我希望你不要介意你通过“Post_model”来生成$query->result in foreach循环,在文档中它说“代表每个结果对象实例化的类”(注意:这个类必须被加载)“就像你在注释中放的一样,$query->result(“Post_模型”)会返回什么,如果它引用这个当前的类Post_模型,它会访问这个类中的所有方法吗?还有$query->result(“Category_模型”)和注释_模型,我需要将它们放在单独的文件中?$query->result(“Post_模型”)将为当前数据行实例化Post_模型的一个实例,并使用查询返回的字段名称设置实例变量。Category_模型和Comment_模型应分别在各自的文件中定义,就像Post_模型类一样。