2013-12-23 2 views
2

컨트롤러에서 자동으로 값을 선택하려면
codeigniter,보기가로드 될 때 선택 필드 값은 컨트롤러 당 컨트롤러에 의해 자동 선택됩니다. 기본적으로 컨트롤러를 에서 드롭 다운을 관리하고 싶습니다.codeigniter의 컨트롤러를 기반으로 드롭 다운의 값을 자동으로 선택 하시겠습니까?

어떻게해야합니까? 예를 들어

: -

내 파일보기 코드가 여기에

<select id="category"> 
<option value="first">first</option> 
<option value="second">second</option>  
<option value="Third">Third</option>  
<option value="fourth">fourth</option>  
</select> 

입니다 내 컨트롤러 같은 secondcontroller로

firstcontroller,secondcontroller,thirdcontroller,fourthcontroller 
Required Code:- firstcontroller load select field value is first. 

- 다음 세 번째, 네 번째 같은에 초 컨트롤러에서 slect 필드를 관리 할 수있는 방법이 있습니까?

답변

0

처리중인 컨트롤러를 알리는 인수를보기에 전달할 수 있습니까?

또는 CI 개체의 인스턴스가 있으면 URL을 구문 분석하여 요청 된 컨트롤러를 확인할 수 있습니다. 그것을 만드는

$CI =& get_instance(); 
$controller = $CI->uri->segment(1); 
$controller_pretty = str_replace('controller', '', $controller); 
0

가장 좋은 방법은, $this->uri->segment(n)를 사용하여 URI Class 처리하는 것입니다. 컨트롤러에서

, 다음 코드 쓰기 controllers/firstcontroller.php에 말할 수 :

class Firstcontroller extends CI_Controller{ 

    public function index() 
    { 
     // Load form helper 
     $this->load->helper('form'); 

     // First we need to catch first segment of the URL as it's name of the controller 
     // so $this->uri->segment(1) will be the first part/segment after your base url (www.website.com/segment1) 
     $current_controller = $this->uri->segment(1); 

     // Array of available options for dropdown element 
     $dropdownOptions = array(
      'firstcontroller' => '1st Controller', 
      'secondcontroller' => '2nd Controller', 
      'thirdcontroller' => '3rd Controller', 
      'fourthcontroller' => '4th Controller', 
     ); 

     // Assign a form_dropdown function to variable for later use in our view file 
     // form_dropdown will generate a <select /> html element for you 
     $data['dropdown'] = form_dropdown('dropdown_name', $dropdownOptions, $current_controller); 

     // Load a view with datas 
     $this->load->view('samplepage', $data); 

    } 

} 

그리고 당신의 views/dropdownpage.php 파일에 넣어이 :

Dropdown: 
<?php echo $dropdown;?> 
관련 문제