duanhe4267 2015-05-11 07:31
浏览 29
已采纳

CodeIgniter - 如何在菜单的部分视图页面中动态选择菜单项?

Basically, I have a menu, which works as a navigation to different pages in my application containing the title of those pages. This menu is created as a partial view. I've a folder named 'partials', inside I've my menu partial view page named 'menu_view.php'. Here's the code of 'menu_view.php':

<div id="menu" class="box">


    <ul class="box">
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins"><span>Home</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/meal2"><span>Add Meal</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/designation"><span>Designation</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/employee2"><span>Employee</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/role"><span>Role</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/user"><span>User</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/meal"><span>Meal Information</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/admin_logins/meal_bill_config"><span>Meal Bill Configuration</span></a></li>
        <li id="<?php if(isset($controller_name)) echo 'selected'; ?>"><a href="<?php echo base_url(); ?>index.php/report_meal_lists/index"><span>Reports</span></a></li>
    </ul>

</div>

In order to show a certain menu item as selected, I've added an array index named 'controller_name' in the respective controller method. For instance, for the 'Designation' title to be selected, I've added the following to my controller method:

function designation() {

        //i've written controller's specific code here. 
        //i'm not showing this as it has no relation with the question

        $data["controller_name"] = 'admin_logins/designation';
        $this->load->view('/admin_logins/designation_view', $data);
}

In the view page('/admin_logins/designation_view'), I've got this:

 $this->load->view('partials/menu_view', $controller_name);

But I don't get my menu item to be selected.

What am I doing wrong?

Note that designation, and most other titles in my menu are from methods of a single controller named 'admin_logins', it is understandable from my code for 'menu_view.php'.

CodeIgniter - 2.2.0, PHP - 5.3.0

  • 写回答

1条回答 默认 最新

  • douwei8295 2015-05-11 07:39
    关注

    Each list item would require an if statement, like this;

    <ul class="box">
        <li id="<?php if(isset($controller_name) == 'admin_logins') echo 'selected'; ?>"><a href="<?php echo site_url('admin_logins'); ?>">Home</a></li>
        <li id="<?php if(isset($controller_name) == 'admin_logins/meal2') echo 'selected'; ?>"><a href="<?php echo site_url('admin_logins/meal2'); ?>">Meal</a></li>
        ...
    </ul>
    

    So you're checking if $controller_name is set, and if it matches the string.

    I also notice you're using "base_url()" for your links, it would make your life easier to use "site_url()" which adds the index.php itself.

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题