001.
<?php
002.
003.
session_start();
004.
$error
=0;
005.
include
"connect.php"
;
006.
$num_insert
=0;
007.
if
(isset(
$_POST
[
"SubmitForm"
]))
008.
{
009.
$filename
=
strtolower
(
$_FILES
[
'file'
][
'name'
]);
010.
$find
=
strpos
(
$filename
,
'xml'
);
011.
$order
=0;
012.
if
(
$find
===false)
013.
{
014.
echo
"<b><font color='#ff0000'>กรุณา upload file .xml เท่านั้น </font></b>"
;
015.
}
else
016.
{
017.
if
(
$_FILES
[
'file'
][
'error'
] )
018.
{
019.
die
(
"upload error "
);
020.
}
021.
022.
$dom
= DOMDocument::load(
$_FILES
[
'file'
][
'tmp_name'
] );
023.
$rows
=
$dom
->getElementsByTagName(
'Row'
);
024.
$row
= 0;
025.
print_r(
$dom
);
026.
027.
028.
029.
foreach
(
$rows
as
$temp
)
030.
{
031.
$col
= 0;
032.
if
(
$row
==0)
033.
{
034.
$row
++;
continue
;
035.
}
036.
$cells
=
$temp
->getElementsByTagName(
'Cell'
);
037.
foreach
(
$cells
as
$cell
)
038.
{
039.
if
(
$col
==0)
$entity
=
$cell
->nodeValue;
040.
if
(
$col
==1)
$install_date
=
$cell
->nodeValue;
041.
if
(
$col
==2)
$contactno
=
$cell
->nodeValue;
042.
if
(
$col
==3)
$customername
=
$cell
->nodeValue;
043.
if
(
$col
==4)
$address
=
$cell
->nodeValue;
044.
if
(
$col
==5)
$province
=
$cell
->nodeValue;
045.
if
(
$col
==6)
$model
=
$cell
->nodeValue;
046.
if
(
$col
==7)
$serial
=
$cell
->nodeValue;
047.
if
(
$col
==8)
$saleentity
=
$cell
->nodeValue;
048.
if
(
$col
==9)
$from_sale_code
=
$cell
->nodeValue;
049.
if
(
$col
==10)
$from_sale_name
=
$cell
->nodeValue;
050.
if
(
$col
==11)
$remark
=
$cell
->nodeValue;
051.
052.
053.
054.
055.
$col
++;
056.
}
057.
if
(
$entity
!=
''
)
058.
{
059.
$sql_find
=
"select * from rental_info_temp where serial='"
.
$serial
.
"' "
;
060.
$result_find
=sqlsrv_query(
$con
,
$sql_find
);
061.
$num_found
=sqlsrv_has_rows(
$result_find
);
062.
if
(
$num_found
==0||
$num_found
==
""
)
063.
{
064.
065.
$order
++;
066.
$importcode
=
date
(
'YmdHsA'
).
$order
;
067.
if
(
$remark
==
""
)
068.
{
069.
$remark
=
"-"
;
070.
}
071.
$sql
=
"insert into rental_info_temp values('"
.
$entity
.
"',N'"
.
$customername
.
"',N'"
.
$address
.
"',N'"
.
$province
.
"','"
.
$model
.
"','"
.
$serial
.
"','"
.
$install_date
.
"','"
.
$saleentity
.
"',N'"
.
$from_sale_code
.
"',N'"
.
$from_sale_name
.
"','"
.
$contactno
.
"',N'"
.
$remark
.
"','"
.
$importcode
.
"')"
;
072.
$result_query
=sqlsrv_query(
$con
,
$sql
);
073.
074.
if
(!
$result_query
)
075.
{
076.
$error
+=1;
077.
}
else
{
078.
$num_insert
+=1;
079.
}
080.
}
else
{
081.
082.
$sql_update
=
"update rental_info_temp set entity='"
.
$entity
.
"',customername=N'"
.
$customername
.
"',address=N'"
.
$address
.
"',province=N'"
.
$province
.
"',model='"
.
$model
.
"',install_date='"
.
$install_date
.
"',saleentity='"
.
$saleentity
.
"',from_sale_code=N'"
.
$from_sale_code
.
"',from_sale_name=N'"
.
$from_sale_name
.
"',contactno='"
.
$contactno
.
"' where serial='"
.
$serial
.
"'"
;
083.
$result_query
=sqlsrv_query(
$con
,
$sql_update
);
084.
}
085.
}
086.
$row
++;
087.
}
088.
089.
$_SESSION
[
"insert"
]=
$num_insert
;
090.
move_uploaded_file(
$_FILES
[
'file'
][
'tmp_name'
],
"uploads/"
.
$_FILES
[
"file"
][
'name'
]);
092.
}
093.
}
094.
?>
097.
<head>
098.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
/>
099.
<link type=
"text/css"
href=
"css/default.css"
rel=
"stylesheet"
/>
100.
<title>Import Excel File</title>
101.
<script language=
"javascript"
type=
"text/javascript"
>
102.
function
openDocDetail(url,width,height)
103.
{
104.
var
windowWidth=width;
105.
var
windowHeight=height;
106.
var
centerWidth = (window.screen.width - windowWidth) / 2;
107.
var
centerHeight = (window.screen.height - windowHeight) / 2;
108.
OpenWindow=window.open(url,
""
,
"menubar=no,toolbar=no,location=no,scrollbars=yes,status=no,resizable=no,width="
+windowWidth+
",height="
+windowHeight+
",top="
+centerHeight+
",left="
+centerWidth);
109.
}
110.
</script>
111.
</head>
112.
113.
<body>
114.
<p><h2>Import ข้อมูลติดตั้งเครื่องเช่า</h2></p>
115.
<form enctype=
"multipart/form-data"
action=
"import_form.php"
method=
"post"
>
116.
<table width=
"95%"
border=
"0"
align=
"center"
style=
"border-collapse:"
>
117.
<tr>
118.
<td width=
"23%"
>เลือกไฟล์ (นามสกุล .xml) เท่านั้น</td>
119.
<td><input type=
"file"
name=
"file"
/></td>
120.
</tr>
121.
<tr>
122.
<td> </td>
123.
<td> </td>
124.
</tr>
125.
<tr>
126.
<td> </td>
127.
<td><label>
128.
<input type=
"submit"
name=
"SubmitForm"
value=
"Submit"
/>
129.
<input type=
"submit"
name=
"Cancel"
value=
"Cancel"
/>
130.
</label></td>
131.
</tr>
132.
</table>
133.
</form>
134.
<ul id=
"menu"
>
135.
<li><a href=
"#"
onclick=
"javascript:openDocDetail('save_xml.php','1000','500');"
class
=
"txtlink"
>วิธี save file .xml</a></li>
136.
<li><a href=
"upload_format.xls"
class
=
"txtlink"
>รูปแบบไฟล์สำหรับ import</a>
137.
<li><a href=
"#"
onclick=
"javascript:openDocDetail('all_upload.php','1100','500');"
class
=
"txtlink"
>ข้อมูลที่ import แล้ว</a></li>
138.
<li><a href=
"manual/Manual_import.pdf"
class
=
"txtlink"
target=
"_blank"
>คู่มือการ import</a></li>
140.
</ul>
141.
</body>
142.
</html>