:::

7-4-1 批次匯入 CSV 報名資料

您沒有觀看影片的權限

您沒有觀看影片的權限

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

  1. index.php 加入對應流程
    //批次匯入 CSV
    case 'tad_signup_data_import_csv':
        Tad_signup_data::import_csv($id);
        redirect_header("{$_SERVER['PHP_SELF']}?id=$id", 3, "成功匯入報名資料!");
        break;

     

  2. 匯入時不需要匯入原有的錄取、報名時間等資訊,所以,修改 class\Tad_signup_data.phppreview_csv(),把不需要的標頭拿掉
    // 預覽 CSV
    public static function preview_csv($action_id)
    {
        /*--略--*/
    
        // 製作標題
        $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]);
            }
        }
    
        $xoopsTpl->assign('head', $head);
        $xoopsTpl->assign('type', $type);
    
        /*--略--*/
    }

     

  3. templates\op_tad_signup_data_preview_csv.tpl 樣板部份,也改成若沒有標頭就不顯示欄位
    <table class="table table-bordered table-sm">
        <!--略-->
            <{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}>
                            <{if $title!=''}>
                                <td>
                                    <!--略-->
                                </td>
                            <{/if}>
                        <{/foreach}>
                    </tr>
                <{/if}>
            <{/foreach}>
        </tbody>
    </table>
    
    

     

  4. class\Tad_signup_data.php 加入對應方法,和 store() 差不多流程,差別在於多筆資料需要用迴圈一筆一筆抽出寫入: 影片中資料庫大小寫不正確,一律改為小寫 prefix("tad_signup_data")
    //批次匯入 CSV
    public static function import_csv($action_id)
    {
        global $xoopsDB, $xoopsUser;
    
        //XOOPS表單安全檢查
        Utility::xoops_security_check();
    
        if (!$_SESSION['can_add']) {
            redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
        }
    
        $action_id = (int) $action_id;
        $uid = $xoopsUser->uid();
    
        $action = Tad_signup_actions::get($action_id);
    
        $TadDataCenter = new TadDataCenter('tad_signup');
    
        foreach ($_POST['tdc'] as $tdc) {
            $sql = "insert into `" . $xoopsDB->prefix("tad_signup_data") . "` (
            `action_id`,
            `uid`,
            `signup_date`,
            `accept`
            ) values(
            '{$action_id}',
            '{$uid}',
            now(),
            '1'
            )";
            $xoopsDB->queryF($sql) or Utility::web_error($sql, __FILE__, __LINE__);
            $id = $xoopsDB->getInsertId();
    
            $TadDataCenter->set_col('id', $id);
            $TadDataCenter->saveCustomData($tdc);
    
            $action['signup'] = self::get_all($action_id);
            if (count($action['signup']) > $action['number']) {
                $TadDataCenter->set_col('data_id', $id);
                $TadDataCenter->saveCustomData(['tag' => ['候補']]);
            }
        }
    }

     

新版提醒:取得標題及類型部份
$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/e265ab10a9232c9d19529b7eaa74a0ca748d8f01 \


:::

搜尋

QR Code 區塊

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

書籍目錄

展開 | 闔起

線上使用者

351人線上 (142人在瀏覽線上書籍)

會員: 0

訪客: 351

更多…