|
|
|
import data โดย excel พบปัญหามันทับ data เดิมไปเลยครับ |
|
|
|
|
|
|
|
Code (PHP)
<?php
session_start();
header('Content-Type:text/html; charset=utf8');
require_once('../../includes/config2.php');
require_once('../../includes/database.php');
require_once('../../includes/functions.php');
require_once('excel/oleread.php');
require_once('excel/reader.php');
$action = $_POST['action'];
$act_del = $_GET['act_del'];
if($action == 'import') {
$now = $_POST['year'];
$old = ($now - 1);
if(is_uploaded_file($_FILES["xls_file"]["tmp_name"]))
{
$ext = strtolower(pathinfo($_FILES["xls_file"]["name"], PATHINFO_EXTENSION));
$allow = array('xls');
if(in_array($ext,$allow))
{
$file_name = md5($_FILES["xls_file"]["name"] . date('Y-m-d H:i:s'));
$uploadPath = $config['report_xls_up'] . $file_name . ".xls";
$tempPath = $_FILES["xls_file"]["tmp_name"];
move_uploaded_file($tempPath, $uploadPath);
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('utf-8');
$data->read($uploadPath);
error_reporting(E_ALL ^ E_NOTICE);
$commands = array();
$abbrev = NULL;
$abbreviations = array();
$error = FALSE;
for($row = 2; $row <= $data->sheets[0]['numRows']; $row++)
{
$avg_now = (float) str_replace(',','',$data->sheets[0]['cells'][$row][6]);
$avg_old = (float) str_replace(',','',$data->sheets[0]['cells'][$row][7]);
$avg_target = (float) str_replace(',','',$data->sheets[0]['cells'][$row][8]);
$growthYear = 0;
$growthTarget = 0;
if($avg_now != 0 AND $avg_old != 0 AND $avg_target != 0)
{
$growthYear = (float) ((($avg_now - $avg_old) / $avg_old) * 100);
$growthTarget = (float) ((($avg_now - $avg_target) / $avg_target) *100);
}
elseif($avg_now == 0 AND $avg_old != 0 AND $avg_target != 0)
{
$growthYear = 0;
$growthTarget = 0;
}
$SQL = "INSERT INTO shop_ranking (id, shop_name, shop_id, val_now_year, val_old_year, avg_now_year, avg_old_year, avg_target, growth_year, growth_target, now_year, old_year, abbreviation, post_by, import_date, hilight) VALUES('',";
$SQL .= "'" . $data->sheets[0]['cells'][$row][2] . "',";
$SQL .= "'" . str_replace(',','',$data->sheets[0]['cells'][$row][3]) . "',";
$SQL .= "'" . str_replace(',','',$data->sheets[0]['cells'][$row][4]) . "',";
$SQL .= "'" . str_replace(',','',$data->sheets[0]['cells'][$row][5]) . "',";
$SQL .= "'" . $avg_now . "',";
$SQL .= "'" . $avg_old . "',";
$SQL .= "'" . $avg_target . "',";
$SQL .= "'" . $growthYear . "',";
$SQL .= "'" . $growthTarget . "',";
$SQL .= "'" . $now . "',";
$SQL .= "'" . $old . "',";
$SQL .= "'" . $data->sheets[0]['cells'][$row][1] . "',";
$SQL .= "'" . $_SESSION['useradmin'] . "',";
$SQL .= "'" . date('Y-m-d H:i:s') . "',";
$SQL .= "'0'";
$SQL .= ");";
$commands[] = $SQL;
}
@unlink($uploadPath);
//Truncate `shop_ranking` before insert new data
mysql_query("TRUNCATE TABLE shop_ranking");
foreach($commands as $command) {
if( ! mysql_query($command)) {
$error = TRUE;
break;
}
//echo "<pre>" . $command . "</pre>";
}
if($error){
re_transection($abbreviations,$now,$old);
} else {
echo redirect('form_import.php?msg=import_success');
}
} else {
echo redirect('form_import.php?msg=extension_error');
}
} else {
echo redirect('form_import.php?msg=upload_error');
}
}
?>
|
|
|
|
|
Date :
2012-03-12 10:30:16 |
By :
มือใหม่ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ปัญหา คือ มันลบ data เก่าแล้ว insert data ไปใหม่อะครับ อันเก่าหายหมด
อยากให้ import โดย data เก่าไม่หายอะครับ
|
|
|
|
|
Date :
2012-03-12 10:32:07 |
By :
มือใหม่ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แล้วครับ ขอบคุณครับ ><
|
|
|
|
|
Date :
2012-03-12 11:26:34 |
By :
มือใหม่ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้แบบไหนและด้วยงิธีการยังงัยช่วยรบกวนบอกสมาชิกให้ทราบขั้นตอนด้วยนะครับ ขอบคุณมากครับ
|
|
|
|
|
Date :
2012-03-12 12:39:40 |
By :
prach_kp |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|