-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_data.php
More file actions
112 lines (94 loc) · 3.31 KB
/
import_data.php
File metadata and controls
112 lines (94 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
require_once 'config.php';
echo "بدء استيراد البيانات من ملفات CSV...\n\n";
$conn = getDBConnection();
// 1. استيراد بيانات الطلاب
echo "استيراد بيانات الطلاب...\n";
$file = fopen('st_data.csv', 'r');
$header = fgetcsv($file); // تخطي السطر الأول (العناوين)
$count = 0;
while (($data = fgetcsv($file)) !== FALSE) {
$st_num = $data[0];
$st_name = $data[1];
$school = $data[2] ?: null;
$tel = $data[3] ?: null;
$mobail = $data[8] ?: null;
$alsaf = $data[6] ?: null;
$alhai = $data[7] ?: null;
$stmt = $conn->prepare("INSERT IGNORE INTO students (st_num, st_name, school, tel, mobail, alsaf, alhai) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssissis", $st_num, $st_name, $school, $tel, $mobail, $alsaf, $alhai);
$stmt->execute();
$stmt->close();
$count++;
}
fclose($file);
echo "تم استيراد $count طالب\n\n";
// 2. استيراد أنواع المخالفات
echo "استيراد أنواع المخالفات...\n";
$file = fopen('mokalfat.csv', 'r');
$header = fgetcsv($file);
$count = 0;
while (($data = fgetcsv($file)) !== FALSE) {
$id_m = $data[0];
$name1 = $data[1];
$degree = $data[2] ?: 0;
$stmt = $conn->prepare("INSERT IGNORE INTO behavior_types (id_m, name1, degree) VALUES (?, ?, ?)");
$stmt->bind_param("isi", $id_m, $name1, $degree);
$stmt->execute();
$stmt->close();
$count++;
}
fclose($file);
echo "تم استيراد $count نوع مخالفة\n\n";
// 3. استيراد سجلات الحضور
echo "حذف سجلات الحضور السابقة...\n";
$conn->query("TRUNCATE TABLE attendance");
echo "استيراد سجلات الحضور...\n";
$file = fopen('registr.csv', 'r');
$header = fgetcsv($file);
$count = 0;
while (($data = fgetcsv($file)) !== FALSE) {
$dateenter = $data[1];
$time = $data[2] ?: null;
$st_num = $data[3];
$enter_status = $data[4] ?: 1;
// تحويل التاريخ إلى صيغة MySQL
if ($dateenter) {
$dateenter = date('Y-m-d H:i:s', strtotime($dateenter));
}
if ($time) {
$time = date('Y-m-d H:i:s', strtotime($time));
}
$stmt = $conn->prepare("INSERT INTO attendance (dateenter, time, st_num, enter_status) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssi", $dateenter, $time, $st_num, $enter_status);
$stmt->execute();
$stmt->close();
$count++;
}
fclose($file);
echo "تم استيراد $count سجل حضور\n\n";
// 4. استيراد سجلات السلوك
echo "استيراد سجلات السلوك...\n";
$file = fopen('reg_m.csv', 'r');
$header = fgetcsv($file);
$count = 0;
while (($data = fgetcsv($file)) !== FALSE) {
$st_num = $data[1];
$date1 = $data[2];
$id_m = $data[3];
$value1 = $data[4] ?: 0;
// تحويل التاريخ إلى صيغة MySQL
if ($date1) {
$date1 = date('Y-m-d H:i:s', strtotime($date1));
}
$stmt = $conn->prepare("INSERT INTO behavior_records (st_num, date1, id_m, value1) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssii", $st_num, $date1, $id_m, $value1);
$stmt->execute();
$stmt->close();
$count++;
}
fclose($file);
echo "تم استيراد $count سجل سلوك\n\n";
$conn->close();
echo "اكتمل الاستيراد بنجاح!\n";
?>