I have a php program that reads excel file and returns the variable perfectly. The variable are as shown below
$str = $jdate . ",";
$str .= $block . ",";
$str .= $a_harv . ",";
$str .= $b_harv . ",";
$str .= $loss . ",";
$str .= $sweet . ",";
$str .= $species . ",";
$str .= $comment . "
";
Now I would to send these variable to another php program(the program sends the variables to data base, it also has no problem) to receive/get the variables as:
list($sdate,$block,$a_harv,$b_harv,$loss,$sweet,$species,$comment) =
explode(",",$_GET['$str']);
I wonder how to connect these two programs?
The first is this readExcel_SendResult.php
<?php
date_default_timezone_set('Asia/Tokyo');
global $link, $db;
include('db.inc');
include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel.php' );
include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php' );
function endsWith( $str, $suffix ){
$len = strlen( $suffix );
return $len == 0 || substr( $str, strlen( $str ) - $len, $len ) ===
$suffix;
}
$excelfilename = 'harv.xlsx';
$excelfilepath = dirname( __FILE__ ) . '/' . $excelfilename;
$reader = null;
if( endsWith( $excelfilename, 'xls' ) ){
$reader = PHPExcel_IOFactory::createReader( 'Excel5' );
}else if( endsWith( $excelfilename, 'xlsx' ) ){
$reader = PHPExcel_IOFactory::createReader( 'Excel2007' );
}
if( $reader ){
$excel = $reader->load( $excelfilepath );
$obj1 = $excel->getActiveSheet()->toArray( null, true, true, true );
$num_row = count($obj1);
print "Variable Count:" . $num_row . "
";
for($i=0; $i<$num_row; $i++){
$sdate = trim($obj1[$i]['A']);
if($sdate == "") continue;
$block = str_replace(" ","",$obj1[$i]['B']);
$block = mb_convert_kana($block,"r","utf-8");
$a_harv = $obj1[$i]['C'];
$b_harv = $obj1[$i]['D'];
$loss = $obj1[$i]['E'];
$sweet = trim($obj1[$i]['F']);
$species = str_replace(" ","",$obj1[$i]['G']);
$comment = trim($obj1[$i]['H']);
list($mm, $dd, $yy) = explode("/",$sdate);
if($yy == "") continue;
$jdate = $yy . "-" . $mm . "-" . $dd;
$str = $jdate . ",";
$str .= $block . ",";
$str .= $a_harv . ",";
$str .= $b_harv . ",";
$str .= $loss . ",";
$str .= $sweet . ",";
$str .= $species . ",";
$str .= $comment . "
";
$shell .= "?ARG=" . $str;
}
}else{
echo "No reader.";
}
?>
The other program I have is this; send_datatoDB.php
global $link, $db;
include'excel_db.php';
include('db.inc');
list($sdate,$block,$a_harv,$b_harv,$loss,$sweet,$species,$comment) =
explode(",",$_GET['$str']);
$block = mb_convert_kana($block,"r","utf-8");
if($species == "type1"){
$vid = "v2";
} else if ($species == "type2"){
$vid = "v3";
} else if ($species == "type3"){
$vid = "v1";
} else {
$vid = "v4";
}
if(is_numeric($sweet)){
$sweetness = $sweet;
$s_and_c = $comment;
} else {
$sweetness = 0.0;
$s_and_c = $sweet . $comment;
}
connect_mysql($link, $db);
$sql = "Select * from tableName where sdate = '{$sdate}' and block = '{$block}' and vid = '{$vid}'";
$result_id = mysqli_query($link,$sql);
if($result_id == false){
exit();
}
$row_cnt = mysqli_num_rows($result_id);
echo "Result Number:" . $row_cnt . "<br>";
while ($row = mysqli_fetch_assoc($result_id)) {
$hid = $row['hid'];
//echo "hid = " . $hid . "<br>";
}
mysqli_close($link);
connect_mysql($link, $db);
if($row_cnt < 1){ //INSERT
$values = "'{$sdate}' , '{$block}' , '{$vid}', '{$a_harv}',
'{$b_harv}', '{$loss}', '{$sweetness}', '{$s_and_c}'";
$sql = "INSERT into tableNamw ";
$sql .= "(`sdate`, `block`, `vid`, `A_harv`, `B_harv`, `loss`,
`sweetness`, `comments`) ";
$sql .= " VALUES ({$values})";
//echo $sql;
$result_id = mysqli_query($link,$sql);
if($result_id == false){
exit();
}
} else { //UPDATE
$sql = "UPDATE tableName SET ";
$sql .= "a_harv = '" . $a_harv . "', ";
$sql .= "b_harv = '" . $b_harv . "', ";
$sql .= "loss = '" . $loss . "', ";
$sql .= "sweetness = '" . $sweetness . "', ";
$sql .= "comments = '" . $s_and_c . "'";
$sql .= " where hid = " . $hid;
//echo $sql . "<br>";
$result_id = mysql_query($sql,$link);
if($result_id == false){
//echo "<BR>Uooops<BR>";
exit();
}
}
mysqli_close($link);
?>
You can pass data from one page to another page by using session and Cookies
Session:
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Cookie:
//One page 1
$_COOKIE['varname'] = $var_value;
//On page 2
$var_value = $_COOKIE['varname'];
Your code is missing a lot of stuff. Instead of making two file put you code in one file and make some changes according to the following code.
Throw all your data from excel to an array by
$rec = array();
$rec['jdate'] = $jdate;
$rec['block'] = $block;
$rec['a_harv'] = $a_harv;
$rec['b_harv'] = $b_harv;
$rec['loss'] = $loss;
$rec['sweet'] = $sweet;
$rec['species'] = $species;
$rec['comment'] = $comment;
$arr[] = $rec;
Then
if(count($arr) > 0){
foreach ($arr as $key => $row) {
$row['block'] = mb_convert_kana($row['block'],"r","utf-8");
if($row['species'] == "type1"){
$vid = "v2";
} else if ($row['species'] == "type2"){
$vid = "v3";
} else if ($row['species'] == "type3"){
$vid = "v1";
} else {
$vid = "v4";
}
if(is_numeric($sweet)){
$sweetness = $sweet;
$s_and_c = $comment;
} else {
$sweetness = 0.0;
$s_and_c = $sweet . $comment;
}
$sql = "Select * from tableName where sdate = '{$row['sdate']}' and block = '{$row['block']}' and vid = '{$vid}'";
$result_id = mysqli_query($link,$sql);
if($result_id == false){
exit();
}
$row_cnt = mysqli_num_rows($result_id);
echo "Result Number:" . $row_cnt . "<br>";
while ($r = mysqli_fetch_assoc($result_id)) {
$hid = $r['hid'];
//echo "hid = " . $hid . "<br>";
}
if($row_cnt < 1){ //INSERT
$values = "'{$row['sdate']}' , '{$row['block']}' , '{$row['vid']}', '{$row['a_harv']}', '{$row['b_harv']}', '{$row['loss']}', '{$row['sweetness']}', '{$row['s_and_c']}'";
$sql = "INSERT into tableNamw ";
$sql .= "(`sdate`, `block`, `vid`, `A_harv`, `B_harv`, `loss`, `sweetness`, `comments`) ";
$sql .= " VALUES ({$values})";
//echo $sql;
$result_id = mysqli_query($link,$sql);
if($result_id == false){
exit();
}
}
else { //UPDATE
$sql = "UPDATE tableName SET ";
$sql .= "a_harv = '" . $row['a_harv'] . "', ";
$sql .= "b_harv = '" . $row['b_harv'] . "', ";
$sql .= "loss = '" . $row['loss'] . "', ";
$sql .= "sweetness = '" . $row['sweetness'] . "', ";
$sql .= "comments = '" . $row['s_and_c'] . "'";
$sql .= " where hid = " . $row['hid'];
//echo $sql . "<br>";
$result_id = mysql_query($sql,$link);
if($result_id == false){
//echo "<BR>Uooops<BR>";
exit();
}
}
}
}
The edited code is below. I have also include a test mysql database for testing and the data.
date_default_timezone_set('Asia/Tokyo');
global $link, $db;
include('db2.inc');
include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel.php' );
include_once( dirname( __FILE__ ) . '/PHPExcel-1.8/Classes/PHPExcel/IOFactory.php' );
function endsWith( $str, $suffix ){
$len = strlen( $suffix );
return $len == 0 || substr( $str, strlen( $str ) - $len, $len ) === $suffix;
}
$excelfilename = 'harv2.xlsx';
$excelfilepath = dirname( __FILE__ ) . '/' . $excelfilename;
$reader = null;
if( endsWith( $excelfilename, 'xls' ) ){
$reader = PHPExcel_IOFactory::createReader( 'Excel5' );
}else if( endsWith( $excelfilename, 'xlsx' ) ){
$reader = PHPExcel_IOFactory::createReader( 'Excel2007' );
}
if( $reader ){
$excel = $reader->load( $excelfilepath );
$obj1 = $excel->getActiveSheet()->toArray( null, true, true, true );
$num_row = count($obj1);
print "Data Number:" . $num_row . "
";
for($i=0; $i<$num_row; $i++){
$sdate = trim($obj1[$i]['A']);
if($sdate == "") continue;
$block = str_replace(" ","",$obj1[$i]['B']);
$block = mb_convert_kana($block,"r","utf-8");
$a_harv = $obj1[$i]['C'];
$b_harv = $obj1[$i]['D'];
$loss = $obj1[$i]['E'];
$sweet = trim($obj1[$i]['F']);
$species = str_replace(" ","",$obj1[$i]['G']);
$comment = trim($obj1[$i]['H']);
list($mm, $dd, $yy) = explode("/",$sdate);
if($yy == "") continue;
$jdate = $yy . "-" . $mm . "-" . $dd;
$rec = array();
$rec['jdate'] = $jdate;
$rec['block'] = $block;
$rec['a_harv'] = $a_harv;
$rec['b_harv'] = $b_harv;
$rec['loss'] = $loss;
$rec['sweet'] = $sweet;
$rec['species'] = $species;
$rec['comment'] = $comment;
$arr[] = $rec;
}
}else{
echo "No reader.";
}
if(count($arr) > 0){
foreach ($arr as $key => $row) {
$row['block'] = mb_convert_kana($row['block'],"r","utf-8");
if($row['species'] == "アイコ"){
$vid = "v2";
} else if ($row['species'] == "チャペル"){
$vid = "v3";
} else if ($row['species'] == "朱雀"){
$vid = "v1";
} else {
$vid = "v4";
}
if($row['sweet']){
$sweetness = preg_replace("/[^0-9,.]/", '', $row['sweet']) . "
";
$s_and_c = $row['comment'];
} else {
$sweetness = 0.0;
$s_and_c = $row['sweetness'] . $row['comment'];
}
connect_mysql($link, $db);
$sql = "Select * from toguchi_harv where sdate = '{$row['sdate']}' and block = '{$row['block']}' and vid = '{$vid}'";
# echo $sql . "<br>";
$result_id = mysqli_query($link,$sql);
if($result_id == false){
exit();
}
$row_cnt = mysqli_num_rows($result_id);
echo "Result Number:" . $row_cnt . "<br>";
while ($r = mysqli_fetch_assoc($result_id)) {
$hid = $r['hid'];
echo "hid = " . $hid . "<br>";
}
mysqli_close($link);
connect_mysql($link, $db);
if($row_cnt < 1){//INSERT
$values = "'{$row['jdate']}' , '{$row['block']}' , '{$vid}', '{$row['a_harv']}', '{$row['b_harv']}', '{$row['loss']}', '{$sweetness}', '{$s_and_c}'";
var_dump($values);
$sql = "INSERT into toguchi_harv ";
$sql .= "(`sdate`, `block`, `vid`, `A_harv`, `B_harv`, `loss`, `sweetness`, `comments`) ";
$sql .= " VALUES ({$values})";
//echo $sql;
$result_id = mysqli_query($link,$sql);
if($result_id == false){
exit();
}
} else {
$sql = "UPDATE toguchi_harv SET sdate=?, a_harv=?, b_harv=?,
loss=?,sweetness=?,comments=? WHERE hid=?";
if($stmt == mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "isssiiiis", $param_hid,$param_sdate,$param_block, $param_vid,$param_a_harv,$param_b_harv,$param_loss,
$param_sweetness,$param_s_and_c);
//set parameters
$param_hid = $hid;
$param_sdate = $row['jdate'];
$param_block = $row['block'];
$param_vid = $vid;
$param_a_harv = $row['a_harv'];
$param_b_harv = $row['b_harv'];
$param_loss = $row['loss'];
$param_sweetness = $sweetness;
$param_s_and_c = $s_and_c;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
echo "Updated";
exit();
} else{
echo "Something went wrong.";
}
}
mysqli_close($link);
}
}
}
?>
The free mysql data base Server: sql12.freemysqlhosting.net Name: sql12238577 Username: sql12238577 Password: L8aEhbgPNP Port number: 3306
And this is the include db2.inc
function connect_mysql(&$link, &$db){
global $link;
$link = mysqli_connect('sql12.freemysqlhosting.net', 'sql12238577',
'L8aEhbgPNP');
if(!$link){
print "MySQL connection failed!";
exit();
}
$db = mysqli_select_db($link,'test_DB');
if (!$db){
die('DB connection failed!'.mysql_error());
exit();
}
mysqli_set_charset($link,'utf8');
return true;
}
?>
Sample data is in this link https://tokyonodai-my.sharepoint.com/:x:/g/personal/45618006_nodai_ac_jp/EaB74JirQUdJn7OAZ86IQPYBlADKBoHWax-x4_-1pf5rkQ?e=X6dIQ7