$handle = fopen($_FILES['上傳名稱']['tmp_name'], "r") or die("無法開啟");
while (($val = fgetcsv($handle, 1000)) !== false) {
$sql = "insert into `" . $xoopsDB->prefix("資料表") . "` (`欄1`, `欄2` `欄3`, ...) values('{$val[0]}', '{$val[1]}', '{$val[2]}', ...)";
$xoopsDB->queryF($sql) or Utility::web_error($sql, __FILE__, __LINE__);
}
fclose($handle);
$id = $xoopsDB->getInsertId();
class\Tad_signup_data.php
加入對應方法
// 預覽 CSV
public static function preview_csv($action_id)
{
global $xoopsTpl;
if (!$_SESSION['can_add']) {
redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
}
$action = Tad_signup_actions::get($action_id);
$xoopsTpl->assign('action', $action);
// 製作標題
$head_row = explode("\n", $action['setup']);
$head = $type = [];
foreach ($head_row as $head_data) {
$cols = explode(',', $head_data);
if (strpos($cols[0], '#') === false) {
$head[] = str_replace('*', '', trim($cols[0]));
$type[] = trim($cols[1]);
}
}
$head[] = '錄取';
$head[] = '報名日期';
$head[] = '身份';
$xoopsTpl->assign('head', $head);
$xoopsTpl->assign('type', $type);
// 抓取內容
$preview_data = [];
$handle = fopen($_FILES['csv']['tmp_name'], "r") or die("無法開啟");
while (($val = fgetcsv($handle, 1000)) !== false) {
$preview_data[] = mb_convert_encoding($val, 'UTF-8', 'Big5');
}
fclose($handle);
$xoopsTpl->assign('preview_data', $preview_data);
}
templates\op_tad_signup_data_preview_csv.tpl
,去除不要的寫入的資訊,並將可複選的欄位另外處理name="tdc[<$i>][<{$title}>]"
二維陣列的方式來放資料
<h2 class="my">匯入「<{$action.title}>」報名資料預覽</h2>
<form action="index.php" method="post" id="myForm">
<table class="table table-bordered table-sm">
<thead>
<tr>
<{foreach from=$head item=title}>
<th><{$title}></th>
<{/foreach}>
</tr>
</thead>
<tbody>
<{foreach from=$preview_data key=i item=data name=preview_data}>
<{if $smarty.foreach.preview_data.iteration > 1}>
<tr>
<{foreach from=$data key=j item=val}>
<{assign var=title value=$head.$j}>
<{assign var=input_type value=$type.$j}>
<td>
<{if $input_type=="checkbox"}>
<{assign var=val_arr value='|'|explode:$val}>
<{foreach from=$val_arr item=val}>
<div class="form-check-inline checkbox-inline">
<label class="form-check-label">
<input class="form-check-input" type="checkbox" name="tdc[<{$i}>][<{$title}>][]" value="<{$val}>" checked>
<{$val}>
</label>
</div>
<{/foreach}>
<{else}>
<input type="text" name="tdc[<{$i}>][<{$title}>]" value="<{$val}>" class="form-control form-control-sm">
<{/if}>
</td>
<{/foreach}>
</tr>
<{/if}>
<{/foreach}>
</tbody>
</table>
<{$token_form}>
<input type="hidden" name="id" value="<{$action.id}>">
<input type="hidden" name="op" value="tad_signup_data_import_csv">
<div class="bar">
<button type="submit" class="btn btn-primary">
<i class="fa fa-save" aria-hidden="true"></i> 匯入CSV資料
</button>
</div>
</form>
$head_row = explode("\n", $action['setup']);
$head = $type = [];
foreach ($head_row as $head_data) {
$cols = explode(',', $head_data);
if (strpos($cols[0], '#') === false) {
$head[] = str_replace('*', '', trim($cols[0]));
$type[] = trim($cols[1]);
}
}
可以改用下列方式更簡單喔:
$TadDataCenter = new TadDataCenter('tad_signup');
$head = $TadDataCenter->getAllColItems($action['setup']);
$type = $TadDataCenter->getAllColItems($action['setup'], 'type');
tadtools/class/TadDataCenter.php 必須是 2021/10/29 日以後的版本,可至此下載覆蓋
link to https://github.com/tadlearn/tad_signup/commit/c910020575d1a9a796c21b080fe4a5b810de4fa8 \