2
votes

I Want to Select a value in drop-down automatically based on controller in
codeigniter,when a view is loaded then select field value is auto select by the controller as per requirment. basically i want to manage the drop down from controller.

What should i need to do?

For Example:-

My View File Code is

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

Here is my controllers

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

same as secondcontroller - second then third, fourth are in same way.Is there a way to manage the slect field from controller.

2

2 Answers

0
votes

Could you not just pass an argument to your view telling it which controller is being handled?

Alternatively if you got an instance of the CI object you could parse the URL to see which controller was requested.

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

Best way to make it, is to deal with URI Class by using $this->uri->segment(n).

In your controller, lets say in controllers/firstcontroller.php write the next code:

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

    }

}

And in your views/dropdownpage.php file put this:

Dropdown:
<?php echo $dropdown;?>