How can I create an interactive web page that displays a list of Prime Ministers of India since 1947 and their political parties, term durations, and notable achievements? Additionally, I would like to add a filtering feature that allows users to search for specific keywords or political parties using a logical algorithm. For example, if the user enters a mathematical equation such as "Party = 'BJP' AND TermDuration > 5", the page should display only the Prime Ministers who belong to the BJP party with a term duration greater than 5 years.
To accomplish this task, I have written the following PHP code:
<?php
// Define an array of Prime Ministers and their respective details
$primeMinisters = array(
    array("name" => "Jawaharlal Nehru", "party" => "Indian National Congress", "termDuration" => "1947-1964", "notableAchievements" => "First Prime Minister of India, played a key role in the country's independence movement."),
    array("name" => "Lal Bahadur Shastri", "party" => "Indian National Congress", "termDuration" => "1964-1966", "notableAchievements" => "Played a key role in the country's struggle for independence, introduced the famous slogan 'Jai Jawan Jai Kisan'."),
    array("name" => "Indira Gandhi", "party" => "Indian National Congress", "termDuration" => "1966-1977", "notableAchievements" => "First female Prime Minister of India, played a key role in India's victory in the Bangladesh Liberation War."),
    array("name" => "Morarji Desai", "party" => "Janata Party", "termDuration" => "1977-1979", "notableAchievements" => "Played a key role in India's independence movement, served as Chief Minister of Bombay State."),
    array("name" => "Charan Singh", "party" => "Janata Party", "termDuration" => "1979-1980", "notableAchievements" => "Served as Deputy Prime Minister of India, played a key role in the formation of the Janata Party."),
    array("name" => "Indira Gandhi", "party" => "Indian National Congress", "termDuration" => "1980-1984", "notableAchievements" => "Played a key role in India's victory in the Sri Lanka and the Operation Blue Star."),
    array("name" => "Rajiv Gandhi", "party" => "Indian National Congress", "termDuration" => "1984-1989", "notableAchievements" => "Introduced computerization in India, played a key role in ending insurgency in Mizoram."),
    array("name" => "V.P. Singh", "party" => "Janata Dal", "termDuration" => "1989-1990", "notableAchievements" => "Played a key role in the Mandal Commission controversy."),
    array("name" => "Chandra Shekhar", "party" => "Samajwadi Janata Party", "termDuration" => "1990-1991", "notableAchievements" => "Played a key role in the formation of the Janata Dal."),
    array("name" => "P.V. Narasimha Rao", "party" => "Indian National Congress", "termDuration" => "1991-1996", "notableAchievements" => "Introduced economic liberalization policies, played a key role in India's nuclear program."),
    array("name" => "Atal Bihari Vajpayee", "party" => "Bharatiya Janata Party", "termDuration" => "1996", "notableAchievements" => "Played a key role in India's nuclear tests, served as Minister of External Affairs."),
    array("name" => "H.D. Deve Gowda", "party" => "Janata Dal", "termDuration" => "1996-1997", "notableAchievements" => "Served as Chief Minister of Karnataka, played a key role in the formation of the United Front."),
    array("name" => "I.K. Gujral", "party" => "Janata Dal", "termDuration" => "1997-1998", "notableAchievements" => "Played a key role in improving India's relations with its neighboring countries."),
    array("name" => "Atal Bihari Vajpayee", "party" => "Bharatiya Janata Party", "termDuration" => "1998-2004", "notableAchievements" => "Introduced the National Highways Development Project, played a key role in the Kargil War."),
    array("name" => "Manmohan Singh", "party" => "Indian National Congress", "termDuration" => "2004-2014", "notableAchievements" => "Introduced economic reforms and policies, played a key role in the Indo-US nuclear deal."),
    array("name" => "Narendra Modi", "party" => "Bharatiya Janata Party", "termDuration" => "2014-2021", "notableAchievements" => "Launched the Swachh Bharat Abhiyan and Make in India initiatives, played a key role in India's COVID-19 vaccination drive."),
    array("name" => "Narendra Modi", "party" => "Bharatiya Janata Party", "termDuration" => "2021-Present", "notableAchievements" => "Initiated the Ayushman Bharat and Digital India campaigns, played a key role in India's space program."),
);
// Retrieve the filtering equation from the user
$filter = "Party = 'BJP' AND TermDuration > 5";
// Parse the filtering equation
$filterParts = explode(" ", $filter);
// Define an empty array to hold the filtered results
$filteredResults = array();
// Loop through each Prime Minister in the array
foreach ($primeMinisters as $primeMinister) {
    
    // Define a variable to store whether this Prime Minister matches the filtering equation
    $matchesFilter = true;
    
    // Loop through each part of the filtering equation
    foreach ($filterParts as $filterPart) {
        
        // Split the part into two parts: the attribute and the value
        $attributeValuePair = explode("=", $filterPart);
        $attribute = trim($attributeValuePair[0]);
        $value = trim($attributeValuePair[1], "'\"");
        
        // Check if the attribute matches
        if ($primeMinister[$attribute] != $value) {
            $matchesFilter = false;
            break;
        }
    }
    
    // If the Prime Minister matches the filtering equation, add them to the results array
    if ($matchesFilter) {
        $filteredResults[] = $primeMinister;
    }
}
// Display the filtered results in an HTML table
echo "<table>";
echo "<tr><th>Name</th><th>Party</th><th>Term Duration</th><th>Notable Achievements</th></tr>";
foreach ($filteredResults as $filteredResult) {
    echo "<tr>";
    echo "<td>".$filteredResult['name']."</td>";
    echo "<td>".$filteredResult['party']."</td>";
    echo "<td>".$filteredResult['termDuration']."</td>";
    echo "<td>".$filteredResult['notableAchievements']."</td>";
    echo "</tr>";
}
echo "</table>";
?> 
Is there any way I can improve the performance of this code or make it more efficient?
user
Sure, here are some suggestions to improve the performance of this code:

