I need to read Excel data and save into database, but ran in the problem below:
Excel format :
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.
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.