分頁以及模糊查詢
//顯示商品列表 public function list1(){ //查詢商品分類 $cat = M('category')->select(); //註冊一個變量 $this->assign("cat_list",$cat); //查詢商品品牌 $brand = M('brand')->select(); //註冊一個變量 $this->assign("brand_list",$brand); //表頭 $arr = array('編號','商品名稱','貨號','價格','圖片','縮略圖','新品','精品','熱銷','庫存','操做'); //註冊一個表頭數據 $this->assign("bt",$arr); //查詢數據庫 $m = M('goods')->select(); //模糊查詢 //獲取分類數據 $cat_id = $_GET['cat_id']; //獲取品牌數據 $brand_id = $_GET['brand_id']; //獲取推薦數據 $intro_type = $_GET['intro_type']; //獲取上架數據 $is_on_sale = $_GET['is_on_sale']; //獲取關鍵字數據 $keyword = $_GET['keyword']; //判斷分類數據是否存在 if(!empty($cat_id)){ //若是存在,把條件追加到數組裏 $where['goods_category_id'] =$cat_id; } //判斷品牌數據是否存在 if(!empty($brand_id)){ $where['goods_brand_id'] =$brand_id; } //判斷推薦數據是否存在 if(!empty($intro_type)){ //若是存在,把條件追加到數組裏(等於1是由於表裏面三個字段是經過1和0來區分是否被選中,html頁面他們三個value是字段名) $where["$intro_type"] = '1'; } if(!empty($keyword)){ // $list = $m->where("goods_name like '%$keyword%'")->select(); $where['goods_name'] = array('like','%'.$keyword.'%'); } // 分頁 //查詢數據庫 $model = M('goods'); //總記錄數 where($where) 這是把上面模糊查詢的結果放到這裏面 $recordcount=$model->where($where)->count(); $page=new \Think\Page($recordcount,3);//new一個方法 $page->lastSuffix=false; //最後一頁是否顯示總頁數 $page->rollPage=3; //分頁欄每頁顯示的頁數 $page->setConfig('prev', '【上一頁】'); $page->setConfig('next', '【下一頁】'); $page->setConfig('first', '【首頁】'); $page->setConfig('last', '【末頁】'); $page->setConfig('theme', '共 %TOTAL_ROW% 條記錄,當前是 %NOW_PAGE%/%TOTAL_PAGE% %FIRST% %UP_PAGE% %DOWN_PAGE% %END%'); $startno=$page->firstRow; //起始行數 $pagesize=$page->listRows; //頁面大小 $list=$model->where($where)->limit("$startno,$pagesize")->select(); $pagestr=$page->show(); //組裝分頁字符串 $this->assign('list',$list);//註冊查詢變量 $this->assign('pagestr',$pagestr);//註冊分頁變量 $this->display();//調用這個模板 }
驗證碼
html頁面調用驗證碼:
<tr> <td>驗證碼:</td> <td> <input type="text" name="captcha" class="capital" /> </td> </tr> <tr> <td colspan="2" align="right"> <img src="__CONTROLLER__/VerifyImg" width="145px" height="30px" alt="" onClick="this.src='__CONTROLLER__/VerifyImg/'+Math.random()" /> </td> </tr>
PHP頁面調用方法html
//建立驗證碼方法 public function VerifyImg(){ //建立驗證碼的配置文件(框架自帶的,能夠直接從\Think\Verify拿過來用) $config = array( 'length' => 4, // 驗證碼位數 'useCurve' => false, // 是否畫混淆曲線 'useNoise' => false, // 是否添加雜點 ); //實例化方法 $Verify = new \Think\Verify($config); //建立驗證碼 $Verify->entry(); }
判斷驗證碼、帳號密碼是否正確sql
//判斷用戶登陸 public function dlyz(){ $this->utf(); //獲取用戶輸入的驗證碼 $yzm = $_POST['captcha']; //new一個方法(找到Verify()) $img = new \Think\Verify(); //判斷,若是驗證碼正確,在判斷用戶名密碼(check($yzm)框架自帶的驗證方法 if($img->check($yzm)){ //找到數據庫的表 $admin = M('admin'); //I方式接收 data括號裏寫字段名 I括號裏寫傳輸方式和name名,相似$_POST['admin_user']; $data['admin_user'] = I('post.username'); $data['admin_psd'] = I('post.password'); $mm = $admin->where($data)->find(); //判斷,若是帳號密碼匹配 if($mm){ //跳轉本控制器的Index方法 $_SESSION['qx'] = I('post.username'); $this->redirect("Index/index"); // $this->display(); }else{ //不然提示帳號密碼錯誤 $this->error('用戶名或密碼錯誤',U("login/login"),2); } }else{ //若是驗證碼錯誤,提示驗證碼錯誤 $this->error('驗證碼錯誤',U("login/login"),2); } }
上傳圖片及生成縮略圖
//添加商品sql語句調用商品模板 public function add(){ //查詢商品品牌表 $brand = M('brand')->select(); //註冊一個變量給html頁面 $this->assign('brand',$brand); //查詢商品分類 $category = M('category')->select(); $this->assign('category',$category); //判斷前面是否post傳值 if(IS_POST){ //查表 $goods = M('goods'); //create()方法接收前面全部的值 $data = $goods->create(); //判斷goods_img(字段名)的error是否爲0(就是判斷上傳圖片是否成功) if($_FILES['goods_img']['error']==0){ //圖片存放路徑 $config = array( //後臺路徑 'rootPath' => "./Applicationxm/Admin/Public/img/", ); //new一個框架的方法 $upload = new \Think\Upload($config); $info = $upload->uploadOne($_FILES['goods_img']); //大圖的存放路徑 $data['goods_big_img'] = $info['savepath'].$info['savename']; //縮略圖 //new一個方法 $img = new \Think\Image(); //打開圖片 $big_img = $upload->rootPath.$data['goods_big_img']; $img->open($big_img); //生成縮略圖 $img->thumb(200,300,2); //保存 $small_img = $upload->rootPath.$info['savepath'].'small_'.$info['savename']; $img->save($small_img); $data['goods_small_img'] = $info['savepath'].'small_'.$info['savename']; //給貨號加時間戳 $data['goods_sn'] = time(); $str = '添加失敗'; if($goods->add($data)){ $str = '添加成功'; } //跳轉頁面 $this->redirect('Menu/list1',array(),2,$str); } } //調用模板 $this->display(); }