在destoon b2b网站系统当中增加批量导入商品功能,将当当方便系统数据填充,新网站当中尤为必要
$menus = array (1.添加菜单\admin\data.inc.php和C:\wamp\www\dev_lebaixiao\admin\database.inc.php );
2.菜单读取csvmall配置文件路径为:
\admin\csvmall.inc.php
<?php
require DT_ROOT.'/include/module.func.php'; $menus = array ( array('数据库备份', '?file=database'), array('数据库恢复', '?file=database&action=import'), array('字符替换', '?file=database&action=replace'), array('执行SQL', '?file=database&action=execute'), array('显示进程', '?file=database&action=process'), array('数据互转', '?file=database&action=move'), array('数据导入', '?file=data'), array('产品批量导入', '?file=csvmall'), ); include tpl('csvimport'); if($action=='import'){ //导入CSV $filename = $_FILES['file']['tmp_name']; if (empty ($filename)) { msg('请选择要导入的CSV文件!'); exit; } $handle = fopen($filename, 'r'); $result = input_csv($handle); //解析csv $len_result = count($result); if($len_result==0){ msg('没有任何数据!'); exit; } $csvdir='file/uploadcsv/'.date("Ymd").'/'; //创建图片文件夹 if (!file_exists($csvdir)){ mkdir(DT_ROOT.'/'.$csvdir,0777) or msg('图片文件夹创建失败'); } iconv('gb2312', 'utf-8', $result[$i][1]); for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值 $itemid = $result[$i][0]; $catid = $result[$i][1]; $areaid = $result[$i][2]; $level = $result[$i][3]; $title = iconv('gb2312', 'utf-8', $result[$i][4]); $brand = iconv('gb2312', 'utf-8', $result[$i][5]); $price = $result[$i][6]; $amount = $result[$i][7]; $keyword = iconv('gb2312', 'utf-8', $result[$i][8]); $orders = $result[$i][9]; $sales = $result[$i][10]; $thumb = iconv('gb2312', 'utf-8', DT_PATH.$csvdir.$result[$i][11]); $thumb1 = iconv('gb2312', 'utf-8', DT_PATH.$csvdir.$result[$i][12]); $thumb2 = iconv('gb2312', 'utf-8', DT_PATH.$csvdir.$result[$i][13]); $username = $result[$i][14]; $groupid = $result[$i][15]; $company = iconv('gb2312', 'utf-8', $result[$i][16]); $validated = $result[$i][17]; $truename = iconv('gb2312', 'utf-8', $result[$i][18]); $telephone = $result[$i][19]; $address = iconv('gb2312', 'utf-8', $result[$i][20]); $qq = $result[$i][21]; $editor = $result[$i][22]; $edittime = strtotime($result[$i][24]); $editdate = $result[$i][24]; $addtime = strtotime($result[$i][26]); $adddate = $result[$i][26]; $status = $result[$i][27]; $linkurl= 'show.php?itemid='.$result[$i][28]; $mallintroduce = iconv('gb2312', 'utf-8', $result[$i][29]); $data_values .= "('$itemid','$catid','$areaid','$level','$title','$brand','$price','$amount','$keyword','$orders','$sales','$thumb','$thumb1','$thumb2','$username','$groupid','$company','$validated','$truename','$telephone','$address','$qq','$editor','$edittime','$editdate','$addtime','$adddate','$status','$linkurl','$mallintroduce'),"; $datacontent_values .= "('$itemid','$mallintroduce'),"; } $data_values = substr($data_values,0,-1); //去掉最后一个逗号 $datacontent_values = substr($datacontent_values,0,-1); //去掉最后一个逗号 fclose($handle); //关闭指针 $cvsreslut=$db->query("INSERT INTO `{$DT_PRE}mall` (`itemid`,`catid`,`areaid`,`level`,`title`,`brand`,`price`,`amount`,`keyword`,`orders`,`sales`,`thumb`,`thumb1`,`thumb2`,`username`,`groupid`,`company`,`validated`,`truename`,`telephone`,`address`,`qq`,`editor`,`edittime`,`editdate`,`addtime`,`adddate`,`status`,`linkurl`,`mallintroduce`) values $data_values"); $cvsreslutcontent=$db->query("INSERT INTO `{$DT_PRE}mall_data` (`itemid`, `content`) values $datacontent_values"); if(false!==$cvsreslut){ msg('导入成功!'); }else{ msg('导入失败!'); } } ?>
3.配置文件中
include tpl('csvimport');包含模块文件
\admin\template\csvimport.tpl.php
<?php
defined('IN_DESTOON') or exit('Access Denied'); include tpl('header'); show_menu($menus); ?> <div class="tt">使用CSV文件批量导入产品</div> <table cellpadding="2" cellspacing="1" class="tb"> <tr class="demo"> <td>请选择要导入的CSV文件: <form id="addform" action="?file=csvmall&action=import" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" class="btn" value="导入CSV"> </form> </td> </tr> <tr> <td> </td> </tr> </table> <script type="text/javascript">Menuon(7);</script> <?php include tpl('footer');?>
4.\include\module.func.php方法
function input_csv($handle) {
setlocale(LC_ALL, 'zh_CN'); //linux下解决中文乱码或不全问题
(责任编辑:好模板)$out = array (); $n = 0; while ($data = fgetcsv($handle, 10000)) { $num = count($data); for ($i = 0; $i < $num; $i++) { $out[$n][$i] = $data[$i]; } $n++; } return $out; } function export_csv($filename,$data) { header("Content-type:text/csv"); header("Content-Disposition:attachment;filename=".$filename); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public'); echo $data; } |