PHP.30-TP Framework Mall Application Example-Backstage 8-Product album-add

Source: Internet
Author: User
Tags php script

Product album "is another photo of the product"

Add album requirements:

Each picture generates three thumbnails {50*50, 350*350, 650*650}

1, build table p39_goods_pic{id,pic,sm_pic,mid_pic,big_pic,goods_id

drop tableifexists p39_goods_pic;create table p39_goods_pic (id mediumint unsigned notNULLAuto_increment comment ' Id ',pic varchar () notNULL default"Comment ' original image ',sm_pic varchar () notNULL default' Comment ' little picture ',mid_pic varchar () notNULL default"Comment ' in the picture ',big_pic varchar () notNULL default"Comment ' big picture ',goods_id mediumint unsigned notNULLComment ' Product ID ',PrimaryKey(ID),Keygoods_id (goods_id)) engine=innodbdefaultCharset=utf8 Comment ' merchandise album ';
P39_goods_pic

2, in the product album to add a form to create a "add a" button, through the JS implementation of a single click to add a text field

// Add a photo        $ ("#btn_add_pic"). Click (function() {            varfile = ' <li><input type= ') File "Name=" pic[] "/></li>";            $ ("#ul_pic_list"). Append (file);
JS Demo

3 . Add the code to handle the product album in the product model Goodsmodel.class.php/_after_inser ()

Ideas :

1. Upload a picture using the already encapsulated function Uploadone (name, storage directory, [thumbnail size array])

functionUploadone ($imgName,$dirName,$thumb=Array())    {        //Upload logo        if(isset($_files[$imgName]) &&$_files[$imgName[' error '] = = 0)        {            $ic= C (' Image_config '); $upload=New\think\upload (Array(                ' RootPath ' =$ic[' RootPath '], ' maxSize ' =$ic[' MaxSize '], ' exts ' =$ic[' Exts '],            ));//instantiating an upload class            $upload->savepath =$dirName. ‘/‘;//the name of the picture level two directory//upload file//upload the name of a picture to upload, or you will be the form of all the pictures are processed, and then think of other pictures when you can not find the picture            $info=$upload->upload (Array($imgName=$_files[$imgName])); if(!$info)            {                return Array(                    ' OK ' = 0, ' ERROR ' =$upload->geterror (),                ); }            Else            {                $ret[' OK '] = 1; $ret[' Images '] [0] =$logoName=$info[$imgName[' Savepath '].$info[$imgName[' Savename ']; //determine whether to generate thumbnails [$thumb array storage size]                if($thumb)                {                    $image=New\think\image (); //Cycle generated thumbnails                    foreach($thumb  as $k=$v)                    {                        $ret[' Images '] [$k+1] =$info[$imgName[' Savepath ']. ' Thumb_ '.$k.‘ _‘ .$info[$imgName[' Savename ']; //Open the picture you want to work with                        $image->open ($ic[' RootPath '].$logoName); $image->thumb ($v[0],$v[1]) ->save ($ic[' RootPath '].$ret[' Images '] [$k+1]); }                }                return $ret; }        }    }
Uploadone ()

2, loop each picture call Uploadone one processing, processing finished insert into the album table

The structure of the file data is observed first, as follows:

Two kinds of documents, one is commodity logo{logo}; but a product album Pic{pic}, one-dimensional array logo; two-dimensional array pic

Since pic is a two-dimensional array, Uploaone can only handle one-dimensional arrays, such as the logo of the array structure, so to convert to a one-dimensional array $pics[]

Then do the picture processing

//Hook Method _after_insert: Add operation after successful execution        protected function_after_insert ($data,$option)        {            /********** Product album processing ********/            $pics=Array(); //var_dump ($_files[' pic ' [' name ']);d ie (); Two-dimensional arrays into one dimension            foreach($_files[' Pic '] [' Name '] as $k=$v)            {                $pics[] =Array(                    ' Name ' + =$v, ' type ' = =$_files[' Pic '] [' type '] [$k], ' tmp_name ' =$_files[' Pic '] [' Tmp_name '] [$k], ' ERROR ' =$_files[' Pic '] [' ERROR '] [$k], ' size ' =$_files[' Pic '] [' Size '] [$k],                ); }            //var_dump ($pics);d ie ();            $_files=$pics;//assign the handled array to $_files, because the Uploadone function takes the picture in $_files            $gpModel= M (' Goods_pic '); //Loop each upload            foreach($pics  as $k=$v)            {                if($v[' error '] = = 0)                {                    $ret= Uploadone ($k, ' Goods ',Array(                        Array(650, 650),Array(350, 350),Array(50, 50),                    )); //var_dump ($ret);d ie ();                    if($ret[' OK '] = = 1)                    {                        $gpModel->add (Array(                            ' Pic ' =$ret[' Images '] [0], ' big_pic ' =$ret[' Images '] [1], ' mid_pic ' =$ret[' Images '] [2], ' sm_pic ' =$ret[' Images '] [3], ' goods_id ' =$data[' ID '],                        )); }                }            }            /********** member Price processing ********/            $MP= I (' Post.member_price ');//receive the member price data submitted by post            $mpModel= D (' Member_price '); foreach($MP  as $k=$v)            {                $_v= (float)$v;//force to float to avoid error data such as inserting characters//set member price >0 INSERT INTO table                if($_v> 0)                {                    $mpModel->add (Array(                        ' Price ' =$_v, ' level_id ' =$k,//level ID' goods_id ' =$data[' ID '],                    )); }            }        }
_after_insert

Note: When bulk uploads, Uploaone cannot handle two-dimensional arrays, it must be turned into a one-dimensional array and then inserted into the database with a foreach Loop

Problem:

1, upload the number of pictures in the form seems to be unlimited, but in the php.ini limit post_max_size, so in the product add a form also need to do some optimization, limit the size and number of users to upload pictures

2, PHP script default execution time is 30 seconds, may cause the data processing is not complete. Can be set up using Set_time_limit () before data processing in the form, as required

PHP.30-TP Framework Mall Application Example-Backstage 8-Product album-add

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.