:::

7-3-2 CSV 匯入後的資料預覽畫面

您沒有觀看影片的權限

您沒有觀看影片的權限

請先登入,登入後,確認您的權限後,即可觀看影片。

  1. 一般匯入CSV檔並寫入資料庫的方法:
    $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();

     

  2. fopen() 用來開啟檔案
  3. fgetcsv() 用來讀取CSV檔的一行
  4. 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);
    
    }

     

  5. 製作 templates\op_tad_signup_data_preview_csv.tpl,去除不要的寫入的資訊,並將可複選的欄位另外處理
  6. 第一行要額外處理,做成標題列。此外,由於多筆資料一起匯入,所以用 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 \


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp22.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D1751%26tbsn%3D48

書籍目錄

展開 | 闔起

線上使用者

361人線上 (157人在瀏覽線上書籍)

會員: 0

訪客: 361

更多…