PHPExcel导入excel数据到数据库中

绝地灬酷狼 2022-08-05 08:23 436阅读 0赞

以下内容是基于ThinkPHP框架。。

PHPExcel导入excel数据到数据库中

简述:导入的excel表格文件由四个工作表组成,其中前面三个工作表根据客户名称关联起来,它们共同构成数据库中12个表的每条数据,第四个工作表是独立的产品信息表。12个表都是关联表,含有其他表的外键。

数据库字段结构设计是全部不能为空,主键id都是自增。

1、总体实现思路

为每个数据库表创建一个数组来保存Excel表格中读取的数据,数组的键等于数据库字段名,数组的值等于excel对应的列的内容。先读取第一个工作表的数据,根据工作表行数增加来读取每条数据保存到数组中,然后使用

M()->add($data)方法添加数组到数据库中。通过PHPExcel类库中的$phpexcel->getSheet(0)方法可以读取每个工作表。

2、如何关联数据表的外键

TP中调用模型的add()方法添加数据成功后会返回插入的记录的主键id,保存该id。

一个客户名称存在于3个工作表的数据通过客户名称关联起来,也就是3个工作表中都有客户名称。所以插入其他工作表的数据到数据库时需要根据客户名称获得关联的表的主键。

$condition[‘name’] = $customer_name;

$res = M(‘customer_info’)->where($condition)->find();

$data_procurement_plan[‘customer_id’] = $res[‘id’];//关联customer_info表的id

3、添加相同的客户名称的记录

客户名称在客户信息表中是唯一存在的所以该字段设置唯一索引。Excel中含有数据库中已经存在的客户名称时更新记录,调用删除方法删除存在的12个表中的记录,再添加新记录。

4、excel有的列为空,但是数据库不能有空字段,设置默认值

定义一个函数对数组进行检查,有空的键给设默认值

public function setArrayDefault(&$arr){

foreach($arr a4 $k => $v){

arr[$k] = isset($v)?$v:’’;

}

return $arr;

}

5、数据库表中的每个字段在excel中都为空时,不插入该记录。例如无历史合作信息。

if( trim($data_old_cooperation[‘cooperatiom_time’]) !== ‘’)

|| trim($data_old_cooperation[‘cooperatiom_type’]) !== ‘’

|| trim($data_old_cooperation[‘cooperatiom_rank’]) !== ‘’ ){

// 添加记录代码

}

三个字段只要有一个有数据就插入,否则不插入。

trim() 函数是去除字符串两端的空格,防止excel中内容为空格的记录插入。

6、excel中的日期处理

很奇怪在excel中的日期2015-09-09在php代码中会变成42256,得不到正确的日期。

处理如下:首先在excel模版的有日期的列设置单元格格式,选中列头,右键->单元格格式->日期->格式码,设置为YYYY-MM-DD。

function formatData($data){

$data = trim($data);// 去除空格

if(empty($data)) return “0000-00-00”;

$n = intval(($data - 25569)*3600*24);// 转换成1970年以来的秒数

$format_data = gmdata(‘Y-m-d’,$n);// 格式化的时间

return $format_data;

}

$data 是从excel中读取的内容;

return “0000-00-00” 是设置自定义的默认值,如果excel内容为空的话,插入到数据库中得到的结果是1899-12-30,该值是输入日期为空时的默认时间。

注意事项:

1、数据库字段名称统一用小写,代码中字段名要可数据库中的大小写对应,否则可能插入数据失败。

2、PHPExcel插件放在Public目录下,没找到已经定义的指向public文件的路径,所以在index.php中定义了PUBLIC_PATH的路径。

