夺命雷公狗ThinkPHP项目之----商城10商品属性管理
我們一般做項目前就要分析業(yè)務(wù)邏輯先,這次也不例外.
?
attr_type:是指屬性的類型,有唯一,單選和多選之分
?
唯一屬性,是指用戶在購買商品時,可以看到的擴(kuò)展屬性如下圖所示:
單選屬性,是指用戶在購買的時候,不需要選擇的擴(kuò)展屬性,否則就無法購買,如下所示:
多選和單選是對應(yīng)的,但是可以選擇多個,但是單選的只能選擇一個,否則就無法購買。
?
?
attr_input_type:是指屬性的輸入方式,有文本框,下拉列表和文本域之分,如下圖所示:
?
attr_value:是指如果屬性是下拉形式的,應(yīng)該提供可選值。
如果該屬性是下拉列表形式的,幾必須提供可選值,如下圖所示,如果其他輸入方式為空即可。
?
?
說白了attr_type是提供給普通用戶使用的,attr_input_type一般是給后臺管理員使用的。
擴(kuò)展屬性在整個商品模塊中的位置目前保存屬性本身,并不是具體某個商品的屬性值。
我們這里面要用到TP里面的模型進(jìn)行量表關(guān)聯(lián)查詢。
?
明白了表結(jié)構(gòu)和邏輯后,那么下一步就開始寫代碼了。
首先在model層創(chuàng)建一個AttriburtModel.class.php來對他進(jìn)行驗證,保證屬性名稱不能為空
<?phpnamespace Admin\Model;use Think\Model;class AttributeModel extends Model{//自動驗證規(guī)則protected $_validate = array(array('attr_name','require','屬性名稱不呢個為空'),);}?
下一步就開始寫控制器了,代碼如下所示:
<?phpnamespace Admin\Controller;use Think\Controller;class AttributeController extends CommonController{public function index(){$this -> display();}public function add(){if(IS_POST){//入庫$data['attr_name'] = I('attr_name');$data['type_id'] = I('type_id');$data['attr_type'] = I('attr_type');$data['attr_input_type'] = I('attr_input_type');$data['attr_value'] = I('attr_value');$attrModel = D('attribute');if($attrModel->create($data)){//通過驗證if($attrModel->add()){$this -> success('添加屬性成功',U('index'),1);}else{$this -> error('添加屬性失敗');}}else{//沒通過驗證,提示錯誤信息$this -> error($attrModel->getError());}return;}//獲取所有的商品類型$types = M('goods_type')->select();$this -> assign('types',$types);$this -> display();}public function edit(){$this -> display();}public function del(){$this -> display();}}?
下一步開始寫add.html模板了,代碼如下所示:
?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SHOP 管理中心 - 屬性管理 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="__ADMIN__/styles/general.css" rel="stylesheet" type="text/css" /> <link href="__ADMIN__/styles/main.css" rel="stylesheet" type="text/css" /> </head> <body><h1> <span class="action-span"><a href="index.php?p=admin&c=attribute&a=index">商品屬性</a></span> <span class="action-span1"><a href="index.php?act=main">SHOP 管理中心</a> </span><span id="search_id" class="action-span1"> - 添加屬性 </span> <div style="clear:both"></div> </h1><div class="main-div"><form action="" method="post" name="theForm" οnsubmit="return validate();"><table width="100%" id="general-table"><tbody><tr><td class="label">屬性名稱:</td><td><input type="text" name="attr_name" value="" size="30"><span class="require-field">*</span> </td></tr><tr><td class="label">所屬商品類型:</td><td><select name="cat_id" οnchange="onChangeGoodsType(this.value)"><option value="0">請選擇...</option><volist name="types" id="vo"><option value="{$vo['type_id']}">{$vo['type_name']}</option></volist></select> <span class="require-field">*</span> </td></tr><tr id="attrGroups" style="display: none;"><td class="label">屬性分組:</td><td><select name="attr_group"></select></td></tr><tr><td class="label"><a href="javascript:showNotice('noticeAttrType');" title="點擊此處查看提示信息"><img src="__ADMIN__/images/notice.gif" width="16" height="16" border="0" alt="點擊此處查看提示信息"></a>屬性是否可選</td><td><label><input type="radio" name="attr_type" value="0" checked="true"> 唯一屬性</label> <label><input type="radio" name="attr_type" value="1"> 單選屬性</label> <label><input type="radio" name="attr_type" value="2"> 復(fù)選屬性</label> <br><span class="notice-span" style="display:block" id="noticeAttrType">選擇"單選/復(fù)選屬性"時,可以對商品該屬性設(shè)置多個值,同時還能對不同屬性值指定不同的價格加價,用戶購買商品時需要選定具體的屬性值。選擇"唯一屬性"時,商品的該屬性值只能設(shè)置一個值,用戶只能查看該值。</span></td></tr><tr><td class="label">該屬性值的錄入方式:</td><td><label><input type="radio" name="attr_input_type" value="0" checked="true" οnclick="radioClicked(0)">手工錄入</label> <label><input type="radio" name="attr_input_type" value="1" οnclick="radioClicked(1)">從下面的列表中選擇(一行代表一個可選值)</label> <label><input type="radio" name="attr_input_type" value="2" οnclick="radioClicked(0)">多行文本框</label> </td></tr><tr><td class="label">可選值列表:</td><td><textarea name="attr_value" cols="30" rows="5" disabled=""></textarea></td></tr><tr><td colspan="2"><div class="button-div"><input type="submit" value=" 確定 " class="button"><input type="reset" value=" 重置 " class="button"></div></td></tr></tbody></table><input type="hidden" name="act" value="insert"><input type="hidden" name="attr_id" value="0"></form> </div><div id="footer">版權(quán)所有 © 2014-2016 奪命雷公狗 - 技術(shù)總結(jié) - </div> </div> <script type="text/javascript"> /*** 點擊類型按鈕時切換選項的禁用狀態(tài)*/ function radioClicked(n) {document.forms['theForm'].elements["attr_value"].disabled = n > 0 ? false : true; }</script> </body> </html>?
下一步就是看是做他的列表頁了。
列表頁分3步走,
1.顯示所有的屬性
?
2.顯示分頁
?
3.按照商品類型進(jìn)行篩選
?
屬性的入口是在-商品類型-下的屬性列表進(jìn)行修改的,我已經(jīng)將商品類型的頁面搭建起來的,如下圖所示:
?
?
?
下一步就開始寫他的列表功能了,先將他的模版弄好,然后再看似下一步。
?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SHOP 管理中心 - 屬性管理 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="__ADMIN__/styles/general.css" rel="stylesheet" type="text/css" /> <link href="__ADMIN__/styles/main.css" rel="stylesheet" type="text/css" /> </head> <body><h1> <span class="action-span"><a href="index.php?p=admin&c=attribute&a=add">添加屬性</a></span> <span class="action-span1"><a href="index.php?act=main">SHOP 管理中心</a> </span><span id="search_id" class="action-span1"> - 商品屬性 </span> <div style="clear:both"></div> </h1><div class="form-div"><form action="" name="searchForm"><img src=" __ADMIN__/images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH">按商品類型顯示:<select name="goods_type" onchange="searchAttr(this.value)"><option value="0">所有商品類型</option><option value="1" selected="true">書</option><option value="2">音樂</option><option value="3">電影</option><option value="4">手機(jī)</option><option value="5">筆記本電腦</option><option value="6">數(shù)碼相機(jī)</option><option value="7">數(shù)碼攝像機(jī)</option><option value="8">化妝品</option><option value="9">精品手機(jī)</option><option value="10">我的商品</option></select></form> </div><form method="post" action="attribute.php?act=batch" name="listForm"> <div class="list-div" id="listDiv"><table cellpadding="3" cellspacing="1"><tbody><tr><th><input onclick="listTable.selectAll(this, "checkboxes[]")" type="checkbox">編號 </th><th>屬性名稱</th><th>商品類型</th><th>屬性值的錄入方式</th><th>可選值列表</th><th>排序</a></th><th>操作</th></tr><volist name="attrs" id="vo"><tr><td nowrap="true" valign="top"><span><input value="1" name="checkboxes[]" type="checkbox">1</span></td><td class="first-cell" nowrap="true" valign="top"><span onclick="listTable.edit(this, 'edit_attr_name', 1)">{$vo['attr_name']}</span></td><td nowrap="true" valign="top"><span>{$vo['type_id']}</span></td><td nowrap="true" valign="top"><span>{$vo['attr_input_type']}</span></td><td valign="top"><span></span></td><td align="right" nowrap="true" valign="top"><span onclick="listTable.edit(this, 'edit_sort_order', 1)">{$vo['sort_order']}</span></td><td align="center" nowrap="true" valign="top"><a href="?act=edit&attr_id=1" title="編輯"><img src="__ADMIN__/images/icon_edit.gif" border="0" height="16" width="16"></a><a href="javascript:;" onclick="removeRow(1)" title="移除"><img src="__ADMIN__/images/icon_drop.gif" border="0" height="16" width="16"></a></td></tr></volist></tbody></table><table cellpadding="4" cellspacing="0"><tbody><tr><td style="background-color: rgb(255, 255, 255);"><input type="submit" id="btnSubmit" value="刪除" class="button" disabled="true"></td><td align="right" style="background-color: rgb(255, 255, 255);"> <!-- $Id: page.htm 14216 2008-03-10 02:27:21Z testyang $ --><div id="turn-page">總計 <span id="totalRecords">12</span>個記錄分為 <span id="totalPages">2</span>頁當(dāng)前第 <span id="pageCurrent">1</span>頁,每頁 <input type="text" size="3" id="pageSize" value="10" onkeypress="return listTable.changePageSize(event)"><span id="page-link"><a href="javascript:listTable.gotoPageFirst()">第一頁</a><a href="javascript:listTable.gotoPagePrev()">上一頁</a><a href="javascript:listTable.gotoPageNext()">下一頁</a><a href="javascript:listTable.gotoPageLast()">最末頁</a><select id="gotoPage" onchange="listTable.gotoPage(this.value)"><option value="1">1</option><option value="2">2</option> </select></span></div> </td></tr></tbody></table> </div></form><div id="footer">版權(quán)所有 © 2014-2016 奪命雷公狗 - 技術(shù)總結(jié) - </div> </div></body> </html>?
這里弄好了,那么下一步就到控制器了,
?
public function index(){$type_id = I('id',0,'int');$condition['type_id'] = $type_id; //放進(jìn)數(shù)組里面主要是為了防止注入$attrs = M('attribute')->where($condition)->select();$this -> assign('attrs',$attrs);$this -> display();}?
?
這里完事了,看下效果
?
?
?
這里發(fā)現(xiàn)了寫問題這里有個商品類型,他是在cz_goods_type表里面的,我們要用到連表查詢,我們可以直接使用TP自帶的關(guān)聯(lián)模型來完成。
model里面關(guān)聯(lián)關(guān)聯(lián)代碼如下:
?
<?phpnamespace Admin\Model;use Think\Model\RelationModel;class AttributeModel extends RelationModel{//自動驗證規(guī)則protected $_validate = array(array('attr_name','require','屬性名稱不呢個為空'),);//定義關(guān)聯(lián)protected $_link = array('rel1' => array('mapping_type' => self::BELONGS_TO, 'class_name' => 'goods_type', //關(guān)聯(lián)的表名'foreign_key' => 'type_id', //外鍵'as_fields' => 'type_name',//獲取過來的字段),);}?
?
要注意上面的命名空間要改成use Think\Model\RelationModel;和繼承的時候要繼承RelationModel。
?
控制器下的代碼如下:
?
class AttributeController extends CommonController{public function index(){$type_id = I('id',0,'int');$condition['type_id'] = $type_id; //放進(jìn)數(shù)組里面主要是為了防止注入$attrs = D('attribute')->where($condition)->relation(true)->select();$this -> assign('attrs',$attrs);$this -> display();}?
在控制器里面一定要用D來調(diào)用里面的relation來調(diào)去關(guān)聯(lián)上,然后display()出來即可。。
?
下一步就是來決解分頁問題了:那么我們繼續(xù)改裝列表里面的代碼了:如下所示:
public function index(){$type_id = I('id',0,'int');$condition['type_id'] = $type_id; //放進(jìn)數(shù)組里面主要是為了防止注入$count = M('attribute')->where($condition)->count(); //統(tǒng)計表里面一共有多少條數(shù)據(jù)$page = new \Think\Page($count,15);$page -> rollPage =5; //分頁列表上顯示多少條$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');$page->setConfig('first','首頁');$page->setConfig('prev','上一頁');$page->setConfig('next','下一頁');$page->setConfig('last','尾頁');$pageHtml = $page -> show();$attrs = D('attribute')->where($condition)->page(I('get.p',1),$page->listRows)->relation(true)->select();$this -> assign('pageHtml',$pageHtml);$this -> assign('attrs',$attrs);$this -> display();}?
再來改寫下列表頁模版的信息:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SHOP 管理中心 - 類型管理 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="__ADMIN__/styles/general.css" rel="stylesheet" type="text/css" /> <link href="__ADMIN__/styles/main.css" rel="stylesheet" type="text/css" /> <style type="text/css"> .num{padding-left:10px; } .current{padding-left:10px;color:blue;font-weight:bold;font-size:16px; } </style> </head> <body><h1> <span class="action-span"><a href="__CONTROLLER__/add">新建商品類型</a></span> <span class="action-span1"><a href="index.php?act=main">SHOP 管理中心</a> </span><span id="search_id" class="action-span1"> - 商品類型 </span> <div style="clear:both"></div> </h1><form method="post" action="" name="listForm"> <!-- start goods type list --> <div class="list-div" id="listDiv"><table width="100%" cellpadding="3" cellspacing="1" id="listTable"><tbody><tr><th>商品類型名稱</th><th>屬性分組</th><th>屬性數(shù)</th><th>狀態(tài)</th><th>操作</th></tr><volist name="types" id="vo"><tr><td class="first-cell"><span onclick="javascript:listTable.edit(this, 'edit_type_name', 1)">{$vo['type_name']}</span></td><td></td><td align="right">12</td><td align="center"><img src="__ADMIN__/images/yes.gif"></td><td align="center"><a href="__MODULE__/attribute/index/id/{$vo['type_id']}" title="屬性列表">屬性列表</a> |<a href="__CONTROLLER__/edit/id/{$vo['type_id']}" title="編輯">編輯</a> |<a href="__CONTROLLER__/del/id/{$vo['type_id']}" onclick="javascript:return confirm('刪除商品類型將會清除該類型下的所有屬性。\n您確定要刪除選定的商品類型嗎?')" title="移除">移除</a></td></tr></volist><tr><td align="right" nowrap="true" colspan="6" style="background-color: rgb(255, 255, 255);"><!-- $Id: page.htm 14216 2008-03-10 02:27:21Z testyang $ --><div id="turn-page">{$pageHtml}</div></td></tr></tbody></table></div> <!-- end goods type list --> </form><div id="footer">版權(quán)所有 © 2014-2016 奪命雷公狗 - 技術(shù)總結(jié) - </div> </div></body> </html>?
下一步就是按照類型來顯示了他是通過js來實現(xiàn)的。
模版代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SHOP 管理中心 - 屬性管理 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="__ADMIN__/styles/general.css" rel="stylesheet" type="text/css" /> <link href="__ADMIN__/styles/main.css" rel="stylesheet" type="text/css" /> <style type="text/css"> .num{padding-left:10px; } .current{padding-left:10px;color:blue;font-weight:bold;font-size:16px; } </style> </head> <body><h1> <span class="action-span"><a href="index.php?p=admin&c=attribute&a=add">添加屬性</a></span> <span class="action-span1"><a href="index.php?act=main">SHOP 管理中心</a> </span><span id="search_id" class="action-span1"> - 商品屬性 </span> <div style="clear:both"></div> </h1><div class="form-div"><form action="" name="searchForm"><img src=" __ADMIN__/images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH">按商品類型顯示:<select name="goods_type" onchange="searchAttr(this.value)"><volist name="types" id="vo"><option value="{$vo['type_id']}"<if condition="$vo['type_id'] eq $type_id">selected="selected"</if>>{$vo['type_name']}</option></volist></select></form> </div><form method="post" action="attribute.php?act=batch" name="listForm"> <div class="list-div" id="listDiv"><table cellpadding="3" cellspacing="1"><tbody><tr><th><input onclick="listTable.selectAll(this, "checkboxes[]")" type="checkbox">編號 </th><th>屬性名稱</th><th>商品類型</th><th>屬性值的錄入方式</th><th>可選值列表</th><th>排序</a></th><th>操作</th></tr><volist name="attrs" id="vo"><tr><td nowrap="true" valign="top"><span><input value="1" name="checkboxes[]" type="checkbox">1</span></td><td class="first-cell" nowrap="true" valign="top"><span onclick="listTable.edit(this, 'edit_attr_name', 1)">{$vo['attr_name']}</span></td><td nowrap="true" valign="top"><span>{$vo['type_name']}</span></td><td nowrap="true" valign="top"><span>{$vo['attr_input_type']}</span></td><td valign="top"><span></span></td><td align="right" nowrap="true" valign="top"><span onclick="listTable.edit(this, 'edit_sort_order', 1)">{$vo['sort_order']}</span></td><td align="center" nowrap="true" valign="top"><a href="?act=edit&attr_id=1" title="編輯"><img src="__ADMIN__/images/icon_edit.gif" border="0" height="16" width="16"></a><a href="javascript:;" onclick="removeRow(1)" title="移除"><img src="__ADMIN__/images/icon_drop.gif" border="0" height="16" width="16"></a></td></tr></volist></tbody></table><table cellpadding="4" cellspacing="0"><tbody><tr><td style="background-color: rgb(255, 255, 255);"><input type="submit" id="btnSubmit" value="刪除" class="button" disabled="true"></td><td align="right" style="background-color: rgb(255, 255, 255);"> <!-- $Id: page.htm 14216 2008-03-10 02:27:21Z testyang $ --><div id="turn-page">{$pageHtml}</div> </td></tr></tbody></table> </div></form><div id="footer">版權(quán)所有 © 2014-2016 奪命雷公狗 - 技術(shù)總結(jié) - </div> </div><script>function searchAttr(type_id){window.location.href="__SLEF__/id/"+type_id;}</script> </body> </html>?
attribute控制器代碼如下:
?
public function index(){$type_id = I('id',0,'int');$condition['type_id'] = $type_id; //放進(jìn)數(shù)組里面主要是為了防止注入$count = M('attribute')->where($condition)->count(); //統(tǒng)計表里面一共有多少條數(shù)據(jù)$page = new \Think\Page($count,2);$page -> rollPage =5; //分頁列表上顯示多少條$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');$page->setConfig('first','首頁');$page->setConfig('prev','上一頁');$page->setConfig('next','下一頁');$page->setConfig('last','尾頁');$pageHtml = $page -> show();$attrs = D('attribute')->where($condition)->page(I('get.p',1),$page->listRows)->relation(true)->select();$this -> assign('pageHtml',$pageHtml);$this -> assign('attrs',$attrs);//獲取所有的商品類型,并分配到模版$types = M('goods_type')->select();$this -> assign('types',$types);$this -> assign('type_id',$type_id);$this -> display();}?
顯示做好了,編輯其實和添加是差不多的,廢話不多說,開工,首先來弄個edit.html的模版,代碼如下所示:
?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SHOP 管理中心 - 屬性管理 </title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="__ADMIN__/styles/general.css" rel="stylesheet" type="text/css" /> <link href="__ADMIN__/styles/main.css" rel="stylesheet" type="text/css" /> </head> <body><h1> <span class="action-span"><a href="index.php?p=admin&c=attribute&a=index">商品屬性</a></span> <span class="action-span1"><a href="index.php?act=main">SHOP 管理中心</a> </span><span id="search_id" class="action-span1"> - 編輯屬性 </span> <div style="clear:both"></div> </h1><div class="main-div"><form action="" method="post" name="theForm" onsubmit="return validate();"><table width="100%" id="general-table"><tbody><tr><td class="label">屬性名稱:</td><td><input type="text" name="attr_name" value="{$attr['attr_name']}" size="30"><span class="require-field">*</span> </td></tr><tr><td class="label">所屬商品類型:</td><td><select name="type_id" onchange="onChangeGoodsType(this.value)"><volist name="types" id="vo"><if condition="$vo['type_id'] eq $attr['type_id']"><option value="{$attr['type_id']}" selected="selected">{$attr['type_name']}</option><else /><option value="{$vo['type_id']}">{$vo['type_name']}</option></if></volist></select> <span class="require-field">*</span> </td></tr><tr id="attrGroups" style="display: none;"><td class="label">屬性分組:</td><td><select name="attr_group"></select></td></tr><tr><td class="label"><a href="javascript:showNotice('noticeAttrType');" title="點擊此處查看提示信息"><img src="__ADMIN__/images/notice.gif" width="16" height="16" border="0" alt="點擊此處查看提示信息"></a>屬性是否可選</td><td><label><input type="radio" name="attr_type" value="0" <if condition="$attr['attr_type'] eq 0">checked="checked"</if>> 唯一屬性</label><label><input type="radio" name="attr_type" value="1"<if condition="$attr['attr_type'] eq 1">checked="checked"</if>> 單選屬性</label><label><input type="radio" name="attr_type" value="2"<if condition="$attr['attr_type'] eq 2">checked="checked"</if>> 復(fù)選屬性</label><br><span class="notice-span" style="display:block" id="noticeAttrType">選擇"單選/復(fù)選屬性"時,可以對商品該屬性設(shè)置多個值,同時還能對不同屬性值指定不同的價格加價,用戶購買商品時需要選定具體的屬性值。選擇"唯一屬性"時,商品的該屬性值只能設(shè)置一個值,用戶只能查看該值。</span></td></tr><tr><td class="label">該屬性值的錄入方式:</td><td><label><input type="radio" name="attr_input_type" value="0" <if condition="$attr['attr_input_type'] eq 0">checked="checked"</if>>手工錄入</label> <label><input type="radio" name="attr_input_type" value="1" <if condition="$attr['attr_input_type'] eq 1">checked="checked"</if>>從下面的列表中選擇(一行代表一個可選值)</label> <label><input type="radio" name="attr_input_type" value="2" <if condition="$attr['attr_input_type'] eq 2">checked="checked"</if>>多行文本框</label> </td></tr><tr><td class="label">可選值列表:</td><td><textarea name="attr_value" cols="30" rows="5" >{$attr['attr_value']}</textarea></td></tr><tr><td colspan="2"><div class="button-div"><input type="submit" value=" 確定 " class="button"><input type="reset" value=" 重置 " class="button"></div></td></tr></tbody></table><input type="hidden" name="act" value="update"><input type="hidden" name="attr_id" value="{$attr['attr_id']}"></form> </div><div id="footer">版權(quán)所有 © 2014-2016 奪命雷公狗 - 技術(shù)總結(jié) - </div> </div> <script type="text/javascript"> /*** 點擊類型按鈕時切換選項的禁用狀態(tài)*/ function radioClicked(n) {document.forms['theForm'].elements["attr_value"].disabled = n > 0 ? false : true; }</script> </body> </html>?
?
?
完事后直接開始寫控制器,代碼如下所示:
?
public function edit(){$attr_id = I('id',0,'int');if(IS_POST){//入庫$data['attr_id'] = I('attr_id');$data['attr_name'] = I('attr_name');$data['type_id'] = I('type_id');$data['attr_type'] = I('attr_type');$data['attr_input_type'] = I('attr_input_type');$data['attr_value'] = I('attr_value');$attrModel = D('attribute');$condition = $data['type_id'];//dump($data);die;if($attrModel->create($data)){//通過驗證if($attrModel->save()){$this -> success('修改屬性成功',U('index'),1);}else{$this -> error('修改屬性失敗');}}else{//沒通過驗證,提示錯誤信息$this -> error($attrModel->getError());}return;}//獲取所有的商品類型$attr = D('attribute')->where($condition)->relation(true)->find($attr_id);$this -> assign('attr',$attr);$types = M('goods_type')->select();$this -> assign('types',$types);$this -> display();}?
?
?
那么下一步就開始寫刪除了,其實刪除也是最容易的一步了,代碼如下所示:
?
public function del(){$id = I('id',0,'int');if (M('attribute')->delete($id)) {$this->success('刪除成功');} else {$this->error('刪除失敗');}}?
?
最終控制器代碼總結(jié)
<?phpnamespace Admin\Controller;use Think\Controller;class AttributeController extends CommonController{public function index(){$type_id = I('id',0,'int');$condition['type_id'] = $type_id; //放進(jìn)數(shù)組里面主要是為了防止注入$count = M('attribute')->where($condition)->count(); //統(tǒng)計表里面一共有多少條數(shù)據(jù)$page = new \Think\Page($count,2);$page -> rollPage =5; //分頁列表上顯示多少條$page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');$page->setConfig('first','首頁');$page->setConfig('prev','上一頁');$page->setConfig('next','下一頁');$page->setConfig('last','尾頁');$pageHtml = $page -> show();$attrs = D('attribute')->where($condition)->page(I('get.p',1),$page->listRows)->relation(true)->select();$this -> assign('pageHtml',$pageHtml);$this -> assign('attrs',$attrs);//獲取所有的商品類型,并分配到模版$types = M('goods_type')->select();$this -> assign('types',$types);$this -> assign('type_id',$type_id);$this -> display();}public function add(){if(IS_POST){//入庫$data['attr_name'] = I('attr_name');$data['type_id'] = I('type_id');$data['attr_type'] = I('attr_type');$data['attr_input_type'] = I('attr_input_type');$data['attr_value'] = I('attr_value');$attrModel = D('attribute');if($attrModel->create($data)){//通過驗證if($attrModel->add()){$this -> success('添加屬性成功',U('index'),1);}else{$this -> error('添加屬性失敗');}}else{//沒通過驗證,提示錯誤信息$this -> error($attrModel->getError());}return;}//獲取所有的商品類型$types = M('goods_type')->select();$this -> assign('types',$types);$this -> display();}public function edit(){$attr_id = I('id',0,'int');if(IS_POST){//入庫$data['attr_id'] = I('attr_id');$data['attr_name'] = I('attr_name');$data['type_id'] = I('type_id');$data['attr_type'] = I('attr_type');$data['attr_input_type'] = I('attr_input_type');$data['attr_value'] = I('attr_value');$condition = $data['type_id'];//dump($data);die;$attrModel = D('attribute');if($attrModel->create($data)){//通過驗證if($attrModel->save()){$this -> success('修改屬性成功',U('index'),1);}else{$this -> error('修改屬性失敗');}}else{//沒通過驗證,提示錯誤信息$this -> error($attrModel->getError());}return;}//獲取所有的商品類型$attr = D('attribute')->where($condition)->relation(true)->find($attr_id);$this -> assign('attr',$attr);$types = M('goods_type')->select();//dump($types);die;$this -> assign('types',$types);$this -> display();}public function del(){$id = I('id',0,'int');if (M('attribute')->delete($id)) {$this->success('刪除成功');} else {$this->error('刪除失敗');}}}?
轉(zhuǎn)載于:https://www.cnblogs.com/leigood/p/4947599.html
總結(jié)
以上是生活随笔為你收集整理的夺命雷公狗ThinkPHP项目之----商城10商品属性管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDC/ISP业务申请需要公司具备哪些资
- 下一篇: 菜鸟学习笔记----BGP基本原理