我正在为浏览器游戏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);
}
这是我在模型中使用的两个函数:
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:请不要删除第一个代码块中的注释!
答案 0 :(得分:1)
同意以上两点,加入'em:)
membership_model功能:
function get_villages_and_tasks() {
$query_results = $this->db
->select('villages.id,villages.name AS villagename,tasks.name AS taskname',false)
->join('tasks','tasks.villageid = villages.id','left')
->get('villages')->result_array();
$return_array = array();
foreach($query_results as $row) {
if(!isset($return_array[$row['id']]) {
$return_array[$row['id']] = array(
'villagename'=>$row['villagename']
);
}
if(!empty($row['taskname'])) {
$return_array[$row['id']]['tasks'][]=array('taskname'=>$row['taskname']);
}
}
return $return_array;
}
查看:强>
<?php foreach($villages as $villageid=>$village) : ?>
<div class="village">
<h3><?php echo $village['villagename']; ?></h3>
<ul>
<?php if(!empty($village['tasks'])): ?>
<?php foreach($village['tasks'] as $task): ?>
<li><?php echo $task['taskname']; ?></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
<?php echo anchor('site/add_village/'.$villageid.'', '+ add new task'); ?>
</div>
<?php endforeach; ?>
答案 1 :(得分:0)
使用您当前的模型功能,您可以迭代村庄,为每个村庄获取任务。
function members_area()
{
global $site_title;
$this->load->model('membership_model');
// Fetch villages
$villages = $this->membership_model->get_villages();
if($villages)
{
$tasks = array();
// Iterate through villages fetching tasks
foreach ($villages as $village)
{
$tasks[$village->id] = $this->membership_model->get_tasks($village->village->id);
}
$data['villages'] = $villages;
$data['tasks'] = $tasks;
}
$data['title'] = $site_title." | Your account";
$data['main_content'] = 'account';
$this->load->view('template', $data);
}
但这需要N + 1个SQL查询,其中N是村庄的数量。更好的解决方案是通过加入村庄表上的任务表来获取一个查询中的所有数据。
答案 2 :(得分:0)
我在本周早些时候的Codeigniter论坛中问道,我认为这是一个相关的问题。
http://ellislab.com/forums/viewthread/151624/
希望这会有所帮助......