false, 'msg' => 'invalid c_uid']); exit; } $rows = json_decode($_POST['data'] ?? '[]', true); if (!is_array($rows)) $rows = []; // validate + clean $clean = []; foreach ($rows as $r) { $dow = isset($r['dow']) ? intval($r['dow']) : -1; if ($dow < 0 || $dow > 6) continue; $open = trim($r['open'] ?? ''); if (!preg_match('/^\d{2}:\d{2}$/', $open)) continue; // start time required $close = trim($r['close'] ?? ''); if ($close !== '' && !preg_match('/^\d{2}:\d{2}$/', $close)) continue; // end optional (open-ended) $note = isset($r['note']) ? trim($r['note']) : ''; $note = utf8_cut($note, 200); $clean[] = ['dow' => $dow, 'open' => $open, 'close' => $close, 'note' => $note]; } // sort by dow then start time, assign seq within each dow usort($clean, function ($a, $b) { if ($a['dow'] !== $b['dow']) return $a['dow'] - $b['dow']; return strcmp($a['open'], $b['open']); }); $uEsc = addslashes($user); // full replace qry("DELETE FROM tbl_customer_accesstime WHERE ca_customer_uid = " . $c_uid); $seqByDow = []; foreach ($clean as $r) { $d = $r['dow']; $seqByDow[$d] = isset($seqByDow[$d]) ? $seqByDow[$d] + 1 : 1; $seq = $seqByDow[$d]; $openV = "'" . $r['open'] . ":00'"; $closeV = ($r['close'] === '') ? "NULL" : "'" . $r['close'] . ":00'"; $noteV = ($r['note'] === '') ? "NULL" : "'" . addslashes($r['note']) . "'"; qry("INSERT INTO tbl_customer_accesstime (ca_customer_uid, ca_day_of_week, ca_seq, ca_open_time, ca_close_time, ca_note, ca_status, ca_createdby, ca_created_at) VALUES ($c_uid, $d, $seq, $openV, $closeV, $noteV, 'A', '$uEsc', NOW())"); } echo json_encode(['ok' => true]); exit; } /* ===================================================================== * GET: load existing * ===================================================================== */ $existing = []; // dow => [ {open, close, note}, ... ] if ($c_uid > 0) { $res = qry("SELECT ca_day_of_week, ca_seq, ca_open_time, ca_close_time, ca_note FROM tbl_customer_accesstime WHERE ca_customer_uid = $c_uid AND ca_status = 'A' ORDER BY ca_day_of_week, ca_seq"); while ($row = fetch_array($res)) { $existing[(int)$row['ca_day_of_week']][] = [ 'open' => substr($row['ca_open_time'], 0, 5), 'close' => $row['ca_close_time'] ? substr($row['ca_close_time'], 0, 5) : '', 'note' => $row['ca_note'], ]; } } $existingJson = json_encode($existing, JSON_UNESCAPED_UNICODE); ?> Access Time

Access Time

~