tp商城(5)

<?php
namespace Admin\Controller;

use \Think\Controller;
class GoodsController extends Controller {
    protected $model = null;

    public function __construct() {
        parent::__construct();

        $this->model = D('Home/Goods');
    }

_______________________________________________________________________________

<form enctype="multipart/form-data" action="" method="post" name="theForm" >

<tr>
  <td class="label">上传商品图片:</td>
  <td>
    <input type="file" name="goods_img" size="35" />
  </td>
</tr>

———————————————————————————————————————————————————————————————————————————————



    public function add() {
        if(!IS_POST) {
            $this->display();
        } else {
            //echo $this->model->add($_POST) ? 'OK' : 'fail';

            // create()会把参数, 赋给内部的data,(如果没传参,则把$_POST赋给内部data)
            // create()的赋值过程,会检测数据的合法性, 自动验证将会发挥作用
            if(!$this->model->create()) {
                echo $this->model->getError();
            } else {
                if(isset($_FILES['goods_img'])) {  //判断是不是有图片先
                    $up = new \Think\Upload();
                    $up->exts = array('jpeg' , 'jpg' , 'png' , 'gif');
                    $up->rootPath = APP_PATH;
                    $up->savePath = '/Public/images/';

                    $info = $up->upload();
                    if($info) {
                    //savename 默认unique 自动生成一个//
                        $this->model->ori_img = $info['goods_img']['savepath']  . $info['goods_img']['savename'];
                        $this->model->goods_img = $this->model->ori_img . '230X230.png';
                        $this->model->thumb_img = $this->model->ori_img . '100X100.png';

                        //作用将图片的地址保存在数据库里 相对应的字段上
                        //以便将来取出数据使用

-————————————————————————————————————————————————————————————————————————————
  缩   略   图  //

                        // 缩略
                        $img = new \Think\Image();
//先打开图片再进行操作    $img->open(APP_PATH . $this->model->ori_img);
//thumb 缩放 前两个参数 宽高  最后一个参数 缩略样式
// 缩略样式    \Think\Image::IMAGE_THUMB_FILLED  两端留白
                        $img->thumb(230,230,\Think\Image::IMAGE_THUMB_FILLED);
                        $img->save(APP_PATH . $this->model->goods_img);

                        $img->thumb(100,100,\Think\Image::IMAGE_THUMB_FILLED);
                        $img->save(APP_PATH . $this->model->thumb_img);
                    }
                }
                $this->model->goods_desc = $_POST['goods_desc'];
                echo $this->model->add() ? 'OK' : 'fail';
            }
        }
    }



/分  页  类  ///
    public function lists() {

    //数一下 每页有多少条?
        $cnt = $this->model->count('*'); // select count(*) from goods;

              //自己写一个分页类?继承一下放在那里??  libery /org /A/B.class.php下
        $page = new \Org\A\B($cnt,2); // 分布类初始化成功
        $pages = $page->show();  计算书分页代码了

        $this->model->field('goods_id,goods_name,goods_sn,shop_price,is_best,is_new,is_hot');
        $this->model->order('goods_id desc')->limit($page->firstRow,$page->listRows);
          //不同的页码的限制limit 页数 条数不同  limit  $page->firstRow  $page->kistRows
          //自己写一个分页类?继承一下放在那里??  libery /org /XX/XX下

        $goods = $this->model->select();

        $this->assign('pages' , $pages);
        $this->assign('goods' , $goods);
        $this->display();
    }

    public function del() {
        $this->model->delete(I('get.goods_id'));

        $this->redirect('Admin/Goods/lists');
    }
}

——————————————————————————————————————————————————————————————————————————————
------------------------------------------------------------------------------
验证码
$cf = array(
  'fontSize'=>  30 ,
  'useNoise'=>false ,
  'length'=>5,
);

$vfy = nw Think\Verify($cf);
$vfy->entry();

------------------------------------------------------------------------------

$vfy = new Think\Verify();
$vfy->length = 5;
$vfy->fontSize = 20;
$vfy -> useNoise =false ;
$vfy->entry();




-------------------------------------------------------------------------------


















$up = new Think\Upload();

$up->exts=array();
$up-> rootPath = APP_PATH;
$up->savePath = '/Public/Img/';
$info = $up->save();

if($info){
    $this->model->org_img = $info['img']['rootPath'] . $info['img']['savename'];
      $this->model->goods_img =$this->model->org_img . '100X100.png'
      $this->model->thumb_img =$this->model->org_img . '300X300.png'

    //缩略图

    $img = new Think\Image();
    $img->open(APP_PATH . $this->model->Org_img);

    $img->thumb(100, 100 , ....);
    $img->save($this->model->thumb_img );

    $img ->thumb (300,300, ....);
    $img->save($this ->model ->gods_img);

}

$this->model ->add();











_______________________________________________________________________________

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

银色种子

打赏 >100 请留言,并私信

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值