已在控制器

时间:2017-11-04 10:09:39

标签: codeigniter

这是我的控制器。在这里我声明了current_company。

public function index($id='')
{   
    $this->load->model('Company_model');
    ($id!='') ? $data["company_details"]=$this->Company_model->get_company($id) :'';

($id!='') ? $data ["current_company"]=$this->Company_model->get_currentcomp($id) :'';

    $this->load->view('includes/header');
    $this->load->view('includes/left_menu');
    $this->load->view('company/manage',($id!='') ? $data : '');
    $this->load->view('includes/footer');
}

这是我的模特。这里我宣布了函数

class Company_model extends CI_Model{

     protected $strTableName = 'suc_company';
     function __construct(){

         parent::__construct ();
         $this->db->from($this->strTableName);
     }

     function get_currentcomp($intPkId){            

         $this->db->where('pk_bint_company_id',$intPkId);
         $q1 = $this->db->get($this->strTableName);            
         return $q1->result_array()[0];
     }

这是视图部分。在这里我调用$ current_company!== FALSE然后

 <div class="form-group">
     <label for="company_package" class="col-sm-3 control-label"> Package</label>
     <div class="col-sm-9 col-xs-12"> 
         <?php if ($current_company !== FALSE ) {?>                                     
             <select name="company_package"  class="form-control select2" disabled="">
             <option value="<?php echo $company_package;?>" selected=""><?php echo $packagename;?></option>

             </select>
         <?php } else { ?>

         <?php } ?>

错误

  

正在发生错误...未定义的数据current_company

2 个答案:

答案 0 :(得分:2)

更改您的控制器代码。

public function index($id='')
{   
    $this->load->model('Company_model');

    $data["company_details"] = false;
    $data["current_company"] = false;

    if($id != ''){
        $data["company_details"] = $this->Company_model->get_company($id);
        $data["current_company"] = $this->Company_model->get_currentcomp($id);
    }

    $this->load->view('includes/header');
    $this->load->view('includes/left_menu');
    $this->load->view('company/manage',$data);
    $this->load->view('includes/footer');
}

答案 1 :(得分:0)

如评论中所述,您可能会将$ data传递给查看文件。你必须限制这种振荡。 试试这个:

public function index($id='')
{   
    if ((int)$id < 1) {
        redirect('some/generic/place', 'refresh');
    }

    // we have integer in parameter
    // so we will check if data by that parameter exists

    $data = [];// initialization of array so we are sure $data is set

    $this->load->model('Company_model');

    $data['company_details'] = $this->Company_model->get_company($id);

    if ($data['company_details']) {
        $data['current_company'] = $this->Company_model->get_currentcomp($id) :'';
    } else {
        // in this point $data is an empty array
    }

    // $this way variable will be available in all view files
    $this->load->var($data);

    $this->load->view('includes/header');
    $this->load->view('includes/left_menu');
    $this->load->view('company/manage');
    $this->load->view('includes/footer');

    // so now, in your view you would have wether company with details wether an empty array

    // *first line of code assumes parameter would be an integer
    // **also assumed that $this->Company_model->get_company($id) would return false/null/anEmptyArray if data doesn't exist
}