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.
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.
Controller:
class Menu extends MX_Controller{
function index() {
$this->load->model('menu_model');
$data['items'] = $this->menu_model->domenii();
$this->load->view('menu', $data);
}
}
Model:
class Menu_model extends CI_Model{
function domenii() {
$query = $this->db->get('domenii');
return $query->result();
}
}
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>
Thanks!
答案 0 :(得分:2)
如果我理解正确,那么我会使用Routes,并将每个请求转发给控制器。像这样;
$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');
}
}
}
希望这有帮助。