$domain_table) { // Skip if domain filter is set and doesn't match if ($domain_filter > 0 && $domain_id != $domain_filter) { continue; } // Get project name field for this domain $projectNameField = getProjectNameField($conn, $domain_table); // Build the query $query = "SELECT *, '$domain_id' as domain_number FROM $domain_table"; $where_clauses = []; $params = []; $types = ''; // Add search condition if search term is provided if (!empty($search)) { $where_clauses[] = "(`$projectNameField` LIKE ? OR `Description` LIKE ?)"; $search_param = "%$search%"; $params[] = $search_param; $params[] = $search_param; $types .= 'ss'; } // Add type filter if provided if (!empty($type_filter)) { $where_clauses[] = "`H/S` = ?"; $params[] = $type_filter; $types .= 's'; } // Finalize the query if (!empty($where_clauses)) { $query .= " WHERE " . implode(' AND ', $where_clauses); } // First, get the total count for this domain $count_query = str_replace("SELECT *, '$domain_id' as domain_number", "SELECT COUNT(*)", $query); $count_stmt = $conn->prepare($count_query); if ($count_stmt) { if (!empty($params)) { $count_stmt->bind_param($types, ...$params); } $count_stmt->execute(); $count_result = $count_stmt->get_result(); $count_row = $count_result->fetch_row(); $total_count += $count_row[0]; $count_stmt->close(); } // Now get the actual projects $query .= " ORDER BY Project_ID DESC"; $stmt = $conn->prepare($query); if ($stmt) { if (!empty($params)) { $stmt->bind_param($types, ...$params); } $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { $row['domain_table'] = $domain_table; $row['projectNameField'] = $projectNameField; $projects[] = $row; } $stmt->close(); } } // If domain filter is active, we need to adjust pagination $total_pages = ceil($total_count / $per_page); // Sort projects by most recent (assuming Project_ID increases with newer projects) usort($projects, function($a, $b) { return $b['Project_ID'] - $a['Project_ID']; }); // Apply pagination $paginated_projects = array_slice($projects, $offset, $per_page); // Get domain descriptions $domainDescriptions = getDomainDescriptions($conn); // Include header include 'includes/header.php'; ?>