完整代码如下:

  1. 控制器代码:
  2. <?php
  3. namespace Home\Controller;
  4. use Think\Controller;
  5. class PhpexcelController extends Controller {
  6. private static function init(){
  7. // 设置字符集编码
  8. header('Content-type:text/html;charset=utf-8');
  9. // 加载类库
  10. include_once PHPEXCEL_PATH.'/PHPExcel.php';
  11. include_once PHPEXCEL_PATH.'/PHPExcel/Style/Fill.php';
  12. }
  13. /**
  14. * 导入一个excel表的多个sheet的数据
  15. */
  16. public function importmoreexcel(){
  17. self::init();
  18. if($_FILES["morefile"]["error"] > 0){
  19. echo 'ERROR:'.$_FILES["morefile"]["error"]."处错误<br/>请重新导入...";
  20. }else{
  21. // 注意:上传中文文件名出现乱码,暂时不能上传中文文件
  22. //$filename = iconv("GB2312","UTF-8",$_FILES["morefile"]["name"]);
  23. // 文件存放路径
  24. $filepath =UPLOAD_PATH."/".$_FILES["morefile"]["name"];
  25. move_uploaded_file($_FILES["morefile"]["tmp_name"],$filepath);
  26. echo '文件保存在:'.$filepath."<br/>";
  27. // 创建读文件对象
  28. $objReader = new \PHPExcel_Reader_Excel2007();
  29. if(!$objReader->canRead($filepath)){
  30. $objReader = new \PHPExcel_Reader_Excel5();
  31. if(!$objReader->canRead($filepath)){
  32. echo '上传的不是excel文件...';
  33. return;
  34. }
  35. }
  36. // 读取第一张表,客户基本信息excel表
  37. $phpexcel = $objReader->load($filepath);
  38. $current_excel = $phpexcel->getSheet(0);
  39. $allRow = $current_excel->getHighestRow();// 得到总行数
  40. // 插入记录成功的条数
  41. $count_success = 0;
  42. // 插入记录失败的条数
  43. $count_fail = 0;
  44. // 更新的记录树木
  45. $count_upload = 0;
  46. // 实例化模型
  47. $m_customer_info = M('customer_info');
  48. $m_customer_data = M('customer_data');
  49. $m_old_cooperation = M('old_cooperation');
  50. for($row = 3;$row <= $allRow; $row++){
  51. $data_customer_data = array();
  52. $data_customer_info = array();// 客户基本信息
  53. $data_old_cooperation = array();
  54. $data_customer_info['name'] = $current_excel->getCell('B'.$row)->getValue();
  55. $data_customer_info['user_name'] = $current_excel->getCell('C'.$row)->getValue();
  56. $data_customer_info['type'] = $current_excel->getCell('D'.$row)->getValue();
  57. $data_customer_info['account'] = $current_excel->getCell('E'.$row)->getValue();
  58. $data_customer_info['con_name'] = $current_excel->getCell('F'.$row)->getValue();
  59. $data_customer_info['phone'] = $current_excel->getCell('G'.$row)->getValue();
  60. $data_customer_info['qq'] = $current_excel->getCell('H'.$row)->getValue();
  61. $data_customer_info['wechat'] = $current_excel->getCell('I'.$row)->getValue();
  62. $data_customer_info['email'] = $current_excel->getCell('J'.$row)->getValue();
  63. $data_customer_info['company_address'] = $current_excel->getCell('K'.$row)->getValue();
  64. $data_customer_info['company_phone'] = $current_excel->getCell('L'.$row)->getValue();
  65. $data_customer_info['company_email'] = $current_excel->getCell('M'.$row)->getValue();
  66. $data_customer_info['web'] = $current_excel->getCell('N'.$row)->getValue();
  67. $data_customer_info['money'] = $current_excel->getCell('O'.$row)->getValue();
  68. $data_customer_info['is_listed'] = $current_excel->getCell('P'.$row)->getValue();
  69. $data_customer_info['market_value'] = $current_excel->getCell('Q'.$row)->getValue();
  70. $data_customer_info['sales_month'] = $current_excel->getCell('R'.$row)->getValue();
  71. $data_customer_info['storage_facilities'] = $current_excel->getCell('S'.$row)->getValue();
  72. $data_customer_info['it_systems'] = $current_excel->getCell('T'.$row)->getValue();
  73. // 客户附加信息
  74. $data_customer_data['plan'] = $current_excel->getCell('U'.$row)->getValue();
  75. $data_customer_data['other_supplier'] = $current_excel->getCell('V'.$row)->getValue();
  76. $data_customer_data['cooperation_proportion'] = $current_excel->getCell('W'.$row)->getValue();
  77. $data_customer_info['create_time'] = date('Y-m-d h:i:s',time());
  78. $data_customer_info['description'] = $current_excel->getCell('X'.$row)->getValue();
  79. // 历史合作信息
  80. $data_old_cooperation['cooperation_time'] = formatData($current_excel->getCell('Y'.$row)->getValue());
  81. $data_old_cooperation['cooperation_type'] = $current_excel->getCell('Z'.$row)->getValue();
  82. $data_old_cooperation['cooperation_rank'] = $current_excel->getCell('AA'.$row)->getValue();
  83. // 添加前判断
  84. $this->setArrayDefault($data_customer_info);
  85. $this->setArrayDefault($data_customer_data);
  86. // 判断客户名称是否已经存在,存在则删除以前的记录,再添加记录
  87. $condition_c_i['name'] = $data_customer_info['name'];
  88. $result = $m_customer_info->where($condition_c_i)->find();
  89. if(!empty($result)){
  90. $this->delete($result['id']);
  91. $count_upload ++;
  92. }
  93. // 插入数据到数据库中
  94. // 插入到客户基本资料表
  95. $customer_id = $m_customer_info->add($data_customer_info);// 保存插入的id,关联其他表
  96. if($customer_id == false){
  97. $count_fail ++;
  98. }else{
  99. $count_success ++;
  100. }
  101. // 插入数据到客户附加信息表
  102. $data_customer_data['customer_id'] = $customer_id;
  103. if($m_customer_data->add($data_customer_data) == false){
  104. $count_fail ++;
  105. }else{
  106. $count_success ++;
  107. }
  108. /**
  109. * 插入到历史合作信息
  110. * 如果历史合作信息字段全没设置则不插入记录到数据库
  111. */
  112. if(trim($data_old_cooperation['cooperation_time']) !== ''
  113. || trim($data_old_cooperation['cooperation_type']) !== ''
  114. || trim($data_old_cooperation['cooperation_rank']) !== ''){
  115. $this->setArrayDefault($data_old_cooperation);
  116. $data_old_cooperation['customer_id'] = $customer_id;
  117. if($m_old_cooperation->add($data_old_cooperation) == false){
  118. $count_fail ++;
  119. }else{
  120. $count_success ++;
  121. }
  122. }
  123. }
  124. // 关键订单excel表
  125. $current_excel = $phpexcel->getSheet(1);
  126. $allRow = $current_excel->getHighestRow();// 得到总行数
  127. $m_procurement_plan = M('procurement_plan');
  128. $m_key_order = M('key_order');
  129. $m_supplier = M('supplier');
  130. $m_r_order = M('r_order_supplier');
  131. $m_r_plan = M('r_plan_product');
  132. for($row = 3;$row <= $allRow; $row++){
  133. $data_procurement_plan = array();
  134. $data_key_order = array();
  135. $data_r_plan = array();
  136. $data_r_order = array();
  137. $data_supplier = array();
  138. // 得到客户名称
  139. $customer_name = $current_excel->getCell('B'.$row)->getValue();
  140. // 采购计划与产品关联表
  141. $data_r_plan['product_id'] = $current_excel->getCell('C'.$row)->getValue();
  142. // 采购计划
  143. $data_procurement_plan['expect_price'] = $current_excel->getCell('D'.$row)->getValue();
  144. $data_procurement_plan['order_time'] = formatData($current_excel->getCell('E'.$row)->getValue());
  145. $data_procurement_plan['expect_delivery_date'] = formatData($current_excel->getCell('F'.$row)->getValue());
  146. $data_procurement_plan['pay'] = $current_excel->getCell('G'.$row)->getValue();
  147. $data_procurement_plan['delivery_address'] = $current_excel->getCell('H'.$row)->getValue();
  148. $data_procurement_plan['transport'] = $current_excel->getCell('I'.$row)->getValue();
  149. $data_procurement_plan['company_doc'] = $current_excel->getCell('J'.$row)->getValue();
  150. $data_procurement_plan['product_doc'] = $current_excel->getCell('K'.$row)->getValue();
  151. $data_procurement_plan['after_sales_agreement'] = $current_excel->getCell('L'.$row)->getValue();
  152. $data_procurement_plan['default_agreement'] = $current_excel->getCell('M'.$row)->getValue();
  153. $data_procurement_plan['is_cooperation'] = $current_excel->getCell('N'.$row)->getValue();
  154. // 供应商信息
  155. $data_supplier['name'] = $current_excel->getCell('O'.$row)->getValue();
  156. $data_supplier['quote'] = $current_excel->getCell('P'.$row)->getValue();
  157. // 关键订单
  158. $data_key_order['our_quote'] = $current_excel->getCell('Q'.$row)->getValue();
  159. $data_key_order['moq'] = $current_excel->getCell('R'.$row)->getValue();
  160. $data_key_order['profit_rate'] = $current_excel->getCell('S'.$row)->getValue();
  161. $data_key_order['type'] = $current_excel->getCell('T'.$row)->getValue();
  162. $data_key_order['status'] = $current_excel->getCell('U'.$row)->getValue();
  163. // 添加前判断
  164. $this->setArrayDefault($data_procurement_plan);
  165. $this->setArrayDefault($data_key_order);
  166. $this->setArrayDefault($data_supplier);
  167. $this->setArrayDefault($data_r_order);
  168. $this->setArrayDefault($data_r_plan);
  169. // 插入数据到数据库
  170. /**
  171. * 插入数据到采购计划表
  172. * 查询客户基本信息表返回客户ID作为采购计划的客户id
  173. */
  174. $condition['name'] = $customer_name;
  175. $res = $m_customer_info->where($condition)->find();
  176. $data_procurement_plan['customer_id'] = $res['id'];
  177. $procurement_plan_id = $m_procurement_plan->add($data_procurement_plan);
  178. if($procurement_plan_id == false){
  179. $count_fail ++;
  180. }else{
  181. $count_success ++;
  182. }
  183. // 插入数据到采购计划与产品关联表
  184. // 插入数据到关键订单表
  185. $data_key_order['plan_id'] = $procurement_plan_id;
  186. $key_order_id = $m_key_order->add($data_key_order);
  187. if($key_order_id == false){
  188. $count_fail ++;
  189. }else{
  190. $count_success ++;
  191. }
  192. // 插入到供应商信息表
  193. $supplier_id = $m_supplier->add($data_supplier);
  194. if($supplier_id == false){
  195. $count_fail ++;
  196. }else{
  197. $count_success ++;
  198. }
  199. // 插入到关键订单与供应商关联表
  200. $data_r_order['order_id'] = $key_order_id;
  201. $data_r_order['supplier_id'] = $supplier_id;
  202. if($m_r_order->add($data_r_order) == false){
  203. $count_fail ++;
  204. }else{
  205. $count_success ++;
  206. }
  207. // 插入到采购计划与产品关联表
  208. $data_r_plan['plan_id'] = $procurement_plan_id;
  209. if($m_r_plan->add($data_r_plan) == false){
  210. $count_fail ++;
  211. }else{
  212. $count_success ++;
  213. }
  214. }
  215. // 促销活动excel表
  216. $current_excel = $phpexcel->getSheet(2);
  217. $allRow = $current_excel->getHighestRow();// 得到总行数
  218. $m_activity = M('activity');
  219. $m_r_activity = M('r_activity_product');
  220. $m_our_prepare = M('our_prepare');
  221. for($row = 3;$row <= $allRow; $row++){
  222. $data_activity = array();
  223. $data_our_prepare = array();
  224. $data_r_activity = array();
  225. // 得到客户名称
  226. $customer_name = $current_excel->getCell('B'.$row)->getValue();
  227. // 活动情况
  228. $data_activity['theme'] = $current_excel->getCell('C'.$row)->getValue();
  229. $data_activity['act_date'] = $current_excel->getCell('D'.$row)->getValue();
  230. $data_activity['act_addr'] = $current_excel->getCell('E'.$row)->getValue();
  231. $data_activity['act_desc'] = $current_excel->getCell('F'.$row)->getValue();
  232. $data_activity['description'] = $current_excel->getCell('G'.$row)->getValue();
  233. $data_activity['model'] = $current_excel->getCell('H'.$row)->getValue();
  234. $data_activity['traffic_sources'] = $current_excel->getCell('I'.$row)->getValue();
  235. $data_activity['conversion_rate'] = $current_excel->getCell('J'.$row)->getValue();
  236. // 活动与产品关联表
  237. $data_r_activity['product_id'] = $current_excel->getCell('K'.$row)->getValue();
  238. $data_activity['quantity'] = $current_excel->getCell('L'.$row)->getValue();
  239. $data_activity['cycle'] = $current_excel->getCell('M'.$row)->getValue();
  240. $data_activity['expect_traffic'] = $current_excel->getCell('N'.$row)->getValue();
  241. $data_activity['hr_cs'] = $current_excel->getCell('O'.$row)->getValue();
  242. $data_activity['hr_it'] = $current_excel->getCell('P'.$row)->getValue();
  243. $data_activity['hr_art'] = $current_excel->getCell('Q'.$row)->getValue();
  244. $data_activity['hr_market'] = $current_excel->getCell('R'.$row)->getValue();
  245. $data_activity['hr_pr'] = $current_excel->getCell('S'.$row)->getValue();
  246. $data_activity['hr_other'] = $current_excel->getCell('T'.$row)->getValue();
  247. $data_activity['money'] = $current_excel->getCell('U'.$row)->getValue();
  248. $data_activity['goods'] = $current_excel->getCell('V'.$row)->getValue();
  249. $data_activity['storage_facilities'] = $current_excel->getCell('W'.$row)->getValue();
  250. $data_activity['it_systems'] = $current_excel->getCell('X'.$row)->getValue();
  251. $data_activity['price_strategy'] = $current_excel->getCell('Y'.$row)->getValue();
  252. $data_activity['ad_channel'] = $current_excel->getCell('Z'.$row)->getValue();
  253. $data_activity['ad_type'] = $current_excel->getCell('AA'.$row)->getValue();
  254. $data_activity['ad_money'] = $current_excel->getCell('AB'.$row)->getValue();
  255. $data_activity['order_feedback'] = $current_excel->getCell('AC'.$row)->getValue();
  256. $data_activity['after_sales_feedback'] = $current_excel->getCell('AD'.$row)->getValue();
  257. $data_activity['faq_feedback'] = $current_excel->getCell('AE'.$row)->getValue();
  258. $data_activity['sales_inventory_feedback'] = $current_excel->getCell('AF'.$row)->getValue();
  259. // 活动的准备
  260. $data_our_prepare['inventory'] = $current_excel->getCell('AG'.$row)->getValue();
  261. $data_our_prepare['procurement_plan'] = $current_excel->getCell('AH'.$row)->getValue();
  262. $data_our_prepare['emergency'] = $current_excel->getCell('AI'.$row)->getValue();
  263. $data_our_prepare['hr_cs'] = $current_excel->getCell('AJ'.$row)->getValue();
  264. $data_our_prepare['hr_it'] = $current_excel->getCell('AK'.$row)->getValue();
  265. $data_our_prepare['hr_purchase'] = $current_excel->getCell('AL'.$row)->getValue();
  266. $data_our_prepare['hr_sales_manager'] = $current_excel->getCell('AM'.$row)->getValue();
  267. $data_our_prepare['hr_art'] = $current_excel->getCell('AN'.$row)->getValue();
  268. $data_our_prepare['hr_market'] = $current_excel->getCell('AO'.$row)->getValue();
  269. $data_our_prepare['hr_pr'] = $current_excel->getCell('AP'.$row)->getValue();
  270. $data_our_prepare['hr_other'] = $current_excel->getCell('AQ'.$row)->getValue();
  271. $data_our_prepare['money'] = $current_excel->getCell('AR'.$row)->getValue();
  272. $data_our_prepare['goods'] = $current_excel->getCell('AS'.$row)->getValue();
  273. $data_our_prepare['storage_facilities'] = $current_excel->getCell('AT'.$row)->getValue();
  274. $data_our_prepare['it_systems'] = $current_excel->getCell('AU'.$row)->getValue();
  275. $data_our_prepare['expect_order'] = $current_excel->getCell('AV'.$row)->getValue();
  276. $data_our_prepare['expect_revenue'] = $current_excel->getCell('AW'.$row)->getValue();
  277. $data_our_prepare['expect_quantity'] = $current_excel->getCell('AX'.$row)->getValue();
  278. $data_our_prepare['expect_popularity'] = $current_excel->getCell('AY'.$row)->getValue();
  279. $data_our_prepare['reach_coop'] = $current_excel->getCell('AZ'.$row)->getValue();
  280. $data_our_prepare['coop_status'] = $current_excel->getCell('BA'.$row)->getValue();
  281. // 添加前判断
  282. $this->setArrayDefault($data_activity);
  283. $this->setArrayDefault($data_r_activity);
  284. $this->setArrayDefault($data_our_prepare);
  285. // 插入到数据库
  286. /**
  287. * 插入到活动情况表
  288. * 查询客户基本信息表返回客户ID作为活动情况的客户id
  289. */
  290. $condition['name'] = $customer_name;
  291. $res = $m_customer_info->where($condition)->find();
  292. $data_activity['customer_id'] = $res['id'];
  293. $data_activity['traffic'] = 1;
  294. $activity_id = $m_activity->add($data_activity);
  295. if($activity_id == false){
  296. $count_fail ++;
  297. }else{
  298. $count_success ++;
  299. }
  300. // 插入到活动与产品关联表
  301. $data_r_activity['activity_id'] = $activity_id;
  302. if($m_r_activity->add($data_r_activity) == false){
  303. $count_fail ++;
  304. }else{
  305. $count_success ++;
  306. }
  307. // 插入到活动的准备表
  308. $data_our_prepare['activity_id'] = $activity_id;
  309. if($m_our_prepare->add($data_our_prepare) == false){
  310. $count_fail ++;
  311. }else{
  312. $count_success ++;
  313. }
  314. }
  315. // 产品信息excel表
  316. $current_excel = $phpexcel->getSheet(3);
  317. $allRow = $current_excel->getHighestRow();// 得到总行数
  318. $m_product = M('product');
  319. for($row = 2;$row <= $allRow; $row++) {
  320. $data_product = array();
  321. $data_product['category'] = $current_excel->getCell('B' . $row )->getValue();
  322. $data_product['brand'] = $current_excel->getCell('C' . $row)->getValue();
  323. $data_product['country'] = $current_excel->getCell('D' . $row)->getValue();
  324. $data_product['name'] = $current_excel->getCell('E' . $row)->getValue();
  325. $data_product['sku'] = $current_excel->getCell('F' . $row)->getValue();
  326. $data_product['model'] = $current_excel->getCell('G' . $row)->getValue();
  327. $data_product['quantity'] = $current_excel->getCell('H' . $row)->getValue();
  328. $data_product['unit'] = $current_excel->getCell('I' . $row)->getValue();
  329. // 添加前判断
  330. $this->setArrayDefault($data_product);
  331. // 插入到产品信息表
  332. if($m_product->add($data_product) == false) {
  333. $count_fail++;
  334. }else{
  335. $count_success ++;
  336. }
  337. }
  338. echo $count_success.'条记录插入成功';
  339. echo "<br/>".$count_fail.'条记录失败';
  340. echo "<br/>更新".$count_upload.'条记录';
  341. $this->success('添加完成',U('Index/index'),3);
  342. }
  343. }
  344. /**
  345. * 给数组中值为空的键设置默认值
  346. *
  347. */
  348. public function setArrayDefault(&$arr){
  349. foreach($arr as $k => $v){
  350. $arr[$k] = isset($v)?$v:'';
  351. }
  352. return $arr;
  353. }
  354. public function delete($id)
  355. {
  356. // 实例化模型对象
  357. $m_customer_info = M('customer_info');
  358. $m_customer_data = M('customer_data');
  359. $m_old_cooperation = M('old_cooperation');
  360. $m_procurement_plan = M('procurement_plan');
  361. $m_key_order = M('key_order');
  362. $m_r_order = M('r_order_supplier');
  363. $m_r_plan = M('r_plan_product');
  364. $m_activity = M('activity');
  365. $m_r_activity = M('r_activity_product');
  366. $m_our_prepare = M('our_prepare');
  367. // 获取关联customer_info表的activity的id
  368. $res = $m_activity->where('customer_id='.$id)->field('id')->find();
  369. $activity_id = $res['id'];
  370. // 获取关联customer_info表的procurement_plan的id
  371. $res = $m_procurement_plan->where('customer_id='.$id)->field('id')->find();
  372. $procurement_plan_id = $res['id'];
  373. // 获取关联key_order表的r_order_supplier的id
  374. $res = $m_key_order->where('plan_id='.$procurement_plan_id)->field('id')->find();
  375. $key_order_id = $res['id'];
  376. $m_customer_info->where('id='.$id)->delete();
  377. /**
  378. * 判断activity表中是否有关联数据
  379. * 无记录则跳过其他两个关联表的删除操作
  380. */
  381. if(intval($activity_id)){
  382. $m_activity->where('customer_id='.$id)->delete();
  383. $m_our_prepare->where('activity_id='.$activity_id)->delete();
  384. $m_r_activity->where('activity_id='.$activity_id)->delete();
  385. }
  386. /**
  387. * 判断procurement_plan表中是否有关联数据
  388. * 无记录则跳过其他三个关联表的删除操作
  389. */
  390. if(intval($procurement_plan_id)){
  391. $m_procurement_plan->where('customer_id='.$id)->delete();
  392. $m_r_plan->where('plan_id='.$procurement_plan_id)->delete();
  393. $m_key_order->where('plan_id='.$procurement_plan_id)->delete();
  394. $m_r_order->where('order_id='.$key_order_id)->delete();
  395. }
  396. $m_customer_data->where('customer_id='.$id)->delete();
  397. $m_old_cooperation->where('customer_id='.$id)->delete();
  398. }
  399. }
  400. 函数代码:functions.php
  401. function formatData($data){
  402. $data = trim($data);
  403. // 为空则返回默认值
  404. if(empty($data)) return "0000-00-00";
  405. $n = intval(($data - 25569) * 3600 * 24); //转换成1970年以来的秒数
  406. $format_data = gmdate('Y-m-d', $n);//格式化时间
  407. return $format_data;
  408. }

发表评论

表情:
评论列表 (有 0 条评论,436人围观)

还没有评论,来说两句吧...

相关阅读