In what ways can the performance of a PHP calendar script be optimized when dealing with a large number of database entries for date highlighting?

When dealing with a large number of database entries for date highlighting in a PHP calendar script, the performance can be optimized by limiting the number of database queries and efficiently retrieving and processing the data. One approach is to fetch all the relevant data in a single query and then loop through the results to extract the necessary information for date highlighting.

// Assume $db is the database connection object

// Fetch all database entries for date highlighting
$query = "SELECT date_field, data_field FROM calendar_entries WHERE date_field BETWEEN 'start_date' AND 'end_date'";
$result = $db->query($query);

// Initialize an array to store the highlighted dates
$highlighted_dates = array();

// Loop through the results and extract the necessary information
while ($row = $result->fetch_assoc()) {
    $date = $row['date_field'];
    $data = $row['data_field'];

    // Process the data and determine if the date should be highlighted
    // Add the date to the highlighted_dates array if necessary
    // Example: $highlighted_dates[$date] = $data;
}

// Use the $highlighted_dates array to display the highlighted dates in the calendar