'', 'email' => '', 'phone' => '' ]; // Initialize display name variable $display_name = ''; // Initialize force form variable $forceShowForm = false; // ========== MINIMAL CHANGE: Check for pre-selected services from services.php ========== $preSelectedServiceIds = []; if (isset($_GET['selected_services'])) { $ids = explode(',', $_GET['selected_services']); $preSelectedServiceIds = array_filter(array_map('intval', $ids)); // Store in session for persistence $_SESSION['pre_selected_services'] = $preSelectedServiceIds; } // Also check if coming from services.php bookmark if (isset($_GET['book_appointment'])) { echo ''; } // ========== RESCHEDULE MODE DETECTION ========== $isRescheduleMode = false; $rescheduleAppointmentId = 0; $rescheduleAppointmentData = null; if (isset($_GET['reschedule']) && is_numeric($_GET['reschedule'])) { $rescheduleAppointmentId = intval($_GET['reschedule']); // Check if user is logged in if (isset($_SESSION['user_id'])) { $isRescheduleMode = true; // Fetch appointment details for rescheduling $reschedule_sql = " SELECT a.Appointment_ID, a.Appointment_Date, a.start_time, a.end_time, a.Appointment_Status, a.Dentist_ID, a.notes, d.name AS dentist_name, GROUP_CONCAT(aps.service_id) as service_ids, GROUP_CONCAT(s.service_name) as service_names FROM appointment a LEFT JOIN appointment_services aps ON a.Appointment_ID = aps.appointment_id LEFT JOIN services s ON aps.service_id = s.service_ID LEFT JOIN dentists d ON a.Dentist_ID = d.Dentist_ID WHERE a.Appointment_ID = ? AND a.Patient_ID = ? GROUP BY a.Appointment_ID "; $reschedule_stmt = $conn->prepare($reschedule_sql); $reschedule_stmt->bind_param("ii", $rescheduleAppointmentId, $_SESSION['user_id']); $reschedule_stmt->execute(); $reschedule_result = $reschedule_stmt->get_result(); if ($reschedule_result->num_rows > 0) { $rescheduleAppointmentData = $reschedule_result->fetch_assoc(); // Pre-select services for the form if (!empty($rescheduleAppointmentData['service_ids'])) { $reschedule_service_ids = explode(',', $rescheduleAppointmentData['service_ids']); $preSelectedServiceIds = array_merge($preSelectedServiceIds, $reschedule_service_ids); $_SESSION['pre_selected_services'] = $preSelectedServiceIds; } } else { // Appointment not found or doesn't belong to user $isRescheduleMode = false; $rescheduleAppointmentId = 0; } $reschedule_stmt->close(); } } // ========== BRACES-SPECIFIC: FUNCTIONS FOR PREVIOUS DENTIST DETECTION ========== // Original function for exact service matches (non-braces services) function getPreviousDentistForService($conn, $patient_id, $service_id) { $previousDentist = null; // FIRST: Check appointment_services table (for multi-service appointments) $sql = "SELECT aps.dentist_id as Dentist_ID, d.name AS dentist_name, a.Appointment_Date, GROUP_CONCAT(DISTINCT s.service_name) as service_names FROM appointment a INNER JOIN appointment_services aps ON a.Appointment_ID = aps.appointment_id INNER JOIN services s ON aps.service_id = s.service_ID INNER JOIN dentists d ON aps.dentist_id = d.Dentist_ID WHERE a.Patient_ID = ? AND aps.service_id = ? AND a.Appointment_Status IN ('Completed', 'Confirmed') AND aps.dentist_id IS NOT NULL GROUP BY a.Appointment_ID, aps.dentist_id ORDER BY a.Appointment_Date DESC, a.Appointment_ID DESC LIMIT 1"; $stmt = $conn->prepare($sql); $stmt->bind_param("ii", $patient_id, $service_id); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows > 0) { $previousDentist = $result->fetch_assoc(); $stmt->close(); return $previousDentist; } $stmt->close(); // SECOND: If not found in appointment_services, check appointment.Service_ID (single service appointments) $sql2 = "SELECT a.Dentist_ID, d.name AS dentist_name, a.Appointment_Date, s.service_name as service_names FROM appointment a LEFT JOIN services s ON a.Service_ID = s.service_ID LEFT JOIN dentists d ON a.Dentist_ID = d.Dentist_ID WHERE a.Patient_ID = ? AND a.Service_ID = ? AND a.Appointment_Status IN ('Completed', 'Confirmed') AND a.Dentist_ID IS NOT NULL ORDER BY a.Appointment_Date DESC, a.Appointment_ID DESC LIMIT 1"; $stmt2 = $conn->prepare($sql2); $stmt2->bind_param("ii", $patient_id, $service_id); $stmt2->execute(); $result2 = $stmt2->get_result(); if ($result2->num_rows > 0) { $previousDentist = $result2->fetch_assoc(); } $stmt2->close(); return $previousDentist; } // NEW: Braces-specific function for detecting both placement and follow-ups function getDentistForBracesService($conn, $patient_id, $service_id) { $result = [ 'found' => false, 'dentist_id' => null, 'dentist_name' => null, 'last_appointment_date' => null, 'match_type' => null, // 'initial' or 'followup' 'service_type' => null, 'initial_date' => null ]; // Get the service name $currentServiceQuery = "SELECT service_name FROM services WHERE service_ID = ?"; $stmt = $conn->prepare($currentServiceQuery); $stmt->bind_param("i", $service_id); $stmt->execute(); $currentServiceResult = $stmt->get_result(); $currentService = $currentServiceResult->fetch_assoc(); $currentServiceName = strtolower($currentService['service_name'] ?? ''); $stmt->close(); // Check if this is a treatment-related service (braces, bridges, crowns, implants, dentures, tmj, veneers) $treatmentKeywords = [ 'brace', 'braces', 'orthodontic', 'ortho', 'bridge', 'crown', 'implant', 'denture', 'tmj', 'veneer', 'root canal' ]; $isTreatmentService = false; foreach ($treatmentKeywords as $keyword) { if (strpos($currentServiceName, $keyword) !== false) { $isTreatmentService = true; break; } } if (!$isTreatmentService) { return $result; // Not a treatment service } // Determine if this is likely initial placement or follow-up based on keywords $initialKeywords = ['placement', 'initial', 'install', 'new', 'start']; $followUpKeywords = ['adjust', 'adjustment', 'follow', 'follow-up', 'check', 'tighten', 'maintenance']; $isInitialService = false; $isFollowUpService = false; foreach ($initialKeywords as $keyword) { if (strpos($currentServiceName, $keyword) !== false) { $isInitialService = true; break; } } foreach ($followUpKeywords as $keyword) { if (strpos($currentServiceName, $keyword) !== false) { $isFollowUpService = true; break; } } // If no clear indicators, check patient history for THIS SPECIFIC SERVICE if (!$isInitialService && !$isFollowUpService) { // Look for any previous appointments with the EXACT SAME SERVICE $historySql = "SELECT COUNT(*) as count FROM appointment a WHERE a.Patient_ID = ? AND a.Service_ID = ? AND a.Appointment_Status IN ('Completed', 'Confirmed')"; $historyStmt = $conn->prepare($historySql); $historyStmt->bind_param("ii", $patient_id, $service_id); $historyStmt->execute(); $historyResult = $historyStmt->get_result(); $historyData = $historyResult->fetch_assoc(); $historyStmt->close(); if ($historyData['count'] > 0) { $isFollowUpService = true; // Patient has history with this service, treat as follow-up } else { $isInitialService = true; // No history with this service, treat as initial } } $result['service_type'] = $isInitialService ? 'initial' : 'followup'; // ========== CASE 1: INITIAL TREATMENT SERVICE (Placement/New Treatment) ========== if ($isInitialService) { // Look for previous appointments with the EXACT SAME SERVICE $sql = "SELECT a.Dentist_ID, d.name AS dentist_name, a.Appointment_Date, s.service_name FROM appointment a LEFT JOIN dentists d ON a.Dentist_ID = d.Dentist_ID LEFT JOIN services s ON a.Service_ID = s.service_ID WHERE a.Patient_ID = ? AND a.Service_ID = ? AND a.Appointment_Status IN ('Completed', 'Confirmed') AND a.Dentist_ID IS NOT NULL ORDER BY a.Appointment_Date DESC LIMIT 1"; $stmt = $conn->prepare($sql); $stmt->bind_param("ii", $patient_id, $service_id); $stmt->execute(); $treatmentResult = $stmt->get_result(); if ($treatmentResult->num_rows > 0) { $previous = $treatmentResult->fetch_assoc(); $result['found'] = true; $result['dentist_id'] = $previous['Dentist_ID']; $result['dentist_name'] = $previous['dentist_name']; $result['last_appointment_date'] = $previous['Appointment_Date']; $result['match_type'] = 'initial'; $result['initial_date'] = $previous['Appointment_Date']; } $stmt->close(); } // ========== CASE 2: FOLLOW-UP TREATMENT SERVICE (Adjustments) ========== if ($isFollowUpService) { // FIRST PRIORITY: Find the most recent INITIAL treatment placement for RELATED service // For follow-ups, look for the main treatment service (without 'Follow-Up' in name) // Get the base service name by removing follow-up indicators $baseServiceName = preg_replace('/\s*\([^)]*follow[^)]*\)/i', '', $currentServiceName); $baseServiceName = preg_replace('/\s*follow[- ]?up\s*/i', '', $baseServiceName); $baseServiceName = trim($baseServiceName); // Find the service ID of the main treatment (without follow-up) $mainServiceId = $service_id; // default to current ID $mainServiceSql = "SELECT service_ID FROM services WHERE LOWER(service_name) = LOWER(?) OR LOWER(service_name) LIKE CONCAT(LOWER(?), '%') AND LOWER(service_name) NOT LIKE '%follow%' LIMIT 1"; $mainServiceStmt = $conn->prepare($mainServiceSql); $mainServiceStmt->bind_param("ss", $baseServiceName, $baseServiceName); $mainServiceStmt->execute(); $mainServiceResult = $mainServiceStmt->get_result(); if ($mainServiceRow = $mainServiceResult->fetch_assoc()) { $mainServiceId = $mainServiceRow['service_ID']; } $mainServiceStmt->close(); // Now look for initial/main treatment with this service ID $initialSql = "SELECT a.Dentist_ID, d.name AS dentist_name, a.Appointment_Date, a.Appointment_ID FROM appointment a LEFT JOIN dentists d ON a.Dentist_ID = d.Dentist_ID LEFT JOIN services s ON a.Service_ID = s.service_ID WHERE a.Patient_ID = ? AND a.Service_ID = ? AND a.Appointment_Status IN ('Completed', 'Confirmed') AND a.Dentist_ID IS NOT NULL ORDER BY a.Appointment_Date ASC LIMIT 1"; $initialStmt = $conn->prepare($initialSql); $initialStmt->bind_param("ii", $patient_id, $mainServiceId); $initialStmt->execute(); $initialResult = $initialStmt->get_result(); if ($initialResult->num_rows > 0) { $initial = $initialResult->fetch_assoc(); $result['found'] = true; $result['dentist_id'] = $initial['Dentist_ID']; $result['dentist_name'] = $initial['dentist_name']; $result['last_appointment_date'] = $initial['Appointment_Date']; $result['match_type'] = 'followup'; $result['initial_date'] = $initial['Appointment_Date']; } $initialStmt->close(); // SECOND PRIORITY: If no initial placement found, look for most recent follow-up for SAME service if (!$result['found']) { $followUpSql = "SELECT a.Dentist_ID, d.name AS dentist_name, a.Appointment_Date FROM appointment a LEFT JOIN dentists d ON a.Dentist_ID = d.Dentist_ID LEFT JOIN services s ON a.Service_ID = s.service_ID WHERE a.Patient_ID = ? AND a.Service_ID = ? AND a.Appointment_Status IN ('Completed', 'Confirmed') AND a.Dentist_ID IS NOT NULL ORDER BY a.Appointment_Date DESC LIMIT 1"; $followUpStmt = $conn->prepare($followUpSql); $followUpStmt->bind_param("ii", $patient_id, $service_id); $followUpStmt->execute(); $followUpResult = $followUpStmt->get_result(); if ($followUpResult->num_rows > 0) { $previous = $followUpResult->fetch_assoc(); $result['found'] = true; $result['dentist_id'] = $previous['Dentist_ID']; $result['dentist_name'] = $previous['dentist_name']; $result['last_appointment_date'] = $previous['Appointment_Date']; $result['match_type'] = 'followup_previous'; } $followUpStmt->close(); } } return $result; } // ========== MAIN PREVIOUS DENTIST COLLECTION ========== $previousDentistsByService = []; // DEBUG: Add this to check user ID $debug_user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0; if (isset($_SESSION['user_id'])) { $user_id = $_SESSION['user_id']; // Connect to database $conn = new mysqli('localhost', 'root', '', 'clinic_db'); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Fetch user info - UPDATED TO INCLUDE FIRST NAME $sql = "SELECT fullname, email, phone, first_name FROM users WHERE id = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $user_id); $stmt->execute(); $result = $stmt->get_result(); if ($result && $result->num_rows > 0) { $userData = $result->fetch_assoc(); // Set display name (first name if available, otherwise username) $display_name = !empty($userData['first_name']) ? htmlspecialchars($userData['first_name']) : htmlspecialchars($_SESSION['username']); } else { // Fallback to username if user not found $display_name = isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : ''; } $stmt->close(); // Check if user has already submitted patient form $sql2 = "SELECT COUNT(*) FROM patient_records WHERE user_id = ?"; $stmt2 = $conn->prepare($sql2); $stmt2->bind_param("i", $user_id); $stmt2->execute(); $stmt2->bind_result($count); $stmt2->fetch(); $stmt2->close(); // If no records, force show form if ($count == 0) { $forceShowForm = true; } // Get all active services $allServicesQuery = "SELECT service_ID, service_name FROM services WHERE is_active = 1 ORDER BY service_ID"; $allServicesResult = mysqli_query($conn, $allServicesQuery); $serviceCount = 0; $servicesWithPreviousDentist = 0; while ($service = mysqli_fetch_assoc($allServicesResult)) { $service_id = $service['service_ID']; $service_name = $service['service_name']; $serviceCount++; // Check if this is a treatment-related service (braces, bridges, crowns, implants, dentures, tmj, veneers) $treatmentKeywords = [ 'brace', 'braces', 'orthodontic', 'ortho', 'bridge', 'crown', 'implant', 'denture', 'tmj', 'veneer', 'root canal' ]; $isTreatmentService = false; foreach ($treatmentKeywords as $keyword) { if (stripos($service_name, $keyword) !== false) { $isTreatmentService = true; break; } } if ($isTreatmentService) { // Use treatment-specific function $dentistData = getDentistForBracesService($conn, $user_id, $service_id); if ($dentistData['found']) { $servicesWithPreviousDentist++; $previousDentistsByService[$service_id] = [ 'dentist_id' => $dentistData['dentist_id'], 'dentist_name' => $dentistData['dentist_name'], 'last_appointment_date' => $dentistData['last_appointment_date'], 'match_type' => $dentistData['match_type'], 'service_type' => $dentistData['service_type'], 'initial_date' => $dentistData['initial_date'] ]; } } else { // For non-treatment services, use original exact match logic $previousDentist = getPreviousDentistForService($conn, $user_id, $service_id); if ($previousDentist) { $servicesWithPreviousDentist++; $previousDentistsByService[$service_id] = [ 'dentist_id' => $previousDentist['Dentist_ID'], 'dentist_name' => $previousDentist['dentist_name'], 'last_appointment_date' => $previousDentist['Appointment_Date'], 'service_names' => $previousDentist['service_names'], 'match_type' => 'exact' ]; } } } $conn->close(); } // Convert to JSON for JavaScript use $previousDentistsJson = json_encode($previousDentistsByService); // DEBUG: Add visible debug output (will appear as HTML comments) echo ""; echo ""; echo ""; echo ""; echo ""; ?>
At Umipig Dental Clinic, our mission is to deliver compassionate, high-quality dental care that enhances the health and confidence of every patient we serve. We are dedicated to creating a welcoming and comfortable environment where patients of all ages feel safe and cared for. Our team of skilled professionals is committed to staying updated with the latest advancements in dental technology and treatment methods to ensure optimal care. We believe in educating our patients, empowering them to make informed decisions about their oral health. Integrity, professionalism, and genuine concern for our patients are at the heart of everything we do. We strive to build long-lasting relationships based on trust, respect, and outstanding results. Through our commitment to excellence, we aim to transform smiles and improve lives—one patient at a time.
State-of-the-art dental technology for precise treatment
Experienced dentists and friendly staff
Sterile and comfortable clinic setting
Competitive pricing and flexible payment options
Orthodontic Treatment
Veneers
Original: at with
Estimated Duration: —
For emergencies, don't hesitate to text/call
No dentists available at the moment.
'; } $conn->close(); function formatSpecialization($text) { $items = explode(',', $text); $formatted = 'Check back regularly for schedule updates.
Speak directly with our staff during clinic hours for immediate assistance.
Send us an email and we'll respond within 24 hours during business days.
Connect with us on Facebook for quick answers to your questions.