codeIgniter:将参数从先前的查询传递给选择查询
我正在为浏览器游戏 travian 创建一个小管理工具。因此,我从数据库中选择所有村庄,并且想显示每个村庄特有的一些内容。但为了查询这些独特的详细信息,我需要传递村庄的 ID。我该怎么做?
这是我的代码(控制器):
function members_area()
{
global $site_title;
$this->load->model('membership_model');
if($this->membership_model->get_villages())
{
$data['rows'] = $this->membership_model->get_villages();
$id = 1;//this should be dynamic, but how?
if($this->membership_model->get_tasks($id)):
$data['tasks'] = $this->membership_model->get_tasks($id);
endif;
}
$data['title'] = $site_title." | Your account";
$data['main_content'] = 'account';
$this->load->view('template', $data);
}
这是我在模型中使用的 2 个函数:
function get_villages()
{
$q = $this->db->get('villages');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
function get_tasks($id)
{
$this->db->select('name');
$this->db->from('tasks');
$this->db->where('villageid', $id);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $task) {
$data[] = $task;
}
return $data;
}
}
当然还有视图:
<?php foreach($rows as $r) : ?>
<div class="village">
<h3><?php echo $r->name; ?></h3>
<ul>
<?php foreach($tasks as $task): ?>
<li><?php echo $task->name; ?></li>
<?php endforeach; ?>
</ul>
<?php echo anchor('site/add_village/'.$r->id.'', '+ add new task'); ?>
</div>
<?php endforeach; ?>
ps:请不要删除第一段代码中的注释!
I'm creating a little management tool for the browser game travian. So I select all the villages from the database and I want to display some content that's unique to each of the villages. But in order to query for those unique details I need to pass the id of the village. How should I do this?
this is my code (controller):
function members_area()
{
global $site_title;
$this->load->model('membership_model');
if($this->membership_model->get_villages())
{
$data['rows'] = $this->membership_model->get_villages();
$id = 1;//this should be dynamic, but how?
if($this->membership_model->get_tasks($id)):
$data['tasks'] = $this->membership_model->get_tasks($id);
endif;
}
$data['title'] = $site_title." | Your account";
$data['main_content'] = 'account';
$this->load->view('template', $data);
}
and this is the 2 functions I'm using in the model:
function get_villages()
{
$q = $this->db->get('villages');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
function get_tasks($id)
{
$this->db->select('name');
$this->db->from('tasks');
$this->db->where('villageid', $id);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $task) {
$data[] = $task;
}
return $data;
}
}
and of course the view:
<?php foreach($rows as $r) : ?>
<div class="village">
<h3><?php echo $r->name; ?></h3>
<ul>
<?php foreach($tasks as $task): ?>
<li><?php echo $task->name; ?></li>
<?php endforeach; ?>
</ul>
<?php echo anchor('site/add_village/'.$r->id.'', '+ add new task'); ?>
</div>
<?php endforeach; ?>
ps: please do not remove the comment in the first block of code!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
同意以上两点,加入吧:)
membership_model 功能:
查看:
Agreed with both of the above, join 'em :)
membership_model function:
View:
使用当前的模型函数,您可以迭代村庄,获取每个村庄的任务。
但这需要 N+1 个 SQL 查询,其中 N 是村庄的数量。更好的解决方案是通过连接村表上的任务表来获取一个查询中的所有数据。
With your current model functions you could iterate the villages, fetching the tasks for each one.
But this requires N+1 SQL queries, where N is the number of villages. A better solution would be to fetch all data in one query by joining the tasks table on the villages table.