DESTOON程序很不错,使用过程中,发现级别只能单选,有时会出现一条信息可以是新品,也可以是推荐或热门等,或者需要实现一条数据在多处调用。
碰巧这次有机会,我就二开这个功能,让级别选项可以多选,且修改后,tag模板标签level条件使用不用做任何更改,还是和之前一样1到9。
第一步,把相应数据表中的level列类型修改为SMALLINT;
第二步,修改edit.tpl.php,在标题栏的下方增加代码:
<tr>
<td class="tl">级别</td>
<td><?php echo level_checkbox('post[level][]', $level);?></td>
</tr>
第三步,修改index.tpl.php,把原来显示级别的代码替换下方代码:
<?php echo level_icon($v['level']); ?>第四步,修改相应功能模块的类文件(*.class.php),我用module/buy/buy.class.php示例,在set方法中增加代码:
unset($post['content']); // 找到这行,在下方添加 $post['level'] = level_post(empty($post['level']) ? 0 : $post['level']);
第五步,修改get_list方法:
global $MOD, $pages, $page, $pagesize, $offset, $items, $sum; // 找到上面那行,在下方添加 $condition = level_to_pow_sql($condition);第六步,修改level方法,这个方法代码较少,我全部复制过来了:
$itemids = is_array($itemid) ? implode(',', $itemid) : $itemid;
$level = level_pow($level);
DB::query("UPDATE {$this->table} SET level=$level WHERE itemid IN ($itemids)");
第七步,把上面使用的函数加到global.func.php文件中去,函数代码如下:
/**
* 级别-多选
*
* @param $name
* @param int $level
* @return string
*/
function level_checkbox($name, $level = 0) {
global $MOD, $L;
$names = isset($MOD['level']) && $MOD['level'] ? $MOD['level'] : '';
$names = $names ? explode('|', trim($names)) : array();
$select = '';
for($i = 1; $i < 10; $i++) {
$val = level_pow($i);
$n = isset($names[$i-1]) ? ' '.$names[$i-1] : '';
$select .= '<label><input type="checkbox" name="'.$name.'" value="'.$val.'" '.(($level & $val) ? ' checked' : '').'>'.$i.' '.$L['level'].$n.' </label>';
}
return $select;
}
/**
* 普通级别转位运算
*
* @param int $level
* @return float|int
*/
function level_pow($level = 0) {
if (!$level) {
return 0;
}
return pow(2, $level - 1);
}
/**
* 级别表单提交
* @param $level
* @return float|int
*/
function level_post($level) {
if (empty($level)) {
return 0;
}
if (is_array($level)) {
return array_sum($level);
}
return $level;
}
/**
* SQL中级别条件转换为位运算
*
* @param string $condition
* @return string|string[]
*/
function level_to_pow_sql($condition = '') {
$is_level = preg_match("/level(?:\s?=\s?)([1-9])/i", $condition, $matches);
if ($is_level && !empty($matches[1])) {
$condition = str_replace($matches[0], '`level`&'.level_pow($matches[1]).'>0', $condition);
}
return $condition;
}
/**
* 显示级别ICON
* @param $levels
* @param int $num 显示数量
* @return string
*/
function level_icon($levels, $num = 0) {
$icon = '';
if (!$levels) {
return $icon;
}
for($i = 1; $i < 10; $i++) {
$val = level_pow($i);
if ($levels & $val) {
$icon .= "<a href=\"javascript:;\" onclick=\"Dq('level',{$i});\"><img src=\"admin/image/level_{$i}.gif\" title=\"{$i}级\" alt=\"{$i}级\"/></a>";
if ($num >= $i) {
break;
}
}
}
return $icon;
}
至此,刷新网站后台,应该能看到如下图的效果了!
添加或编辑效果图
列表效果图