1. Use an associative array instead of a nested array to store the Prime Ministers data. An associative array uses the key-value pair instead of numerical indices, which can make it easier to search and filter data.

2. Use a loop to check if a Prime Minister matches the filtering equation rather than using the `if else` condition. A loop is faster and more efficient for complex comparisons.

3. Use `substr` function to get the attribute and the value of the filtering part, instead of the `explode` function. `substr` function is faster than `explode` function.

4. Use `trim` function to remove leading and trailing whitespaces from the filtering part to ensure it's accurate and proceeds accurately.

5. Use `trigger_error / die` functions to show only necessary information during the development phase, rather than both errors and useful information. Code with an interactive web page must take into account the user interface as well.

6. Use `json_encode` function if you've used the associative array format and your output structure is just a `json` format.

7. You should ensure that your output file is saved in a server directory, instead of building it straight away. Write past the directory where PHP has been enabled.

This would help considerably to boost performance:

```
<?php
// Define an array of Prime Ministers in an associative one
$primeMinisters = array(
    'Nehru' => array(
        'party' => 'Indian National Congress',
        'termDuration' => '1947-1964',
        'notableAchievements' => 'First Prime Minister of India, played a key role in India's independence movement.'),
    'Shastri' => array(
        'party' => 'Indian National Congress',
        'termDuration' => '1964-1966',
        'notableAchievements' => 'Played a crucial role in India's struggle for independence, introduced the famous slogan "Jai Jawan Jai Kisan".'),
    // More of them...
);

// Parse the filtering equation
$filterParts = array_map('trim', explode(' ', $filter));

// Loop through each Prime Minister in the array
foreach ($primeMinisters as $key => $primeminister) {
    $matchesFilter = true;
    foreach ($filterParts as $filterPart) {
        list($attr, $value) = array_map('trim', array_map('substr', explode('=', $filterPart), 2));
        if ($primeminister[$attr] != $value) {
            $matchesFilter = false;
            break;
        }
    }
    if ($matchesFilter) {
        $filteredResults[] = $primeMinisters;
    }
}

// Display the filtered results in an HTML table
$output = array();
$output[] = array(
    'Title' => 'Name',
    'Data' => array_column($filteredResults, 'name')
);
$output[] = array(
    'Title' => 'Party',
    'Data' => array_column($filteredResults, 'party')
);
$output[] = array(
    'Title' => 'Term Duration',
    'Data' => array_column($filteredResults, 'termDuration')
);
$output[] = array(
    'Title' => 'Notable Achievements',
    'Data' => array_column($filteredResults, 'notableAchievements')
);

// Use "JSON" format
echo json_encode($output);
?>
```
assistant
How can I create an interactive web page that displays a list of Prime Ministers of India since 1947 and their political parties, term durations, and notable achievements? Additionally, I would like to add a filtering feature that allows users to search for specific keywords or political parties using a logical algorithm. For example, if the user enters a mathematical equation such as "Party = 'BJP' AND TermDuration > 5", the page should display only the Prime Ministers who belong to the BJP party with a term duration greater than 5 years.
To accomplish this task, I have written the following PHP code:
<?php
// Define an array of Prime Ministers and their respective details
$primeMinisters = array(
    array("name" => "Jawaharlal Nehru", "party" => "Indian National Congress", "termDuration" => "1947-1964", "notableAchievements" => "First Prime Minister of India, played a key role in the country's independence movement."),
    array("name" => "Lal Bahadur Shastri", "party" => "Indian National Congress", "termDuration" => "1964-1966", "notableAchievements" => "Played a key role in the country's struggle for independence, introduced the famous slogan 'Jai Jawan Jai Kisan'."),
    array("name" => "Indira Gandhi", "party" => "Indian National Congress", "termDuration" => "1966-1977", "notableAchievements" => "First female Prime Minister of India, played a key role in India's victory in the Bangladesh Liberation War."),
    array("name" => "Morarji Desai", "party" => "Janata Party", "termDuration" => "1977-1979", "notableAchievements" => "Played a key role in India's independence movement, served as Chief Minister of Bombay State."),
    array("name" => "Charan Singh", "party" => "Janata Party", "termDuration" => "1979-1980", "notableAchievements" => "Served as Deputy Prime Minister of India, played a key role in the formation of the Janata Party."),
    array("name" => "Indira Gandhi", "party" => "Indian National Congress", "termDuration" => "1980-1984", "notableAchievements" => "Played a key role in India's victory in the Sri Lanka and the Operation Blue Star."),
    array("name" => "Rajiv Gandhi", "party" => "Indian National Congress", "termDuration" => "1984-1989", "notableAchievements" => "Introduced computerization in India, played a key role in ending insurgency in Mizoram."),
    array("name" => "V.P. Singh", "party" => "Janata Dal", "termDuration" => "1989-1990", "notableAchievements" => "Played a key role in the Mandal Commission controversy."),
    array("name" => "Chandra Shekhar", "party" => "Samajwadi Janata Party", "termDuration" => "1990-1991", "notableAchievements" => "Played a key role in the formation of the Janata Dal."),
    array("name" => "P.V. Narasimha Rao", "party" => "Indian National Congress", "termDuration" => "1991-1996", "notableAchievements" => "Introduced economic liberalization policies, played a key role in India's nuclear program."),
    array("name" => "Atal Bihari Vajpayee", "party" => "Bharatiya Janata Party", "termDuration" => "1996", "notableAchievements" => "Played a key role in India's nuclear tests, served as Minister of External Affairs."),
    array("name" => "H.D. Deve Gowda", "party" => "Janata Dal", "termDuration" => "1996-1997", "notableAchievements" => "Served as Chief Minister of Karnataka, played a key role in the formation of the United Front."),
    array("name" => "I.K. Gujral", "party" => "Janata Dal", "termDuration" => "1997-1998", "notableAchievements" => "Played a key role in improving India's relations with its neighboring countries."),
    array("name" => "Atal Bihari Vajpayee", "party" => "Bharatiya Janata Party", "termDuration" => "1998-2004", "notableAchievements" => "Introduced the National Highways Development Project, played a key role in the Kargil War."),
    array("name" => "Manmohan Singh", "party" => "Indian National Congress", "termDuration" => "2004-2014", "notableAchievements" => "Introduced economic reforms and policies, played a key role in the Indo-US nuclear deal."),
    array("name" => "Narendra Modi", "party" => "Bharatiya Janata Party", "termDuration" => "2014-2021", "notableAchievements" => "Launched the Swachh Bharat Abhiyan and Make in India initiatives, played a key role in India's COVID-19 vaccination drive."),
    array("name" => "Narendra Modi", "party" => "Bharatiya Janata Party", "termDuration" => "2021-Present", "notableAchievements" => "Initiated the Ayushman Bharat and Digital India campaigns, played a key role in India's space program."),
);
// Retrieve the filtering equation from the user
$filter = "Party = 'BJP' AND TermDuration > 5";
// Parse the filtering equation
$filterParts = explode(" ", $filter);
// Define an empty array to hold the filtered results
$filteredResults = array();
// Loop through each Prime Minister in the array
foreach ($primeMinisters as $primeMinister) {
    
    // Define a variable to store whether this Prime Minister matches the filtering equation
    $matchesFilter = true;
    
    // Loop through each part of the filtering equation
    foreach ($filterParts as $filterPart) {
        
        // Split the part into two parts: the attribute and the value
        $attributeValuePair = explode("=", $filterPart);
        $attribute = trim($attributeValuePair[0]);
        $value = trim($attributeValuePair[1], "'\"");
        
        // Check if the attribute matches
        if ($primeMinister[$attribute] != $value) {
            $matchesFilter = false;
            break;
        }
    }
    
    // If the Prime Minister matches the filtering equation, add them to the results array
    if ($matchesFilter) {
        $filteredResults[] = $primeMinister;
    }
}
// Display the filtered results in an HTML table
echo "<table>";
echo "<tr><th>Name</th><th>Party</th><th>Term Duration</th><th>Notable Achievements</th></tr>";
foreach ($filteredResults as $filteredResult) {
    echo "<tr>";
    echo "<td>".$filteredResult['name']."</td>";
    echo "<td>".$filteredResult['party']."</td>";
    echo "<td>".$filteredResult['termDuration']."</td>";
    echo "<td>".$filteredResult['notableAchievements']."</td>";
    echo "</tr>";
}
echo "</table>";
?> 
Is there any way I can improve the performance of this code or make it more efficient?
user
Your code is a good start, but there are a few improvements you can make to enhance performance and efficiency. Here are some suggestions:

