codeIgniter:将参数从先前的查询传递给选择查询

发布于 2024-08-28 02:26:34 字数 1958 浏览 5 评论 0原文

我正在为浏览器游戏 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笔绘流年 2024-09-04 02:26:34

同意以上两点,加入吧:)

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; ?>

Agreed with both of the above, join 'em :)

membership_model function:

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;
}

View:

<?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; ?>
清风挽心 2024-09-04 02:26:34

使用当前的模型函数,您可以迭代村庄,获取每个村庄的任务。

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 是村庄的数量。更好的解决方案是通过连接村表上的任务表来获取一个查询中的所有数据。

With your current model functions you could iterate the villages, fetching the tasks for each one.

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);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文