dsxon40042 2015-01-03 11:58
浏览 48

如何连接不同行的单行列中的列

I need to read Excel data and save into database, but ran in the problem below:

Excel format : Spreadsheet

I need to save data in the database but the problem is that in message column some column start as [1/2-PNR], which means it has a long message that is divided into two parts as in [2/2-PNR]

But that message need to save in a single column

same as if message like [1/3-ADF] that is the message is divided into three parts and need to save in the database in single column

I tried saving the data with the code below, but unable to do so.

I used this zip file to read excel https://code.google.com/p/php-excel-reader/downloads/detail?name=php-excel-reader-2.21.zip

<?php
//error_reporting(0);
$host="localhost";
$username="root";
$password="";
$db_name="abro";

mysql_connect("$host", "$username", "$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select db");
ini_set("display_errors",1);
error_reporting(0);
require_once 'excel_reader2.php';
 $date=date('d-M-Y',strtotime("-1 days"));
 $actual_file=$date.".xls";
$xls = new Spreadsheet_Excel_Reader($actual_file);
$success=0;


for ($row=1;$row<=$xls->rowcount();$row++)
{
     $mobile_no=$xls->val($row,1);
     $party_name=$xls->val($row,2);
      $message=$xls->val($row,3);

     if($row==1)
     {

     }
     else{
         $query="insert into party_excel set
                                party_name='".mysql_real_escape_string($party_name)."',
                                message='".mysql_real_escape_string($message)."',
                                moble_no='".mysql_real_escape_string($mobile_no)."'
                                  ";
        $query_run=mysql_query($query);
        echo mysql_error();
        $success++;                            
     }


 }
 if($success>0)
 {
     echo "file imported successfully";
 } 

Actually this sheet is used to save SMS messages. If the SMS exceeds 160 characters then the remaining message is saved in next row. But when I show the message, I want to show the message on the same row.

  • 写回答

1条回答 默认 最新

  • dongzhang6544 2015-01-03 12:17
    关注

    Initially, because the image is hard to read, I thought you would have as many columns as parts in the message, but I realize now you have as many rows as parts.

    I think the best is really to process the data in PHP, as follows:

    $part = 1;
    $parts = 1;
    for ($row=2; $row<=$xls->rowcount(); $row++) {
         $mobile_no=$xls->val($row,1);
         $party_name=$xls->val($row,2);
         if ($part < $parts) { // need to merge rows
            $message = $message . $xls->val($row,3);
            $part++;
         }
         else { // a new message
              $message = $xls->val($row,3);
              $pattern = "/^\[()\/()-" . $party_name . "\]/";
              $part = 1;
              if (preg_match($pattern, $message, $matches)) {
                 $parts = $matches[2];
              }
              else $parts = 1;
         }
         if ($part == $parts) { // completed a message
            $query="insert into party_excel set
                        party_name='".mysql_real_escape_string($party_name)."',
                        message='".mysql_real_escape_string($message)."',
                        moble_no='".mysql_real_escape_string($mobile_no)."'
                                      ";
            $query_run=mysql_query($query);
            echo mysql_error();
            $success++;                            
         }
    }
    

    I have adjusted your loop to start on 2 since you are discarded line 1. I have merged the multi-part messages as they are, i.e., I didn't remove the [1/2-SJF] indicators.

    Also, I would rather output the lines to a CSV and then use LOAD DATA INFILE to insert all the rows in one go by mysql itself: http://dev.mysql.com/doc/refman/5.1/en/load-data.html

    Disclaimer: this code has not been tested, specially double check the $pattern.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?