|
本帖最后由 tugh 于 2011-2-17 16:30 编辑
大家好我是一个新手,上周开始接触CI, 我一贯很懒,学习速度很慢,刚开始学的时候自己做个空项目很难做成东西。所以每次学新的技术下载大量的代码,修改改善原有的项目来学习其中的知识。周一的时候下载STBLOG安装,看了一天代码,觉得Saturn 以及 STBLOG组写的这个程序非常好,非常容易看懂。当发现菜单时用一个插件来生成而不是后台管理的时候就有了自己写一下的冲动 , 因为插件只生成独立页面的菜单,我相信很多童鞋还想要某个分类或每个分类的菜单。所以动手写了一些这个功能。再声明一下本人是菜鸟,写这个也是为了学习所以希望高手们多多指导。
首先列出新建的文档:
PHP复制代码
APPHOME/controllers/admin/menu.php
APPHOME/views/admin/manage_menu.php
APPHOME/views/admin/menu_cate_form.php
APPHOME/models/menu_mdl.php
复制代码
首先在数据库中创建一个menu表
SQL复制代码
DROP TABLE IF EXISTS `menu`;
CREATE TABLE `menu` (
`menu_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '项目主键',
`name` VARCHAR(200) DEFAULT NULL COMMENT '名称',
`url` VARCHAR(50) NOT NULL COMMENT '菜单地址',
`description` VARCHAR(200) DEFAULT NULL COMMENT '选项描述',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '项目状态',
`order` INT(10) UNSIGNED DEFAULT '0' COMMENT '项目排序',
PRIMARY KEY (`menu_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
复制代码
再创建相关的MVC,
一下是新增文档源代码:
PHP复制代码 APPHOME/controllers/admin/menu.php : 复制代码
PHP复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Menu extends ST_Auth_Controller {
/**
* 传递到对应视图的数据
*
* @access private
* @var array
*/
private $_data = array();
/**
* 当前操作Menu的ID
*
* @access private
* @var int
*/
private $_mid = 0;
public function __construct ()
{
parent ::__construct ();
$this->load->model('menu_mdl');
/** privilege confirm */
$this->auth->exceed('editor');
/** 标题 */
$this->_data ['page_title'] = '菜单';
$this->_data ['parentPage'] = 'manage-posts';
$this->_data ['currentPage'] = 'manage-menu';
}
/**
* 默认执行函数
*
* @access public
* @return void
*/
public function index ()
{
redirect ('admin/menu/manage');
}
/**
* 管理页面
*
* @access public
* @param string $type 类型
* @param int
$mid ID
* @return void
*/
public function manage ($menu_id = NULL)
{
$mid = $menu_id;
$this->_data ['menu'] = $this->menu_mdl->list_menu();
$this->_data ['category'] = $this->metas_mdl->list_metas("category");
$this->_data ['page'] = $this->menu_mdl->list_pages();
$this->_data ['menu_arr'] = $this->menu_mdl->url_to_name();
$ret_data = $this->_data ['menu_arr'] ;
if($mid && is_numeric($mid))
{
$this->_data ['menu_id'] = $mid;
$menu = $this->menu_mdl->get_menu($mid);
$this->_data ['name'] = $menu->name;
$this->_data ['order'] = $menu->order;
$this->_data ['status'] = $menu->status;
$this->_data ['description'] = $menu->description;
$menu_url = $menu->url;
$menu_array = explode("/",$menu_url);
#var_dump($menu_array); exit;
//$this->_data['menuurl'] = $menu_array['1'].'-'.$menu_array['3'];
$this->_data ['type'] = $menu_array['1'];
$this->_data ['slug'] = $menu_array['2'];
unset($menu);
}
$type= 1;
$this->_operate ($type, $mid);
$this->load->view('admin/manage_menu',$this->_data );
}
/**
* 新增和编辑菜单
*
* @access private
* @param string $type 类型
* @param int
$mid ID
* @return void
*/
private function _operate ($type,$mid)
{
/** 验证需要 */
$this->_mid = $mid;
$this->_load_validation_rules ();
if($this->form_validation->run() === FALSE)
{
return;
}
else
{
$action = $this->input->post('do',TRUE);
$name = $this->input->post('name',TRUE);
$url = $this->input->post('menu_type',TRUE);
$order = $this->input->post('order',TRUE);
$status = $this->input->post('status',TRUE);
$description = $this->input->post('description',TRUE);
$data = array(
'name' => $name,
'url' => $url ,
'status' => $status,
'order' => $order,
'description' => (!$description)? NULL : $description
);
if('insert' == $action)
{
$this->menu_mdl->add_menu($data);
$this->session->set_flashdata('success', '菜单添加成功');
}
if('update' == $action)
{
$this->menu_mdl->update_menu($mid, $data);
$this->session->set_flashdata('success', '菜单更新成功');
}
go_back ();
}
}
/**
* 加载验证规则
*
* @access private
* @return void
*/
private function _load_validation_rules ()
{
$this->form_validation->set_rules('name', '名称', 'required|trim|callback__name_check|htmlspecialchars');
$this->form_validation->set_rules('order', '顺序', 'required|trim|callback__order_check');
$this->form_validation->set_rules('description', '描述', 'trim|htmlspecialchars');
}
/**
* 启用函数
*
* @access public
* @param string $mid
* @return void
*/
public function enable ($mid){
$enabled = 0;
$this->menu_mdl->enable_menu($mid);
$enabled++ ;
$msg = ($enabled>0) ? '启用成功' : '没有菜单被启用';
$notify = ($enabled>0) ? 'success':'error';
$this->session->set_flashdata($notify, $msg);
go_back ();
}
/**
* 禁用函数
*
* @access public
* @param string $mid
* @return void
*/
public function disable ($mid){
$disabled = 0;
$this->menu_mdl->disable_menu($mid);
$disabled++ ;
$msg = ($disabled>0) ? '禁用成功' : '禁用菜单被启用';
$notify = ($disabled>0) ? 'success':'error';
$this->session->set_flashdata($notify, $msg);
go_back ();
}
/**
* 操作分发
*
* @access public
* @param string $type 类型
* @return void
*/
public function operate ()
{
$action = $this->input->post('do',TRUE);
switch ($action)
{
case 'delete':
$this->_remove ();
break;
case 'enable':
$this->_enable ();
break;
case 'disable':
$this->_disable ();
break;
default:
show_404 ();
break;
}
}
/**
* 删除
*
* @access private
* @return void
*/
private function _remove ()
{
$menus = $this->input->post('mid',TRUE);
$deleted = 0;
if ($menus)
{
foreach ($menus as $menu)
{
$this->menu_mdl->remove_menu($menu);
$deleted++ ;
}
}
$msg = ($deleted>0) ? '删除成功' : '没有菜单被删除';
$notify = ($deleted>0) ? 'success':'error';
$this->session->set_flashdata($notify, $msg);
go_back ();
}
/**
* 启用
*
* @access private
* @return void
*/
private function _enable ()
{
$menus = $this->input->post('mid',TRUE);
$enabled = 0;
if ($menus)
{
foreach ($menus as $menu)
{
$this->menu_mdl->enable_menu($menu);
$enabled++ ;
}
}
$msg = ($enabled>0) ? '启用成功' : '没有菜单被启用';
$notify = ($enabled>0) ? 'success':'error';
$this->session->set_flashdata($notify, $msg);
go_back ();
}
/**
* 禁用
*
* @access private
* @return void
*/
private function _disable ()
{
$menus = $this->input->post('mid',TRUE);
$disabled = 0;
if ($menus)
{
foreach ($menus as $menu)
{
$this->menu_mdl->disable_menu($menu);
$disabled++ ;
}
}
$msg = ($disabled>0) ? '禁用成功' : '没有菜单被禁用';
$notify = ($disabled>0) ? 'success':'error';
$this->session->set_flashdata($notify, $msg);
go_back ();
}
/**
* 回调函数:检查Name是否唯一
*
* @access public
* @param $str 输入值
* @return bool
*/
public function _name_check ($str)
{
if($this->menu_mdl->check_exist('name', $str, $this->_mid ))
{
$this->form_validation->set_message('_name_check', '已经存在一个为 '.$str.' 的名称');
return FALSE;
}
return TRUE;
}
/**
* 回调函数:检查顺序是否符合条件
*
* @access public
* @param $str 输入值
* @return bool
*/
public function _order_check ($str){
#
var_dump(is_numeric ($str));exit;
if(!is_numeric ($str)){
$this->form_validation->set_message('_order_check', '顺序只能从0-20的数字');
return FALSE;
}
if($str<0 || $str >20){
$this->form_validation->set_message('_order_check', '顺序只能从0-20的数字');
return FALSE;
}
return TRUE;
}
}
/* End of file Menu.php */
/* Location: ./application/controllers/admin/Menu.php */
复制代码
PHP复制代码 APPHOME/views/admin/manage_menu.php 复制代码HTML复制代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->load->view('admin/header');
$this->load->view('admin/menu');
?>
<div class="main">
<div class="body body-950">
<?php $this->load->view('admin/notify'); ?>
<div class="container typecho-page-title">
<div class="column-24 start-01">
<h2><?php echo $page_title;?></h2>
<p><?php echo anchor(site_url(),'查看我的站点');?></p>
</div>
</div>
<div class="container typecho-page-main manage-metas">
<div class="column-16 suffix">
<form method="post" name="manage_categories" class="operate-form" action="<?php echo site_url('admin/menu/operate/');?>">
<div class="typecho-list-operate">
<p class="operate">操作:
<span class="operate-button typecho-table-select-all">全选 </span>,
<span class="operate-button typecho-table-select-none">不选 </span>
选中项:
<span rel="delete" lang="此分类下的所有内容将被删除, 你确认要删除这些分类吗?" class="operate-button operate-delete typecho-table-select-submit">删除 </span>,
<span rel="enable" class="operate-button typecho-table-select-submit">启用 </span>,
<span rel="disable" class="operate-delete operate-button typecho-table-select-submit">禁用 </span>,
</p>
</div>
<table class="typecho-list-table draggable">
<colgroup>
<col width="25"/>
<col width="120"/>
<col width="30"/>
<col width="140"/>
<col width="30"/>
<col width="95"/>
<col width="30"/>
<col width="95"/>
</colgroup>
<thead>
<tr>
<th class="typecho-radius-topleft"> </th>
<th>名称 </th>
<th> </th>
<th>对应的分类或页面 </th>
<th> </th>
<th >状态 </th>
<th> </th>
<th class="typecho-radius-topright">顺序 </th>
</tr>
</thead>
<tbody>
<?php if($menu->num_rows() > 0):?>
<?php foreach($menu->result() as $cate):?>
<tr<?php echo ($cate->menu_id % 2==0)?'':' class="even"'; ?> id=" <?php echo 'category-'.$cate->menu_id; ?>">
<td><input type="checkbox" value="<?php echo $cate->menu_id; ?>" name="mid[]"/> </td>
<td>
<?php echo anchor('admin/menu/manage/'.$cate->menu_id,$cate->name);?>
</td>
<td>
<a class="right hidden-by-mouse" href="<?php echo site_url($cate->url); ?>"> <img src="<?php echo base_url();?>application/views/admin/images/view.gif" title=" <?php echo '浏览 '. $cate->name; ?>" width="16" height="16" alt="view" /> </a>
</td>
<td><?php echo $menu_arr[$cate->menu_id];?> </td>
<td>
</td>
<?php ?>
<td><a href="<?php $en_dis_able = ""; if($cate->status=="1") en_dis_able = "disable";else: $en_dis_able = "enable";endif; echo site_url('admin/menu/'.$en_dis_able.'/'.$cate->menu_id);?>" class="balloon-button center size- <?php echo Common::split_by_count($cate->status, 1, 10, 20, 50, 100); ?>"> <?php if($cate->status=="1")$status_out = "已启用";else $status_out = "已禁用";echo $status_out;?> </a></td>
<td></td>
<td><a class="balloon-button center size-<?php echo Common::split_by_count($cate->order, 1, 10, 20, 50, 100); ?>"> <?php echo $cate->order;?> </a></td>
</tr>
<?php endforeach;?>
<?php else:?>
<tr class="even">
<td colspan="6"><h6 class="typecho-list-table-title">没有任何菜单 </h6></td>
</tr>
<?php endif;?>
</tbody>
</table>
<input type="hidden" name="do" value="delete" />
</form>
</div>
<div class="column-08 typecho-mini-panel typecho-radius-topleft typecho-radius-topright typecho-radius-bottomleft typecho-radius-bottomright">
<?php $this->load->view('admin/menu_cate_form');?>
</div>
</div>
</div>
</div>
<?php
include 'copyright.php';
include 'common-js.php';
?>
<script type="text/javascript">
(function () {
window.addEvent('domready', function() {
var _selection;
<?php if (isset($menu_id)): ?>
var _hl = $(document).getElement('.typecho-mini-panel');
if (_hl) {
_hl.set('tween', {duration: 1500});
var _bg = _hl.getStyle('background-color');
if (!_bg || 'transparent' == _bg) {
_bg = '#F7FBE9';
}
_hl.tween('background-color', '#AACB36', _bg);
}
<?php endif; ?>
});
})();
</script>
<?php $this->load->view('admin/footer'); ?>
复制代码PHP复制代码 APPHOME/views/admin/menu_cate_form.php : 复制代码HTML复制代码 <form action="<?php echo site_url('admin/menu/manage'.((isset($menu_id) && is_numeric($menu_id))?'/'.$menu_id:''));?>" method="post" enctype="application/x-www-form-urlencoded">
<ul class="typecho-option" id="typecho-option-item-name-0">
<li>
<label class="typecho-label" for="name-0-1">
菜单名称* </label>
<?php echo form_error('name', '<p class="message error">', ' </p>'); ?>
<input id="name-0-1" name="name" type="text" class="text" value="<?php echo set_value('name',(isset($name))?htmlspecialchars_decode($name):''); ?>"/>
</li>
</ul>
<ul class="typecho-option" id="typecho-option-item-slug-1">
<li>
<label class="typecho-label" for="slug-0-2">
对应分类或页面 </label>
<?php echo form_error('menu_type', '<p class="message error">', ' </p>'); ?>
<select id="menu_type" name="menu_type">
<?php
$type = (isset($type))?htmlspecialchars_decode($type):'';
$slug = (isset($slug))?htmlspecialchars_decode($slug):'';
foreach($category->result() as $cate):
if($type=="category" && $slug==$cate->slug ):
$select_cat = "selected";
else:
$select_cat = "";
endif;
?>
<option <?php echo $select_cat;?> value=" <?php echo "/category/".$cate->slug;?>"> <?php echo $cate->name;?> </optin>
<?php endforeach;?>
<?php foreach($page->result() as $pate):
if($type=="page" && $slug==$pate->slug ):
$select_page = "selected";
else:
$select_page = "";
endif;
?>
<option <?php echo $select_page;?> value=" <?php echo "/pages/".$pate->slug;?>"> <?php echo $pate->title;?> </optin>
<?php endforeach;?>
</select>
<p class="description">
创建分类或独立页面对应的菜单选项 </p>
</li>
</ul>
<ul class="typecho-option" id="typecho-option-item-slug-1">
<li>
<label class="typecho-label" for="menu_status">
菜单状态 </label>
<?php echo form_error('slug', '<p class="message error">', ' </p>'); ?>
<?php $status = (isset($status))?htmlspecialchars_decode($status):'';
$checked = ""; $checker = "";
if($status == "1"): $checked = "selected";
else: $checker = "selected";
endif;
?>
<select id="menu_status" name="status">
<option value="1" <?php echo $checked;?>>启用 </option>
<option value="0" <?php echo $checker;?>>禁用 </option>
</select>
<p class="description">
创建分类或独立页面对应的菜单选项 </p>
</li>
</ul>
<ul class="typecho-option" id="typecho-option-item-name-0">
<li>
<label class="typecho-label" for="name-0-1">
菜单顺序 </label>
<?php echo form_error('order', '<p class="message error">', ' </p>'); ?>
<input id="name-0-1" name="order" type="text" class="text" value="<?php echo set_value('order',(isset($order))?htmlspecialchars_decode($order):''); ?>"/>
<p class="description">
填写0-20 数字,0级别最高。顺序值一样的按创建时间来排序 </p>
</li>
</ul>
<ul class="typecho-option" id="typecho-option-item-description-2">
<li>
<label class="typecho-label" for="description-0-3">
菜单描述 </label>
<?php echo form_error('description', '<p class="message error">', ' </p>'); ?>
<textarea id="description-0-3" name="description"><?php echo set_value('description',(isset($description))?htmlspecialchars_decode($description):''); ?>
</textarea>
<p class="description">
此文字用于描述菜单,为提醒自己用 </p>
</li>
</ul>
<input name="do" type="hidden" value="<?php echo (isset($menu_id) && is_numeric($menu_id))?'update':'insert';?>" />
<ul class="typecho-option typecho-option-submit" id="typecho-option-item--5">
<li>
<button type="submit">
<?php echo (isset($menu_id) && is_numeric($menu_id))?'更新分类':'添加菜单';?>
</button>
</li>
</ul>
</form>
复制代码
PHP复制代码 APPHOME/models/menu_mdl.php : 复制代码PHP复制代码 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Menu_mdl extends Model {
const TBL_METAS = 'metas';
const TBL_MENU = 'menu';
const TBL_RELATIONSHIPS = 'relationships';
const TBL_POSTS = 'posts';
/**
* 文章元数据
*
* @access public
* @var mixed
*/
public $menu = NULL;
/**
* 构造函数
*
* @access public
* @return void
*/
public function __construct ()
{
parent ::Model();
log_message ('debug', "STBLOG: Menu Model Class Initialized");
}
/**
* 获取所有MENUS
*
* @access public
* @param strint $type 类型
* @return object
*/
public function list_menu ($type = "1")
{
if($type=="1"){
//显示全部 为管理用
$this->db->select(self::TBL_MENU.'.*');
return $this->db->get(self::TBL_MENU);
}else{
//只显示已启用的 为生成菜单用
$this->db->select(self::TBL_MENU.'.*');
$this->db->where(self::TBL_MENU.'.status', "1");$this->db->orderby(self::TBL_MENU.'.order', "ASC");
return $this->db->get(self::TBL_MENU);
}
}
/**
* 获取所有pages
*
* @access public
* @return object
*/
public function list_pages ()
{
$type = "page";
$this->db->select(self::TBL_POSTS.'.*');
$this->db->where(self::TBL_POSTS.'.type', $type);
return $this->db->get(self::TBL_POSTS);
}
/**
* 计算menu个数
*
* @access public
* @return int
*/
public function count_menus ()
{
return $this->db->count_all_results(self::TBL_MENU);
}
/**
* 获取元数据
*
* @access public
*
@param string $name 元数据名称
*
@return object - result object
*/
public function get_menu ( $name = '')
{
if(empty($name)) exit();
$this->db->where(self::TBL_MENU.'.menu_id', intval($name));
$return_data = $this->db->get(self::TBL_MENU)->row();
return $return_data;
}
/**
* 检查menu是否存在
*
* @access public
* @param string - $key 栏位名
* @param string - $value 内容
* @param int - $exclude_mid 要排除的mid
* @return bool
*/
public function check_exist ($key = 'name', $value = '', $exclude_mid = 0)
{
$this->db->select('menu_id')->from(self::TBL_MENU)->where($key, trim($value));
if(!empty($exclude_mid) && is_numeric($exclude_mid))
{
$this->db->where('menu_id !=', $exclude_mid);
}
$query = $this->db->get();
$num = $query->num_rows();
$query->free_result();
return ($num > 0) ? TRUE : FALSE;
}
// -----------------------CRUD---------------------------------------------
/**
* 添加menu
*
* @access public
* @param array $menu_data 内容
* @return boolean 成功与否
*/
public function add_menu ($menu_data)
{
$this->db->insert(self::TBL_MENU, $menu_data);
return ($this->db->affected_rows() ==1) ? $this->db->insert_id() : FALSE;
}
/**
* 修改内容
*
* @access public
* @param int - $data 内容信息
* @return boolean - success/failure
*/
public function update_menu ($mid, $data)
{
$this->db->where('menu_id', intval($mid));
$this->db->update(self::TBL_MENU, $data);
return ($this->db->affected_rows() ==1) ? TRUE : FALSE;
}
/**
* 删除一个内容
*
* @access public
* @param int - $mid 内容id
* @return boolean - success/failure
*/
public function remove_menu ($mid)
{
$this->db->delete(self::TBL_MENU, array('menu_id' => intval($mid)));
return ($this->db->affected_rows() ==1) ? TRUE : FALSE;
}
/**
* 启用一个内容
*
* @access public
* @param int - $mid 内容id
* @return boolean - success/failure
*/
public function enable_menu ($mid)
{
$this->db->query('UPDATE '.self::TBL_MENU.' SET `status` = "1" WHERE `menu_id`='.$mid.'');
return ($this->db->affected_rows() ==1) ? TRUE : FALSE;
}
/**
* 禁用一个内容
*
* @access public
* @param int - $mid 内容id
* @return boolean - success/failure
*/
public function disable_menu ($mid)
{
$this->db->query('UPDATE '.self::TBL_MENU.' SET `status` = "0" WHERE `menu_id`='.$mid.'');
return ($this->db->affected_rows() ==1) ? TRUE : FALSE;
}
/**
*
*
* @access public
* @param int - $mid 内容id
* @return boolean - success/failure
*/
public function url_to_name (){
$ret_data = array();
$menus = $this->list_menu();
$categories = $this->metas_mdl->list_metas("category");
$pages = $this->list_pages();
if ($menus->num_rows() > 0)
{
$menu_arr = $menus->result_array();
}
if ($categories->num_rows() > 0)
{
$cat_arr = $categories->result_array();
}
if ($pages->num_rows() > 0)
{
$page_arr = $pages->result_array();
}
foreach ($menu_arr as $menu){
$url_array = explode("/", $menu['url']);
$url_type = $url_array['1'];
$url_slug = $url_array['2'];
if($url_type=="category"){
foreach ($cat_arr as $cat){
if($cat['slug'] == $url_slug ){
$ret_data[$menu['menu_id']] = $cat['name'];
}
}
}else{
foreach ($page_arr as $page){
if($page['slug']== $url_slug ){
$ret_data[$menu['menu_id']] = $page['title'];
}
}
}
}
return $ret_data;
}
}
/* End of file Menu_mdl.php */
/* Location: ./application/models/Menu_mdl.php */
复制代码
修改的文档:
PHP复制代码
APPHOME /views /admin /menu .php
APPHOME /controllers /admin /settings .php
APPHOME /views /admin /settings_general .php
数据库中 ->settings 表 增加一个值 menu_status
Themes /default/header.php
复制代码
现在我们在后台菜单中增加菜单管理菜单项即可
打开 APPHOME/views/admin/menu.php 文档,找到 “管理”, 在合适的地方添加如下代码。
PHP复制代码
<li <?php echo ($currentPage=='manage-menu')? ' class="focus"':'';?>> <?php echo anchor (site_url ('admin/menu/manage/'),'菜单',array('title'=>'菜单'));?></li>
复制代码
即可在后台管理下出现菜单一项。
现在只剩下一个问题了 如何在首页呈现菜单。
因为已有Saturn 开发的一个插件是生产菜单,所以为了避免与其冲突在后台设置增加了菜单开关。
打开 APPHOME/views/admin/settings_general.php 文档,找到 “站点关闭”的label, 在其之前添加如下代码:(推荐添加到这里,你还可以添加到任何你想要的合适的位置)
HTML复制代码 <label class="typecho-label">
是否关闭菜单 </label>
<span>
<input name="menu_status" id="blog_status_0" type="radio" value="on" <?php echo set_radio('menu_status','on',('on' == $menu_status)?TRUE:FALSE); ?> />
<label for="blog_status_0">
不关闭 </label>
</span>
<span>
<input name="menu_status" type="radio" value="off" id="blog_status_1" <?php echo set_radio('menu_status','off',('off' == $menu_status)?TRUE:FALSE ); ?> />
<label for="blog_status_1">
关闭 </label>
</span>
<p class="description warn" >
关闭菜单时,需要激活菜单插件。菜单插件功能十分简单所以我们推荐您不要关闭菜单。 </p>
</li>
</ul>
复制代码
多加了一个label到View就得改变其Controller
PHP复制代码
/** 获取配置数据 */
$setting = array();
$setting['blog_title']
= $this->input->post('blog_title',TRUE);
$setting['blog_slogan'] = $this->input->post('blog_slogan',TRUE);
$setting['blog_description'] = $this->input->post('blog_description',TRUE);
$setting['blog_keywords'] = $this->input->post('blog_keywords',TRUE);
$setting['offline_reason'] = $this->input->post('offline_reason',TRUE);
$setting['upload_dir'] = $this->input->post('upload_dir',TRUE);
$setting['upload_exts'] = $this->input->post('upload_exts',TRUE);
$setting['blog_status'] = $this->input->post('blog_status',TRUE);
$setting['menu_status'] = $this->input->post('menu_status',TRUE);
//Added by Tugh 只增加了这行就可以了
if(empty($setting['upload_dir']))
复制代码
你在数据库settings表中增加一个记录,name=menu_status.
SQL复制代码
INSERT INTO settings(name)
VALUES ('menu_status')
复制代码
现在只剩下最后一步工作了 也就是在首页视图中添加菜单生成的语句。
打开 Themes/default/header.php 文件: 找到<div id="menu">
其改成如下
HTML复制代码 <div id="menu">
<ul>
<li><?php echo anchor(site_url(), '首页');?></li>
<?php if(setting_item('menu_status')=='on'): $menus = get_menus(); // 如果菜单功能打开了,就得到菜单并输出?>
<?php if($menus->num_rows() > 0):?>
<?php foreach($menus->result() as $cate):?>
<li><?php echo anchor($cate->url, $cate->name);?> </li>
<?php endforeach;?>
<?php endif;?>
<?php
else: //如果关闭状态则启用菜单插件, 不过记得 插件是激活了才能使用
$this->plugin->trigger('Widget::Navigation', ' <li><a href="{permalink}">{title} </a></li>');
?>
<?php endif;?>
</ul>
</div> 复制代码
代码都贴出来有点长了 不知道会不会有人看。 最后贴几张效果图:
|
评分
-
查看全部评分
|