0

我有两个表 'category' 和 'organization' 。在我看来,我想列出在各个类别表下工作的所有组织。我所做的如下所示:

*这是我的控制器:*

function organization()
{   
    $data['category'] = $this->category_model->get_all_category();
    $data['organization'] = $this->organization_model->get_categorised_organization();        
    $data['title'] = "Welcome to the organization page";        
    $this->load->view('organization_index',$data);
}

这是模型:

 function get_categorised_organization()     function get_categorised_organization() {
        $category = $this->category_model->get_all_category();
        $i = 0;
        foreach ($category as $c):
            $sql = "SELECT * FROM ss_organization where org_working_area='$c->category_name'";
            $query = $this->db->query($sql);
            $result[] = $query->result();
        endforeach;
        return $result;
    } {
        $category = $this->category_model->get_all_category();
        $i = 0;
        foreach ($category as $c):
            $sql = "SELECT * FROM ss_organization where org_working_area='$c->category_name'";
            $query = $this->db->query($sql);
            $result[] = $query->result();
        endforeach;
        return $result;
    }

这是视图

 <?php foreach($category as $c): ?>
    <div class="categorybox">
        <h2><?php echo $c->category_name;?></h2><hr>
        <ul>
          <?php //print_r($organization); die();?>
         <?php foreach($organization as $o):?>
           <?php foreach($o as $p): ?>


        <li><a href="<?php echo base_url();?>index.php/home_controller/organization_detail/<?php echo $p->org_id;?>"><?php echo $p->org_name;?></a></li>
         <?php endforeach; ?>
        <?php endforeach; ?>

        </ul>
    </div>
   <?php endforeach;?>

现在我得到的是不同类别的相同组织..我怎样才能让各自的组织在各自的类别下工作

4

1 回答 1

0

我制作了我的版本,请适应您的代码

控制器:

function organization()
{ 
    $this->load->model('myModel');
    $data['category'] = $this->myModel->getAllCategory();
    $data['title'] = "Welcome to the organization page";        
    $this->load->view('organization_index',$data);
}

模型:

public function getAllCategory()
{
    $query = $this->db->get('category');

    if($this->db->_error_number() > 0)
    {
        return false;
    }
    else
    {
        return $query->result_array();
    }
}

function getOrganizationByCategory($category_id) {

    $this->db->where('category_id = ' . (int)$category_id);

    $query = $this->db->get('ss_organization');
    if($this->db->_error_number() > 0)
    {
        return false;
    }
    else
    {
        return $query->result_array();
    }
}

看法:

<?php foreach($category as $c): ?>
  <div class="categorybox">
    <h2><?php echo $c['name'];?></h2><hr>
    <ul>
      // here you call your function to display organizations by category
      <?php $organization = $this->myModel->getOrganizationByCategory($c['id']);?>
      <?php foreach($organization as $o):?>
         <li><a href="#"><?php echo $o['name'];?></a></li>
      <?php endforeach; ?>
    </ul>
  </div>
<?php endforeach; ?>
于 2013-07-07T15:42:05.060 回答