找回密码
 立即注册
即日起,论坛关闭新用户注册和登录,论坛相关的贴子保留查阅和下载。获得授权后,有技术问题可联系微信 13199509559 一对一解决。 2024-3-12
查看: 14688|回复: 3

[经验分享] 修改产品属性功能,后台添加excel表上传数据功能

3

主题

3

主题

3

主题

注册会员

Rank: 2

积分
89
meiyidian 发表于 2017-12-8 06:45:11 | 显示全部楼层 |阅读模式
提交问题
提示:: -
运行环境: 本地测试
运行服务器: win
PHP版本: php5.6
OurPHP版本: ourphp1.7.3
编辑器: 专用的PHP编辑器
程序存放目录: 根目录
就是给程序添加了一个excel表导入数据库功能,利用了Phpexcel这个东西。测试通过,修改了下产品属性这里的显示,把选择框,改成了输入框了。有需要的留言吧。不多就不放全部源码了。思路是下载phpexcel放到后台的插件中,\client\manage\plugs\phpexcel,
仿照后台的执行sql语句页面,制作了相应的our_excle.php和模板our_excel.html

提交页面和其他的按照https://www.cnblogs.com/zxf100/archive/2017/06/07/6957532.html中制作的,里面那个判断文件上传的代码是错误的,错误代码如下

  1. //判断是否选择了要上传的表格
  2. if (empty($_POST['myfile'])) {
  3.     echo "<script>alert(您未选择表格);history.go(-1);</script>";
  4. }
复制代码
自己修改如下:
  1. //判断是否选择了要上传的表格
  2.         if (empty($_FILES['myfile']['name'])) {
  3.             $ourphp_font = 4;
  4.                 $ourphp_content = '您未选择表格!';
  5.                 $ourphp_class = 'ourphp_excel.php?id=ourphp';
  6.                 require 'ourphp_remind.php';
  7.         }
