dongrao1862 2013-07-07 08:08
浏览 47

获取循环codeigniter中存在的参数的分类结果

I have got two tables 'category' and 'organization' . Under my view i want to list all those organizations which work under respective categories of category table . What i did is shown below :

*Here's my controller : *

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

Here's the model :

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

and here's the view

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

Now what i'm getting is same organizations over different categories .. How can i get respective organizations working under the respective category

  • 写回答

1条回答 默认 最新

  • dsj2014 2013-07-07 15:42
    关注

    I make my version of this, and please just adapt to your code

    Controller:

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

    Model:

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

    View:

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

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示