欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號)    在線QQ:181796286
NEWS BLOG ·
學無止境
關注開優網絡 關注前沿
PHP Web開發之權限模塊
PHP Web開發之上傳圖片

PHP Web開發之無限級分類

發表日期:2015-10-26    文章編輯:南昌開優網絡    瀏覽次數:4879    標簽:PHP應用

ThinkPHP中的無限級分類
模版頁category.html
<script type="text/javascript" src="__PUBLIC__/js/jquery.js"></script>
<script type="text/javascript">
function addclick() {
frm.action = "__URL__/add";//添加事件
}
function editclick() {
frm.action = "__URL__/edit";//修改事件
}
function delclick() {
frm.action = "__URL__/del";//刪除事件
}
 
function isSelected(pid){
var caname=$.trim(pid.options[pid.selectedIndex].text);//獲取下拉列表選中的文本去掉前后空格
document.getElementById("txtmodcaname").value = caname;
document.getElementById("txtdelcaname").value = caname;
document.getElementById("hfid").value = pid.options[pid.selectedIndex].value; 
}
</script>
</head>
<body>
<form id="frm" name="frm" method="post" action="">
    <select onchange="isSelected(this);" name="pid" size="20" id="pid">
      <option>分類列表</option>
      <volist name="alist" id="vo">
        <option value="<{$vo['id']}>">
        <php>        
       for($i=0;$i<$vo['count'];$i++){
       echo "&nbsp;";
       }
        </php>       
        <{$vo['caname']}></option>
      </volist>
    </select>
    <input type="text" name="txtaddcaname" id="txtaddcaname" /> 
    <input name="hfid" type="hidden" id="hfid" />
    <input name="btnAdd" type="submit" onclick="addclick();" id="btnAdd" value="添加" /> 
  <input type="text" name="txtmodcaname" id="txtmodcaname" /> 
  <input type="submit" onclick="editclick();" name="btnMod" id="btnMod" value="修改" /> 
    <input type="text" name="txtdelcaname" id="txtdelcaname" />
    <input type="submit" onclick="delclick();" name="btnDel" id="btnDel" value="刪除" />
</form>

控制器
<?php
header ( "Content-Type: text/html;charset=utf-8" );
class CatAction extends Action {
//頁面加載
public function index() {
$cat = new Model ( 'category' );
//list=$cat->query("select * from shop_category order by code asc");
$list = $cat->order('code asc')->select ();
foreach($list as $key=>$value){//key為下標,value為值
$list[$key]['count']=count(explode('0',$value['code']));//explode為分割
}
$this->assign ( 'alist', $list );//分配變量
$this->display ( './Public/category.html' );
}
//添加
public function add() {
$cat = new CatModel ( 'category' );
$caname = trim ( $_POST ['txtaddcaname'] );
if (strlen ( $caname ) == 0) {
echo "<script>alert('請輸入分類名稱');history.back();</script>";
exit ();
}
$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;//獲取pid值
$code = $cat->GenCode ( $pid );//通過傳入的pid生成code
$data ['caname'] = $caname;
$data ['pid'] = $pid;
$data ['code'] = $code;
$list = $cat->add ( $data );
if ($list > 0) {
echo "<script>alert('添加成功');history.back();</script>";
exit ();
} else {
echo "<script>alert('添加失敗');history.back();</script>";
exit ();
}
}
//修改
public function edit() {
$cat = new Model ( 'category' );
$caname=trim($_POST['txtmodcaname']);
if(strlen($caname)==0){
echo "<script>alert('請輸入分類名稱');history.back();</script>";
exit ();
}
$id=$_POST['hfid'];
$data['caname']=$caname;
$list=$cat->where("id=$id")->save($data);
if($list>0){
echo "<script>alert('修改成功');history.back();</script>";
exit ();
}else{
echo "<script>alert('修改失敗');history.back();</script>";
exit ();
}
}
//刪除
public function del() {
$cat = new Model ( 'category' );
$caname=trim($_POST['txtdelcaname']);
if(strlen($caname)==0){
echo "<script>alert('請選擇分類名稱');history.back();</script>";
exit ();
}
$id=$_POST['hfid'];
$list=$cat->where("id=$id")->delete();
if($list>0){
echo "<script>alert('刪除成功');history.back();</script>";
exit ();
}else{
echo "<script>alert('刪除失敗');history.back();</script>";
exit ();
}
}
}
?>

模型
<?php
header ( "Content-Type: text/html;charset=utf-8" );
class CatModel extends Model{
public function GenCode($pid){
$list=$this->where("pid=$pid")->field("max(code)as bh")-> find();
//$list=$this->query("select max(code) from shop_category where pid=$pid");
if($list["bh"]==""||$list["bh"]==null){
if($pid=="0"){
return "01";
}else{
$tmp1=$this->where("id=$pid")->field('code')-> find();
//$tmp=$this->query("select code from shop_category where id=$pid");
return $tmp1['code']."01";
}
}else{
$tmp2=$this->where("pid=$pid")->field("max(code)as bh")-> find();
//$tmp=$this->query("select max(code) from shop_category where pid=$pid");
$left=substr($tmp2['bh'],0,strlen($tmp2['bh'])-1);
$right=substr($tmp2['bh'],strlen($tmp2['bh'])-1)+1;
return $left.$right;
}
}
}
?>
主站蜘蛛池模板: 攀枝花市| 化德县| 陵水| 襄城县| 西盟| 土默特左旗| 浦北县| 陵水| 洛川县| 环江| 阳朔县| 竹山县| 德惠市| 报价| 青海省| 长岛县| 镇安县| 大邑县| 东乌珠穆沁旗| 武隆县| 沙河市| 潮州市| 碌曲县| 囊谦县| 伊金霍洛旗| 普宁市| 邻水| 安多县| 普兰店市| 南城县| 寻乌县| 莲花县| 林西县| 辉南县| 新源县| 富民县| 南江县| 建瓯市| 会宁县| 宽城| 洪雅县|