/** * Import profs data from CSV file */ function import_profs() { global $db; echo "Importing Profs data\n"; try { $rows = read_datafile(DIR_DATA . "enseignants.csv"); } catch (Exception $e) { exit_error($e->getMessage()); } $sql_insert_prof = 'INSERT INTO ' . TBL_PROFS . ' (titre, nom, prenom, email) VALUES (:titre, :nom, :prenom, :email) ON DUPLICATE KEY UPDATE titre = :titre, nom = :nom, prenom = :prenom, email = :email'; $qry_insert_prof = $db->prepare($sql_insert_prof); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); foreach ($rows as $id => $row) { $data = explode(',', $row); // Case conversion array_walk($data, function (&$val, $key) { $case = $key == 3 ? MB_CASE_LOWER : MB_CASE_TITLE; $val = mb_convert_case($val, $case); }); // Insert data $qry_insert_prof->bindParam(':titre', $data[0]); $qry_insert_prof->bindParam(':prenom', $data[1]); $qry_insert_prof->bindParam(':nom', $data[2]); $qry_insert_prof->bindParam(':email', $data[3]); $qry_insert_prof->execute(); } echo PHP_EOL; }
<?php /** * Using the classes list provided by school, update the database with any * missing or incorrect * - class information * - grade */ include 'core.php'; // Retrieve reference data $filename = DIR_DATA . "classes_list_20151030.csv"; $classes_list = read_datafile($filename); // Classes lookup table echo "Building classes lookup table\n"; $sql = 'SELECT id, nom FROM ' . VIEW_CLASSES; foreach ($db->query($sql) as $classe) { extract($classe, EXTR_PREFIX_ALL, 'cl'); $classes_lookup[$cl_nom] = $cl_id; } // Prepare Update query $sqlUpdChild = 'UPDATE ' . TBL_ENFANTS . ' SET degre_id = :degre, classe_id = :classe_id WHERE id = :id'; $qryUpdChild = $db->prepare($sqlUpdChild); // Read children table echo "Processing children\n"; $sql = 'SELECT * FROM ' . VIEW_ENFANTS . ' ORDER BY nom, prenom'; $count_upd = $count_err = 0; foreach ($db->query($sql) as $child) { extract($child, EXTR_PREFIX_ALL, 'child'); $msg = '';