 |
นำเข้า import excel csv to mysql ไม่เป็นภาษาไทยครับ |
|
 |
|
|
 |
 |
|
ไฟล์ csv ภาษาไทย พอนำเข้าแล้วภาษาต่างดาวหมดเลย
หรืออาจารย์ท่านใดมี ฟอร์มตัวอย่างการนำเข้าบ้างครับ ในเวปเป็นโค๊ดเก่าหมดแล้ว
ขอบคุณครับ
Code (PHP)
<?php
namespace Phppot;
use Phppot\DataSource;
require_once __DIR__ . '/lib/UserModel.php';
$userModel = new UserModel();
if (isset($_POST["import"])) {
$response = $userModel->readUserRecords();
}
?>
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" charset=tis-620/>
<script type="text/javascript">
function validateFile() {
var csvInputFile = document.forms["frmCSVImport"]["file"].value;
if (csvInputFile == "") {
documemt.getElementById("response").innerHTML = "No source found to import";
return false;
}
return true;
}
</script>
</head>
<body>
<h2>Import CSV file into Mysql using PHP</h2>
<div class="outer-scontainer">
<div class="row">
<form class="form-horizontal" action="" method="post"
name="frmCSVImport" id="frmCSVImport" enctype="multipart/form-data"
onsubmit="return validateFile()">
<div Class="input-row">
<label>Choose your file. <a href="./import-template.csv" download>Download
template</a></label><input type="file" name="file" id="file"
class="file" accept=".csv,.xls,.xlsx">
<div class="import">
<button type="submit" id="submit" name="import" class="btn-submit">Import</button>
</div>
</div>
</form>
</div>
<div id="response"
class="<?php if(!empty($response["type"])) { echo $response["type"] ; } ?>">
<?php if(!empty($response["message"])) { echo $response["message"]; } ?>
</div><?php require_once __DIR__ . '/list.php';?></div>
</body>
</html>
Code (PHP)
<?php
namespace Phppot;
use Phppot\DataSource;
class UserModel
{
private $conn;
function __construct()
{
require_once 'DataSource.php';
$this->conn = new DataSource();
}
function getAllUser()
{
$sqlSelect = "SELECT * FROM customer";
$result = $this->conn->select($sqlSelect);
return $result;
}
function readUserRecords()
{
$fileName = $_FILES["file"]["tmp_name"];
if ($_FILES["file"]["size"] > 0) {
$file = fopen($fileName, "r");
$importCount = 0;
while (($column = fgetcsv($file, 10000, ",")) !== FALSE) {
if (! empty($column) && is_array($column)) {
if ($this->hasEmptyRow($column)) {
continue;
}
if (isset($column[1], $column[3], $column[4])) {
$DateCall = $column[1];
$NumberCall = $column[2];
$CustomerName = $column[3];
$CarDetail = $column[4];
$insertId = $this->insertUser($DateCall, $NumberCall, $CustomerName, $CarDetail);
if (! empty($insertId)) {
$output["type"] = "success";
$output["message"] = "Import completed.";
$importCount ++;
}
}
} else {
$output["type"] = "error";
$output["message"] = "Problem in importing data.";
}
}
if ($importCount == 0) {
$output["type"] = "error";
$output["message"] = "Duplicate data found.";
}
return $output;
}
}
function hasEmptyRow(array $column)
{
$columnCount = count($column);
$isEmpty = true;
for ($i = 0; $i < $columnCount; $i ++) {
if (! empty($column[$i]) || $column[$i] !== '') {
$isEmpty = false;
}
}
return $isEmpty;
}
function insertUser($DateCall, $NumberCall, $CustomerName, $CarDetail)
{
$sql = "SELECT CustomerName FROM customer WHERE CustomerName = ?";
$paramType = "s";
$paramArray = array(
$DateCall
);
$result = $this->conn->select($sql, $paramType, $paramArray);
$insertId = 0;
if (empty($result)) {
$hashedPassword = password_hash($NumberCall, PASSWORD_DEFAULT);
$sql = "INSERT into customer (DateCall,NumberCall,CustomerName,CarDetail)
values (?,?,?,?)";
$paramType = "ssss";
$paramArray = array(
$DateCall,
$NumberCall,
$CustomerName,
$CarDetail
);
$insertId = $this->conn->insert($sql, $paramType, $paramArray);
}
return $insertId;
}
}
?>
Code (PHP)
<?php
/**
* Copyright (C) 2019 Phppot
*
* Distributed under MIT license with an exception that,
* you don’t have to include the full MIT License in your code.
* In essense, you can use it on commercial software, modify and distribute free.
* Though not mandatory, you are requested to attribute this URL in your code or website.
*/
namespace Phppot;
/**
* Generic datasource class for handling DB operations.
* Uses MySqli and PreparedStatements.
*
* @version 2.5 - recordCount function added
*/
class DataSource
{
// PHP 7.1.0 visibility modifiers are allowed for class constants.
// when using above 7.1.0, declare the below constants as private
const HOST = 'localhost';
const USERNAME = 'root';
const PASSWORD = '';
const DATABASENAME = 'dbname';
private $conn;
/**
* PHP implicitly takes care of cleanup for default connection types.
* So no need to worry about closing the connection.
*
* Singletons not required in PHP as there is no
* concept of shared memory.
* Every object lives only for a request.
*
* Keeping things simple and that works!
*/
function __construct()
{
$this->conn = $this->getConnection();
}
/**
* If connection object is needed use this method and get access to it.
* Otherwise, use the below methods for insert / update / etc.
*
* @return \mysqli
*/
public function getConnection()
{
$conn = new \mysqli(self::HOST, self::USERNAME, self::PASSWORD, self::DATABASENAME);
if (mysqli_connect_errno()) {
trigger_error("Problem with connecting to database.");
}
$conn->set_charset("utf8");
return $conn;
}
/**
* To get database results
*
* @param string $query
* @param string $paramType
* @param array $paramArray
* @return array
*/
public function select($query, $paramType = "", $paramArray = array())
{
$stmt = $this->conn->prepare($query);
if (! empty($paramType) && ! empty($paramArray)) {
$this->bindQueryParams($stmt, $paramType, $paramArray);
}
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$resultset[] = $row;
}
}
if (! empty($resultset)) {
return $resultset;
}
}
/**
* To insert
*
* @param string $query
* @param string $paramType
* @param array $paramArray
* @return int
*/
public function insert($query, $paramType, $paramArray)
{
$stmt = $this->conn->prepare($query);
$this->bindQueryParams($stmt, $paramType, $paramArray);
$stmt->execute();
$insertId = $stmt->insert_id;
return $insertId;
}
/**
* To execute query
*
* @param string $query
* @param string $paramType
* @param array $paramArray
*/
public function execute($query, $paramType = "", $paramArray = array())
{
$stmt = $this->conn->prepare($query);
if (! empty($paramType) && ! empty($paramArray)) {
$this->bindQueryParams($stmt, $paramType, $paramArray);
}
$stmt->execute();
}
/**
* 1.
* Prepares parameter binding
* 2. Bind prameters to the sql statement
*
* @param string $stmt
* @param string $paramType
* @param array $paramArray
*/
public function bindQueryParams($stmt, $paramType, $paramArray = array())
{
$paramValueReference[] = &$paramType;
for ($i = 0; $i < count($paramArray); $i ++) {
$paramValueReference[] = &$paramArray[$i];
}
call_user_func_array(array(
$stmt,
'bind_param'
), $paramValueReference);
}
/**
* To get database results
*
* @param string $query
* @param string $paramType
* @param array $paramArray
* @return array
*/
public function getRecordCount($query, $paramType = "", $paramArray = array())
{
$stmt = $this->conn->prepare($query);
if (! empty($paramType) && ! empty($paramArray)) {
$this->bindQueryParams($stmt, $paramType, $paramArray);
}
$stmt->execute();
$stmt->store_result();
$recordCount = $stmt->num_rows;
return $recordCount;
}
}
หาทางแก้ไม่ถูกเลยครับ ไฟล์ csv ภาษาไทย พอนำเข้าแล้วภาษาต่างดาวหมดเลย
Tag : PHP, MySQL
|
|
 |
 |
 |
 |
Date :
2023-06-21 13:26:43 |
By :
sweerawat |
View :
716 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
encode ข้อความก่อนนำลง database ครับ
Code (PHP)
$message = "สวัสดีครับ ผมมาจากไทยครีเอทดอทคอม";
$tis620 = iconv("utf-8", "tis-620", $message );
$utf8 = iconv("tis-620", "utf-8", $tis620 );
//นำ $uft8 ไปใช้
|
 |
 |
 |
 |
Date :
2023-06-26 14:20:00 |
By :
mongkon.k |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|