数据库中的Codeigniter菜单 [英] Codeigniter Menu from Database

查看:284
本文介绍了数据库中的Codeigniter菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Codeigniter和本地数据库创建了一个菜单,其中包含不同图书的域。我可以显示所有的域名,并建立菜单的视觉,但我不知道如何让他们可点击,点击后,只显示属于点击的域的书籍。

I've made a menu using Codeigniter and a local database containing domains for different books. I was able to display all the domains name and build up the menu visually, but I don't really know how to make them clickable and after click, to only display the books that belong to the clicked domain.

例如,如果有人点击域X,我想将用户重定向到website.com/X并在X域中显示图书。

For example if someone clicks on the domain X, I want the user to be redirected to website.com/X and to display the books in the X domain.

我不知道从哪里开始,以及如何做到这一点。我不是要求有人为我写代码,而是为了解释我应该做的步骤,以实现这一点。

I'm not sure where to start, and how to actually do it. I'm not asking for someone to write the code for me, but to explain me the steps I should make in order to accomplish this.

这里有一些代码,我认为

Here's some code that I think it's relevant.

控制器:

class Menu extends MX_Controller{
    function index() {
        $this->load->model('menu_model');

        $data['items'] = $this->menu_model->domenii();

        $this->load->view('menu', $data);

    }

}

型号: / p>

Model:

class Menu_model extends CI_Model{
    function domenii() {
        $query = $this->db->get('domenii');
        return $query->result();
    }
}

检视(Bootstraped):

View (Bootstraped):

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand col-lg-6 col-md-6 col-xs-6" href="<?php echo base_url(); ?>" title="Librarie">Librarie</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">

    <?php foreach ($items as $item): ?>
    <li><a href=""><?php echo $item->nume_domeniu; ?></a></li>
    <?php endforeach; ?>

      <li class="custom-li">
      <?php echo form_open('search/cauta'); ?>
      <?php echo form_input(array('name'=>'element', 'class'=>'form-control', 'placeholder' => 'Cautare',)); ?>
      <?php echo form_submit(array('name'=>'search_submit','value' => 'Search', 'class'=>' hidden')); ?>
      <?php echo form_close(); ?> </li> </ul>

    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>


$ b

谢谢!

Thanks!

推荐答案

如果我理解你的权利,那么我会使用路由为此,并转发每个请求到控制器。像这样;

If I am understanding you right, then I would use Routes for this, and forward every request to a controller. Like this;

$route['(:any)'] = 'controller/method/$1';

现在,控制器方法;

function method($name = null)
{
    if(is_null($name))
    {
        echo 'no $name found...';
    }
    else
    {
        // Name was found, make sure it exists
        if ($item = $this->model->check_name($name))
        {
            // Yes the name exists
            var_dump($item);
        }
        else
        {
            show_error('invalid $name');
        }
    }

}

这篇关于数据库中的Codeigniter菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