codeigniter将参数传递给控制器

时间:2014-07-09 09:15:56

标签: php mysql codeigniter

我需要将参数从视图传递给控制器​​..

控制器

<?php

class Site2 extends CI_Controller{

    function index(){
        $this->load->helper('url');
        $this->home();
    }

    public function getBranchDetails($b_id){
        $this->load->model('bank_account_model');
        $data['rresults'] = $this->bank_account_model->getAccount($b_id);
        $this->load->view('view_nav',$data);
    }

    public function home(){
        $this->load->model('get_company_model');
        $this->load->model('bank_account_model');
        $data['results'] = $this->get_company_model->get_All();
        //$data['site2']=$this; 
        $this->load->view('view_header');
        $this->load->view('view_nav',$data);
        $this->load->view('view_content');
        $this->load->view('view_footer');
    }
public function company_details($id){
        $this->load->model('company_detail_model');
        $data['company_result'] = $this->company_detail_model->getRecords($id);
        $this->load->model('get_company_model');
        $data['results'] = $this->get_company_model->get_All();
        $this->load->view('view_header');
        $this->load->view('view_nav',$data);
        $this->load->view('company_details',$data);
        $this->load->view('view_footer');
    }

查看

<?php
    foreach($results as $row):
    ?>
    <div>
        <ol class="tree">
    <li>
        <label for="folder1"><a href="<?php echo site_url('site2/company_details/'.$row->id.''); ?>"><?=$row->name?></label></a> <input type="checkbox"  id="folder1" /> 
        <ol>
        <?php
        //here need to pass is ($row->id);
        foreach($myresult as $row2):
        ?>
            <li>
                <label for="subfolder1"><a href="#"><?=$row2->name?></a></label> <input type="checkbox" id="subfolder1" /> 

            </li>
            <?php
            endforeach;
            ?>
        </ol>
    </li>       
</ol>
    </div>

    <?php
    endforeach;
    ?>

在第一个foreach循环内部的视图中,我需要将$ row-&gt; id作为参数传递给控制器​​函数company_details。(此代码的目的是从DB获取第一个公司ID,然后根据需要获取分支公司ID。)

2 个答案:

答案 0 :(得分:0)

在控制器中整理数据并将其传递给视图

public function home()
{
    $this->load->model('get_company_model');
    $this->load->model('bank_account_model');
    $data['rresults'] = $this->bank_account_model->getAccount($b_id);
    $results = $this->get_company_model->get_All();
    foreach ($results as $row) {
        $row->branches = $this->your_branch_model->getAllBranchesOfCompany($row->id);
    }
    $data['results'] = $results;
    $this->load->view('view_header');
    $this->load->view('view_nav', $data);
    $this->load->view('view_content');
    $this->load->view('view_footer');
}

并在视图中循环每个公司数据及其分支索引

<?php foreach ($results as $row):?>
    <div>
        <ol class="tree">
            <li>
                <label for="folder1"><a
                        href="<?php echo site_url('site2/company_details/' . $row->id . ''); ?>"><?= $row->name ?>
                </label></a> <input type="checkbox" id="folder1"/>
                <ol>
                    <?php
                    foreach ($row->branches as $row2):
                        ?>
                        <li>
                            <label for="subfolder1"><a href="#"><?= $row2->name ?></a></label> <input type="checkbox"id="subfolder1"/>
                        </li>
                    <?php
                    endforeach;
                    ?>
                </ol>
            </li>
        </ol>
    </div>
<?php
endforeach;
?>

另一种方法是使用公司ID对公司和分支机构订单数据进行加入查询,并通过结果循环显示公司名称,并为其所有分支显示公司名称,以供参考Combining Three tables into correct view format

答案 1 :(得分:0)

它100%工作

<强>控制器

<?php

class Site2 extends CI_Controller{

    function index(){
        $this->load->helper('url');
        $this->home();
    }

    public function home(){
        $this->load->model('get_company_model');
        $this->load->model('bank_account_model');
        $data['results_company'] = $this->get_company_model->get_All();
        $data['results_branch'] = $this->get_company_model->get_branch();
        $data['results_banks'] = $this->get_company_model->get_banks();
        $this->load->view('view_header');
        $this->load->view('view_nav',$data);
        //$this->load->view('view_nav',$data_branch);
        $this->load->view('view_content');
        $this->load->view('view_footer');
        //$this->load->view('check',$data);
    }

查看

 <?php    foreach($results_company as $row):  ?>

        <ol class="tree">
    <li>
        <label for="folder1"><a href="<?php echo site_url('site2/company_details/'.$row->id.''); ?>"><?php echo $row->name; ?></label></a> <input type="checkbox"  id="folder1" /> 
        <ol>
                <?php    foreach($results_branch as $row_branch): $count; ?>
                                    <?php 

                                    if($row_branch->companyid == $row->id){?>
                                        <li><label for="subfolder1"><a href="<?php echo site_url('site2/company_details/'.$row->id.''); ?>"><?php echo $row_branch->name; ?></a></label> <input type="checkbox" id="subfolder1" />

                                            <ol>

                                                    <?php    foreach($results_banks as $row_bank): $count2; ?>
                                                    <?php 

                                                    if($row_bank->branch_id == $row_branch->id){?>
                                                    <li><label for="childfolder1"><a href="<?php echo site_url('site2/company_details/'.$row_bank->branch_id.''); ?>"><?php echo $row_bank->bank;  ?></a></label> <input type="checkbox" id="subfolder1" /></li>
                                                    <?php } ?>

                                                    <?php   endforeach; ?>

                                            </ol>

                                    <?php } ?>
                </li>
                                    <?php   endforeach; ?>



        </ol>
    </li>       
</ol>


    <?php
    endforeach;
    ?>