如何从视图 - CodeIgniter查询数据库 [英] How to query a database from view - CodeIgniter

查看:108
本文介绍了如何从视图 - CodeIgniter查询数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在控制器中运行的查询:

I have query that runs in the controller:

$data['query'] = $this->Member->select_sql($id);    
$this->load->view('myform');

,然后输出视图中的数据:

and then outputs data in the view:

foreach ($query->result() as $row):
   echo $row->post_title;
   echo $row->post_user_id;
endforeach;

因此,这会输出一个用户发布的帖子列表。现在我想运行一个查询,每个后循环通过我的用户表和输出每个帖子旁边的用户信息。 (我不想从一个视图选择数据或联合这两个表在这个时候在MySQL)

So this outputs me a list of posts made by a user. Now I would like to run one more query that would for each post loop through my user table and output user information next to each post. (I dont want to select data from a view or joint those 2 tables at this time in MySQL)

任何想法?

推荐答案

将数据库适配器或适当的表对象注入到视图中。

Inject the database adapter or appropriate table object into the View.

从上面的代码中,我假设这将是

From your code above, I'd assume this would be

$data['userModel'] = $this->User;

然后使用它来运行你的查询,例如

Then use it from there to run your query, e.g.

$user = $userModel->select_sql($row->post_user_id);

这篇关于如何从视图 - CodeIgniter查询数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