/** * 首页 * */ public function actionIndex() { $model = new Goods(); $criteria = new CDbCriteria(); $condition = "type = " . $this->_type; $goods_name = trim($this->_request->getParam('goods_name')); $catalogId = intval($this->_request->getParam('catalogId')); $goods_name && ($condition .= ' AND goods_name LIKE \'%' . $goods_name . '%\''); $catalogId && ($condition .= ' AND catalog_id= ' . $catalogId); $criteria->condition = $condition; $criteria->order = 't.id DESC'; $criteria->with = array('catalog'); $count = $model->count($criteria); $pages = new CPagination($count); $pages->pageSize = 10; //根据goods_name,catelogId查询 $pageParams = $this->buildCondition($_GET, array('goods_name', 'catalogId')); $pages->params = is_array($pageParams) ? $pageParams : array(); $criteria->limit = $pages->pageSize; $criteria->offset = $pages->currentPage * $pages->pageSize; $result = $model->findAll($criteria); //推荐位 $recom_list = RecommendPosition::model()->findAll('type=:type', array(':type' => $this->_type), array('order' => 'id')); $this->render('index', array('datalist' => $result, 'pagebar' => $pages, 'recom_list' => $recom_list)); }
if (!$result) { flash(); } } if (isset($_POST['del']) && !empty($_POST['id'])) { $result = $goods->del($_POST['id']); } if (isset($_GET['do'])) { $do = trim($_GET['do']); if (!empty($_GET['id'])) { $id = intval($_GET['id']); } if ($do == "del" && !empty($id)) { $result = $goods->del($_GET['id']); } if ($do == "edit") { if (!empty($id)) { $result = $goods->read("*", $id); setvar("item", $result); } $tpl_file = "goods.edit"; template($tpl_file); exit; } } $amount = $goods->findCount(); $page->setPagenav($amount); $result = $goods->findAll("*", null, $conditions, "id desc", $page->firstcount, $page->displaypg); setvar("Items", $result); setvar("ByPages", $page->pagenav); template($tpl_file);
function ceshi() { $model = new Goods(); //$info = $model -> findAllByPk(10); //$info = $model -> findAllByPk(1,4,9); /////////////////////////////////////////// //findAll($condition ,$param); //$condition就是sql语句的where条件 //$infos = $model -> findAll("goods_name like'诺%' and goods_price > 500"); ////////////////////////////////////////// //为了避免sql注入,使用占位符填充 //$infos = $model -> findAll("goods_name like :name and goods_price > :price",array(":name"=> '诺%',":price"=>500) ); //$model -> goods_name = 'sanming'; //$model -> save(); //有时候我们查询信息 //想要查询的"字段" select //想要查询的"条件" condition //想要查询的"排序" order //想要查询的"分组" group //想要查询的"限制" limit //想要查询的"偏移量" offset //$infos = $model -> findAll(array( // 'select'=>'goods_name,goods_price', // 'condition'=>"goods_name like '诺%'", // 'order'=>'goods_price desc', // 'limit'=>3, // 'offset'=>2, //)); /////////////////////////////////////////// //通过criteria实现信息的查询 $criteria = new CDbCriteria(); $criteria->select = 'goods_name,goods_price'; $criteria->condition = "goods_name like '诺%'"; $criteria->limit = 3; $criteria->order = 'goods_price desc'; $infos = $model->findAll($criteria); }