【问题标题】:Select a value in drop-down automatically based on controller in codeigniter?根据codeigniter中的控制器自动选择下拉列表中的值?
【发布时间】:2014-01-11 23:00:58
【问题描述】:

我想根据控制器在下拉列表中自动选择一个值
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> 

这是我的控制器

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

与第二个控制器相同 - 第二个、第三个、第四个方式相同。有没有办法从控制器管理 slect 字段。

【问题讨论】:

    标签: php html codeigniter


    【解决方案1】:

    你能不能只向你的视图传递一个参数来告诉它正在处理哪个控制器?

    或者,如果您有 CI 对象的实例,您可以解析 URL 以查看请求的控制器。

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

    【讨论】:

      【解决方案2】:

      最好的办法是使用$this-&gt;uri-&gt;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;?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        • 2020-06-18
        • 2011-09-15
        • 1970-01-01
        • 2015-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多