PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/products/add_products.php

http://online-wholesale.googlecode.com/
PHP | 302 lines | 286 code | 16 blank | 0 comment | 6 complexity | a8dbecf5add1a69caf22e255e0957c0d MD5 | raw file
  1. <div class="page">
  2. <div class="menu" style="margin-top:-50px;">
  3. <?php include('modules/members/dashboard.php');?>
  4. </div>
  5. <div class="products" style="margin-left: 40px;">
  6. <?php if($_GET['ad_by_upload']){ ?>
  7. <div class="add-products-header" style="width:570px;overflow:hidden;margin:0 auto;padding:10px;">
  8. <form action="?m=products&p=add_products&ad_by_upload=true&process=true" method="post" enctype="multipart/form-data">
  9. <h2>???????????????? CSV ????????</h2>
  10. <p style="text-indent:30px;color:blue;margin-bottom:15px;">?????????????????????? Excel(.xls) ???? CSV(.csv) ??????????? <span style="color:red;">(???????????????????????????? .xls ??? ??????????? .xls ?????????????????????????????? windows server ???? IIS ??? server ?????????????? MS Office ????????) </span></p>
  11. <h3>???????????????????????</h3>
  12. <table border="1" cellpadding="0" cellspacing="0">
  13. <tr align="center">
  14. <td width="150">????????????</td>
  15. <td width="150">???? / ?????</td>
  16. <td width="150">??????????????</td>
  17. <td width="150">????????</td>
  18. </tr>
  19. </table>
  20. <div class="clear" style="margin: 20px;"></div>
  21. <div align="center">
  22. <input type="file" name="products_file" id="products_file" size="50" align="middle"/>
  23. <div class="clear" style="margin-top:20px;"></div>
  24. <input type="submit" value="?????????" />
  25. </div>
  26. </form>
  27. </div>
  28. <?php if($_GET['process']){ ?>
  29. <div style="width:570px;overflow:hidden;margin:0 auto;padding:10px;">
  30. <?php
  31. copy($_FILES["products_file"]["tmp_name"],'uploads/'.$_FILES["products_file"]["name"]);
  32. $csvfile = 'uploads/'.$_FILES["products_file"]["name"];
  33. $databasetable = "products";
  34. $fieldseparator = ",";
  35. $lineseparator = "\n";
  36. $addauto = 1;
  37. $has_id = 1;
  38. $save = 0;
  39. $lines = 0;
  40. $queries = "";
  41. $linearray = array();
  42. $first_loop = false;
  43. $u = mysql_query('SELECT * FROM units');
  44. if(!file_exists($csvfile)) {
  45. echo "?????????.\n";
  46. exit;
  47. }
  48. $file = fopen($csvfile,"r");
  49. if(!$file) {
  50. echo "Error ????????????????????.\n";
  51. exit;
  52. }
  53. $size = filesize($csvfile);
  54. if(!$size) {
  55. echo "???????????????.\n";
  56. exit;
  57. }
  58. $csvcontent = fread($file,$size);
  59. fclose($file);
  60. foreach(split($lineseparator,$csvcontent) as $line) {
  61. $lines++;
  62. $line = trim($line," \t");
  63. $line = str_replace("\r","",$line);
  64. $line = str_replace("'","\'",$line);
  65. $linearray = explode($fieldseparator,$line);
  66. $arr = explode('/',$linearray[1]);
  67. $count = count($linearray)+4;
  68. $j=false;
  69. $k=true;
  70. $newarray = array();
  71. foreach($linearray as $value){
  72. if($j){
  73. $newarray[] = $arr[0];
  74. $newarray[] = $arr[1];
  75. $newarray[] = 0;
  76. $k= false;
  77. $j= false;
  78. }else{
  79. $newarray[] = $value;
  80. if($k) $j = true;
  81. }
  82. }
  83. mysql_data_seek($u,0);
  84. while($r = mysql_fetch_assoc($u)){
  85. $unit = iconv('tis-620','utf-8', $newarray[2]);
  86. if($r['name'] == $unit){
  87. $newarray[2] = $r['id'];
  88. }
  89. }
  90. $linemysql = implode("','",$newarray);
  91. for($i=$count;$i<12;$i++){
  92. $linemysql .= "','";
  93. }
  94. if($addauto == 1 && $has_id == 1)
  95. if($first_loop){
  96. $query = "insert into $databasetable values(NULL,'$lines".date('dmYHms',time())."','".iconv('tis-620','utf-8',$linemysql)."');";
  97. }else{
  98. $lines = 0;
  99. $first_loop = true;
  100. }
  101. elseif($addauto == 1 && $has_id == 0)
  102. $query = "insert into $databasetable values(NULL,'".iconv('tis-620','utf-8',$linemysql)."');";
  103. elseif($first_loop && $linemysql != "" && $has_id == 1)
  104. $query = "insert into $databasetable values('$lines".date('dmYHms',time())."','".iconv('tis-620','utf-8',$linemysql)."');";
  105. elseif($first_loop && $linemysql != "" && $has_id == 0)
  106. $query = "insert into $databasetable values('".iconv('tis-620','utf-8',$linemysql)."');";
  107. else
  108. $first_loop = true;
  109. $queries .= $query . "\n<br/>";
  110. if($newarray[0] != "" && $query != "") mysql_query($query)or die(mysql_error());
  111. //echo "??????????????? $lines records <br/>";
  112. //if($query != "") mysql_query($query)or die(mysql_error());
  113. }
  114. //echo $queries;
  115. //mysql_close($con);
  116. if($save) {
  117. if(!is_writable($outputfile)) {
  118. echo "?????????????????????????????????\n";
  119. }else {
  120. $file2 = fopen($outputfile,"w");
  121. if(!$file2) {
  122. echo "?????????????????????.\n";
  123. }else {
  124. fwrite($file2,$queries);
  125. fclose($file2);
  126. }
  127. }
  128. }
  129. echo "????????????????????????? $lines records ??????? ".substr($csvfile,strrpos($csvfile,'/')+1)."\n";
  130. ?>
  131. </div>
  132. <?php }?>
  133. <?php }else{?>
  134. <p style="margin-left: 60px;margin-top:0px;float:right;" id="button">
  135. <a href="?m=products&p=add_products&ad_by_upload=true" class="btn" style="font-size:25px;" id="add_by_upload"><span>?????????????????????????</span></a>
  136. </p>
  137. <div class="clear"></div>
  138. <div class="add-products-header">
  139. <span class="header">????????????</span>
  140. <span class="header" style="margin-left:55px;">????</span>
  141. <span class="header" style="margin-left:40px;margin-right:-11px;">?????</span>
  142. <span class="header" >?????</span>
  143. <span class="header" style="margin-left:23px;">??????????????</span>
  144. <span class="header" style="margin-left:40px;">????????</span>
  145. </div>
  146. <form action="#" method="POST" id="add_product_form">
  147. <div class="products-group">
  148. <div class="products-input-template">
  149. <input type="text" name="name" id="pname" value="" style="width:155px;height:20px;"/>
  150. <input type="text" name="price" id="pprice" value="" style="width:80px;height:20px;"/>
  151. <select name="unit" id="unit" style="margin-right:10px;">
  152. <?php
  153. $sql = 'SELECT * FROM units';
  154. $q = $db->select_query($sql);
  155. while($rs = $db->fetch($q)){
  156. ?>
  157. <option value="<?php echo $rs['id'];?>"><?php echo $rs['name'];?></option>
  158. <?php } ?>
  159. </select>
  160. <input type="text" name="num" id="pnum" value="" style="width:80px;height:20px;"//>
  161. <input type="text" name="promotion" id="ppromotion" value="" style="width:155px;height:20px;"//>
  162. <input type="text" name="detail" id="pdetail" value="" style="width:155px;height:20px;"//>
  163. <input type="hidden" name="code" id="pcode" value="<?php echo genStr(); ?>" />
  164. <!--<a href="#" onClick="return false;" id="del_row">
  165. <img src="css/images/delete.png" alt="?????" />
  166. </a>-->
  167. </div>
  168. </div>
  169. </form>
  170. </div>
  171. <p style="margin-left: 60px;margin-top:25px;" id="button">
  172. <a href="#" class="btn" style="font-size:25px;" id="add_products" onClick="return false;"><span>???????????</span></a>
  173. <span class="loading"></span>
  174. </p>
  175. <div class="clear"></div>
  176. <form action="#" method="POST" id="add_product_form" style="margin-left: 40px; height: 200px;margin-bottom: 20px;overflow: hidden;">
  177. <div class="products-group">
  178. <h2>5 ??????????????????</h2>
  179. <table id="products_list" width="100%">
  180. <thead>
  181. <tr>
  182. <th width="40">ID</th>
  183. <th>????????????</th>
  184. <th>????</th>
  185. <th>?????</th>
  186. <th>???????</th>
  187. <th>??????????????</th>
  188. <th>????????</th>
  189. </tr>
  190. </thead>
  191. <tbody>
  192. <?php
  193. $sql = 'SELECT
  194. `products`.*
  195. , `units`.`name` AS unit
  196. FROM
  197. `posterth_store`.`units`
  198. INNER JOIN `posterth_store`.`products`
  199. ON (`units`.`id` = `products`.`unit_id`)
  200. ORDER BY `products`.`id` DESC
  201. LIMIT 0,5';
  202. $q = mysql_query($sql);
  203. while($rs = mysql_fetch_assoc($q)){?>
  204. <tr>
  205. <td align="center"><?php echo $rs["id"];?></td>
  206. <td>
  207. <a href="?m=products&p=order_products&pedit=true&code=<?php echo $rs['code'];?>">
  208. <?php echo $rs["name"];?>
  209. </a>
  210. </td>
  211. <td><?php echo $rs["price"];?></td>
  212. <td><?php echo $rs["unit"];?></td>
  213. <td><?php echo $rs["num"];?></td>
  214. <td><?php echo $rs["promotion"];?></td>
  215. <td><?php echo $rs["annotation"];?></td>
  216. </tr>
  217. <?php } ?>
  218. </tbody>
  219. </table>
  220. </div>
  221. </form>
  222. <?php } ?>
  223. </div>
  224. <div class="products-template" style="display:none;">
  225. <div class="products-input-template">
  226. <input type="text" name="name" id="pname" value="" style="width:155px;height:20px;"/>
  227. <input type="text" name="price" id="pprice" value="" style="width:80px;height:20px;"/>
  228. <select name="unit" id="unit" style="margin-right:10px;">
  229. <?php
  230. $sql = 'SELECT * FROM units';
  231. $q = $db->select_query($sql);
  232. while($rs = $db->fetch($q)){
  233. ?>
  234. <option value="<?php echo $rs['id'];?>"><?php echo $rs['name'];?></option>
  235. <?php } ?>
  236. </select>
  237. <input type="text" name="num" id="pnum" value="" style="width:80px;height:20px;"//>
  238. <input type="text" name="promotion" id="ppromotion" value="" style="width:155px;height:20px;"//>
  239. <input type="text" name="detail" id="pdetail" value="" style="width:155px;height:20px;"//>
  240. <input type="hidden" name="code" id="pcode" value="" />
  241. <!--<a href="#" onClick="return false;" id="del_row">
  242. <img src="css/images/delete.png" alt="?????" />
  243. </a>-->
  244. </div>
  245. </div>
  246. <style type="text/css">
  247. .ui-state-default .ui-icon {
  248. float:right;
  249. }
  250. </style>
  251. <script type="text/javascript">
  252. $(function(){
  253. $('#products_list').dataTable({
  254. "bJQueryUI": true,
  255. "sDom": 't',
  256. "aaSorting": [[ 0, "desc" ]],
  257. "aoColumns": [
  258. null,
  259. null,
  260. null,
  261. null,
  262. null,
  263. null,
  264. null
  265. ],
  266. "iDisplayLength": 5
  267. });
  268. $('input[name=detail]').bind('keypress', function(e) {
  269. if(e.keyCode==13){
  270. $('a#add_products').click();
  271. }
  272. });
  273. });
  274. </script>