|
本帖最后由 spt119 于 2010-10-18 14:19 编辑
前几日帮朋友写了个小程序,其中有用到PHP无限分类。关于无限分类,有很多实现的方式,但多基于对数据库的多次操作,而且是递归查询。虽然对于小型项目而言,递归与否以及查询数据库的次数对效率的影响是可以无视的,但还是本着精益求精的想法,重新理顺思路写了一个。
这个无限分类程序基于纯PHP操作,没有javascript参与。你完全可以在代码的基础上重新开发一下即可。多了就不白话了,直接上代码:
1.数据库
SQL复制代码 CREATE TABLE IF NOT EXISTS `tuan_cate` (
`cid` INT(11) NOT NULL AUTO_INCREMENT,
`fatherid` INT(11) NOT NULL DEFAULT '0',
`catename` VARCHAR(50) DEFAULT NULL,
`lineid` INT(11) NOT NULL DEFAULT '0',
`content` VARCHAR(1000) DEFAULT NULL,
`path` VARCHAR(32) DEFAULT NULL,
`rank` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`cid`),
KEY `catename` (`catename`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=71 ; 复制代码
2.控制器
PHP复制代码
function __construct (){
$this->cate = $this->cate_model->get();
$data ['cate'] = $this->cate;
}
function update ()
{
if( empty($cid) )
{
$cid = $this->input->post('cid',TRUE);
$path = $this->input->post('father',TRUE);
$sql = array(
'catename' => $this->input->post('catename',TRUE),
'lineid' => $this->input->post('lineid',TRUE),
'content' => htmlspecialchars($this->input->post('content',true))
);
}
else
{
$this->cateinfo = $this->cate_model->get_one($cid);
echo 'path = '.$path.' - this->cateinfo->path = '.$this->cateinfo->path;
if( $this->cateinfo->path != $path )
{
$path_change = 1;
}
else
{
$path_change = 0;
}
}
if( empty( $cid ) )
{//insert new cate
$this->cate_model->insert($sql,$path);
}
else
{//modify the exists cate
$this->cate_model->update($cid,$sql,$path,$path_change);
}
$this->dblog_model->add();
//redirect('back/tuan_cate','refresh');
}
}
复制代码
3.模型
PHP复制代码 function get ()
{
$this->db->order_by('path','asc');
$query = $this->db->get('cate');
if($query->num_rows() > 0)
{
return $query->result_array();
}
}
function get_one ($cid)
{
$this->db->where('cid',$cid);
$query = $this->db->get('cate');
if($query->num_rows > 0)
{
return $query->row();
}
}
function insert ($sql,$path)
{
$this->db->insert('cate',$sql);
$new_id = $this->db->insert_id();
$new_path = $path.','.$new_id;
$p = explode(',',$path);
rsort($p);
$fatherid = $p[0];
$rank = explode(',',$new_path);
$this->db->where('cid',$new_id);
$this->db->update('cate',array( 'fatherid' => $fatherid,'path' => $new_path,'rank' => count($rank) ));
}
function update ($cid,$sql,$path,$path_change)
{
if($path_change == 1)
{
$new_path = $path.','.$cid;
$p = explode(',',$path);
rsort($p);
$fatherid = $p[0];
$rank = explode(',',$new_path);
$this->db->where('cid',$cid);
$this->db->update('cate',array( 'fatherid' => $fatherid,'path' => $new_path,'rank' => count($rank) ));
}
$this->db->where('cid',$cid);
$this->db->update('cate',$sql);
}
复制代码
4.视图
以下是分类显示部分;
PHP复制代码 <?php foreach((array) $cate as $key => $c){?>
<tr>
<td>
<?php echo '┣';
if( $c['rank'] > 0 )
{
for( $i=1;$i<=$c['rank'];$i++ )
{
if($i > 0 AND $i < $c['rank']) {echo '━';}
if($i >= $c['rank']) {echo '━';}
}
}
echo ' <span id=red>'.$c['rank'].'级</span> '.$c['catename'];
?>
</td>
<td> <?php echo $c['catename'];?></td>
<td> <?php echo $c['lineid'];?></td>
<td> <?php echo $c['content'];?></td>
<td><a id="add1" href=" <?php echo site_url ('back/tuan_cate/update/'.$c['cid']);?>#add">修改</a> | <a href=" <?php echo site_url ('back/tuan_cate/delete/'.$c['cid']);?>">删除</a></td>
</tr>
<?php }unset ($c);?> 复制代码
以下是类的添加和修改部分:
PHP复制代码
<select name="father">
<?php foreach( (array)$cate as $key => $c )
{
echo "<option value=\"".$c['path']."\"";
if( ! empty($cid) )
{
if( $c['cid'] == $modify->cid ) echo ' selected';
}
echo '>';
echo '┣';
if( $c['rank'] > 0 )
{
for( $i=0;$i<=$c['rank'];$i++ )
{
if($i > 0 AND $i < $c['rank']) {echo '━';}
if($i >= $c['rank']) {echo '━';}
}
}
echo " ".$c['rank']."级 ".$c['catename']."</option>\n";
}
?>
</select> 复制代码 |
评分
-
查看全部评分
|