复制代码
其他的基本上和案例的代码类似,就是相应的按照ourphp程序修改成了适合的代码了,其他的自己研究下了,贴出ourphp_excel.php全部代码,仅供参考了。
  1. <?php
  2. include 'ourphp_admin.php';
  3. include 'ourphp_checkadmin.php';
  4. include 'plugs/phpexcel/PHPExcel/IOFactory.php';
  5. if (isset($_GET["ourphp_cms"]) == "add"){
  6.         header("Content-type:text/html;charset=utf-8");
  7.         if($_POST["kl"] == ''){
  8.        
  9.                 $ourphp_font = 4;
  10.                 $ourphp_content = '口令码不能为空!';
  11.                 $ourphp_class = 'ourphp_excel.php?id=ourphp';
  12.                 require 'ourphp_remind.php';
  13.                                                        
  14.         }elseif($_POST["kl"] != $ourphp['validation']){
  15.        
  16.                 $ourphp_font = 4;
  17.                 $ourphp_content = '口令码错误!';
  18.                 $ourphp_class = 'ourphp_excel.php?id=ourphp';
  19.                 require 'ourphp_remind.php';

  20.         }
  21.         //判断是否选择了要上传的表格
  22.         if (empty($_FILES['myfile']['name'])) {
  23.             $ourphp_font = 4;
  24.                 $ourphp_content = '您未选择表格!';
  25.                 $ourphp_class = 'ourphp_excel.php?id=ourphp';
  26.                 require 'ourphp_remind.php';
  27.         }

  28.                 //获取表格的大小,限制上传表格的大小5M
  29.                 $file_size = $_FILES['myfile']['size'];
  30.                 if ($file_size>5*1024*1024) {
  31.                     $ourphp_font = 4;
  32.                         $ourphp_content = '上传失败,上传的表格不能超过5M的大小';
  33.                         $ourphp_class = 'ourphp_excel.php?id=ourphp';
  34.                         require 'ourphp_remind.php';
  35.                 }

  36.                 //限制上传表格类型
  37.                 $file_type = $_FILES['myfile']['type'];
  38.                 //application/vnd.ms-excel  为xls文件类型
  39.                 if ($file_type!='application/vnd.ms-excel') {
  40.                         $ourphp_font = 4;
  41.                         $ourphp_content = '上传失败,只能上传excel2003的xls格式!';
  42.                         $ourphp_class = 'ourphp_excel.php?id=ourphp';
  43.                         require 'ourphp_remind.php';
  44.                 }

  45.                 //判断表格是否上传成功
  46.                 if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
  47.                     require_once 'plugs/phpexcel/PHPExcel.php';
  48.                         require_once 'plugs/phpexcel/PHPExcel/IOFactory.php';
  49.                         require_once 'plugs/phpexcel/PHPExcel/Reader/Excel5.php';
  50.                         //以上三步加载phpExcel的类

  51.                         $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
  52.                         //接收存在缓存中的excel表格
  53.                         $filename = $_FILES['myfile']['tmp_name'];
  54.                         $objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
  55.                         $sheet = $objPHPExcel->getSheet(0);
  56.                         $highestRow = $sheet->getHighestRow(); // 取得总行数
  57.                         $highestColumn = $sheet->getHighestColumn(); // 取得总列数
  58.    
  59.                         //循环读取excel表格,读取一条,插入一条
  60.                         //ii表示从哪一行开始读取  从第二行开始读取,因为第一行是标题不保存
  61.                         //$a表示列号
  62.                         for($ii=2;$ii<=$highestRow;$ii++){
  63.                                 $a = $objPHPExcel->getActiveSheet()->getCell("A".$ii)->getValue();//获取A(产品名称)列的值
  64.                         $b = $objPHPExcel->getActiveSheet()->getCell("B".$ii)->getValue();//获取B(产品编号)列的值
  65.                         $c = $objPHPExcel->getActiveSheet()->getCell("C".$ii)->getValue();//获取C(产品货号)列的值
  66.                         $d = $objPHPExcel->getActiveSheet()->getCell("D".$ii)->getValue();//获取D(大类)列的值
  67.                                 $e = $objPHPExcel->getActiveSheet()->getCell("E".$ii)->getValue();//获取E(小类)列的值
  68.                                 $f = $objPHPExcel->getActiveSheet()->getCell("F".$ii)->getValue();//获取F(价格)列的值
  69.                                 $g = $objPHPExcel->getActiveSheet()->getCell("G".$ii)->getValue();//获取G(数量)列的值
  70.                                 $h = $objPHPExcel->getActiveSheet()->getCell("H".$ii)->getValue();//获取H(描述)列的值
  71.                                 $i = $objPHPExcel->getActiveSheet()->getCell("I".$ii)->getValue();//获取I(内容)列的值
  72.                                 $j = $objPHPExcel->getActiveSheet()->getCell("J".$ii)->getValue();//获取J(缩略图)列的值
  73.                                 $k = $objPHPExcel->getActiveSheet()->getCell("K".$ii)->getValue();//获取K(大图)列的值
  74.                                 $zarr=array("L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI");
  75.                                 $zcount=array_search($highestColumn,$zarr);
  76.                                 $n=array();
  77.                                 for($jj=0;$jj<=$zcount;$jj++){
  78.                                         $l=$objPHPExcel->getActiveSheet()->getCell($zarr[$jj].$ii)->getValue();//获取L-AI(附加属性)列的值
  79.                                         $m=$objPHPExcel->getActiveSheet()->getCell($zarr[$jj]."1")->getValue();//获取L-AI属性名
  80.                                         $n[]=$m.":".$l;
  81.                                         //查询属性中是否含有属性名称,如果不含有则添加,含有则不添加
  82.                                         $ourphp_rs = $db -> select("*","`ourphp_productattribute`","where `OP_Title` = '".$m."'");
  83.                                         if(empty($ourphp_rs)){
  84.                                                 $query = $db -> insert("`ourphp_productattribute`","`OP_Title` = '".admin_sql($m)."',`OP_Class` = '1',`OP_Text` = '',`OP_Sorting` = '".admin_sql($jj)."',`time` = '".date("Y-m-d H:i:s")."'","");
  85.                                         }
  86.                                 }
  87.                                 $o = implode('|',$n);
  88.                                
  89.                                 $ourphp_rs = $db -> select("*","`ourphp_column`","where `OP_Uid` = '4' and `OP_Columntitle` = '".$d."'");
  90.                                 if(empty($ourphp_rs)){
  91.                                         $query = $db -> insert("`ourphp_column`","`OP_Uid` = '4',`OP_Lang` = 'cn',`OP_Columntitle` = '".admin_sql($d)."',`OP_Columntitleto` = '',`OP_Model` = 'product',`OP_Templist` = 'cn_product.html',`OP_Tempview` = 'cn_productview.html',`OP_Url` = '',`OP_About` = '',`OP_Hide` = '0',`OP_Sorting` = '0',`OP_Briefing` = '',`OP_Img` = '',`OP_Userright` = '0',`OP_Weight` = '0',`OP_Adminopen` = '1,0,1'","");
  92.                                         $pcateid=msyql_insert_id();
  93.                                         $query = $db -> insert("`ourphp_column`","`OP_Uid` = '".$pcateid."',`OP_Lang` = 'cn',`OP_Columntitle` = '".admin_sql($e)."',`OP_Columntitleto` = '',`OP_Model` = 'product',`OP_Templist` = 'cn_product.html',`OP_Tempview` = 'cn_productview.html',`OP_Url` = '',`OP_About` = '',`OP_Hide` = '0',`OP_Sorting` = '0',`OP_Briefing` = '',`OP_Img` = '',`OP_Userright` = '0',`OP_Weight` = '0',`OP_Adminopen` = '1,0,1'","");
  94.                                         $ccateid=msyql_insert_id();
  95.                                 }else{
  96.                                         $pcateid=$ourphp_rs['id'];
  97.                                         $ourphp_rs2 = $db -> select("*","`ourphp_column`","where `OP_Uid` = '".$pcateid."' and `OP_Columntitle` = '".$e."'");
  98.                                         if(empty($ourphp_rs)){
  99.                                                 $query = $db -> insert("`ourphp_column`","`OP_Uid` = '".$pcateid."',`OP_Lang` = 'cn',`OP_Columntitle` = '".admin_sql($e)."',`OP_Columntitleto` = '',`OP_Model` = 'product',`OP_Templist` = 'cn_product.html',`OP_Tempview` = 'cn_productview.html',`OP_Url` = '',`OP_About` = '',`OP_Hide` = '0',`OP_Sorting` = '0',`OP_Briefing` = '',`OP_Img` = '',`OP_Userright` = '0',`OP_Weight` = '0',`OP_Adminopen` = '1,0,1'","");
  100.                                                 $ccateid=msyql_insert_id();
  101.                                         }else{
  102.                                                 $ccateid=$ourphp_rs2['id'];;
  103.                                         }
  104.                                
  105.                                 }
  106.                                
  107.                         //null 为主键id,自增可用null表示自动添加
  108.                                 $query = $db -> insert("`ourphp_product`","`OP_Class` = '".$ccateid."',`OP_Lang` = 'cn',`OP_Title` = '".admin_sql($a)."',`OP_Number` = '".admin_sql($b)."',`OP_Goodsno` = '".admin_sql($c)."',`OP_Brand` = '',`OP_Market` = '".admin_sql($f)."',`OP_Webmarket` = '".admin_sql($f)."',`OP_Stock` = '".admin_sql($g)."',`OP_Usermoney` = '1:0.00|2:0.00|3:0.00|4:0.00|5:0.00',`OP_Specificationsid` = '',`OP_Specificationstitle` = '',`OP_Specifications` = '',`OP_Pattribute` = '".$o."',`OP_Minimg` = '".$j."',`OP_Maximg` = '".$k."',`OP_Img` = '',`OP_Content` = '".admin_sql($i)."',`OP_Down` = '2',`OP_Weight` = '1',`OP_Freight` = '1',`OP_Tag` = '',`OP_Sorting` = '99',`OP_Attribute` = '',`OP_Url` = '',`OP_Description` = '".compress_html($h)."',`time` = '".date("Y-m-d H:i:s")."',`OP_Integral` = '0.00',`OP_Integralok` = '0',`OP_Integralexchange` = '0.00',`OP_Suggest` = ''","");
  109.                        
  110.                         }
  111.                 }

  112.        
  113.         $ourphp_font = 5;
  114.         $ourphp_img = 'ok.png';
  115.         $ourphp_content = '操作成功!';
  116.         $ourphp_class = 'ourphp_excel.php?id=ourphp';
  117.         require 'ourphp_remind.php';

  118. }
  119. Admin_click('批量上传Excel','ourphp_excel.php?id=ourphp');
  120. $smarty->display('ourphp_excel.html');
  121. ?>
复制代码


206

主题

206

主题

206

主题

管理员

Rank: 9Rank: 9Rank: 9

积分
0
admin 发表于 2017-12-8 10:48:12 | 显示全部楼层
不错,谢谢分享
源码可以打包传给我们,上传网盘下载

0

主题

0

主题

0

主题

高级会员

Rank: 4

积分
621
蓝色屠龙刀 发表于 2018-3-3 14:14:54 | 显示全部楼层
辛苦辛苦,谢谢了~~












葛根茶批发 网狼葛根茶 广西葛根茶 广西葛根茶批发 葛根茶价格

4

主题

4

主题

4

主题

注册会员

Rank: 2

积分
116
纳兰词 发表于 2018-9-28 11:33:46 | 显示全部楼层
我要代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表