1. **Data Structure**: The term duration is currently a string. It would be better to have start and end years as separate integer values. This will make filtering based on term duration easier and more accurate.

2. **Filter Parsing**: Your current filter parsing logic assumes that the filter will always be in the format "Attribute = 'Value'". This might not always be the case, especially when you want to support more complex filters like "TermDuration > 5". You might want to consider using a more robust parsing method or even a small domain-specific language for filters.

3. **Filtering Logic**: The filtering logic currently checks every part of the filter against every attribute of each prime minister. This might not scale well as the number of prime ministers or the complexity of the filters increases. Consider refactoring your code to only check the relevant attributes for each part of the filter.

4. **Separation of Concerns**: Your current code mixes data retrieval, filtering, and display logic. It would be better to separate these concerns into different functions or even different files. This will make your code easier to maintain and extend in the future.

5. **Use of a Database**: If the amount of data grows, it would be more efficient to store this data in a database and use SQL to filter the data. This would also make the filtering more flexible and powerful.

Here's a simplified version of how your code might look with some of these improvements:

```php
<?php
// Define an array of Prime Ministers and their respective details
$primeMinisters = array(
    // ...
);

// Function to filter prime ministers based on a given filter
function filterPrimeMinisters($primeMinisters, $filter) {
    // Parse the filter
    // ...

    // Filter the prime ministers
    // ...

    return $filteredResults;
}

// Retrieve the filtering equation from the user
$filter = "party = 'BJP' AND termDuration > 5";

// Filter the prime ministers
$filteredResults = filterPrimeMinisters($primeMinisters, $filter);

// Display the filtered results in an HTML table
displayResults($filteredResults);

// Function to display results
function displayResults($results) {
    echo "<table>";
    echo "<tr><th>Name</th><th>Party</th><th>Term Duration</th><th>Notable Achievements</th></tr>";
    foreach ($results as $result) {
        echo "<tr>";
        echo "<td>".$result['name']."</td>";
        echo "<td>".$result['party']."</td>";
        echo "<td>".$result['termStart']." - ".$result['termEnd']."</td>";
        echo "<td>".$result['notableAchievements']."</td>";
        echo "</tr>";
    }
    echo "</table>";
}
?>
```
assistant