I am required to return a search result using only one keyword, how do I do this in PHP?

PB's Boys

New member
This question was hard to word.

We have script in PHP and HTML and we are required to modify the script so that when we enter only one search query, the data with all of that query in it is returned.
The example we are given is of prime ministers. Just say I enter John. The result returned must include all prime ministers with the name John along with displaying their year, state, party, etc.

Here is the PHP code in "defs.php":

<?php
/* Functions for PM database example. */

/* Load sample data, an array of associative arrays. */
include "pms.php";

/* Search sample data for $name or $year or $state from form. */
function search($name, $year, $state) {
global $pms;

//should make another variable called $input?

// Filter $pms by $name
if (!empty($name)) {
$results = array();
foreach ($pms as $pm) {
if (stripos($pm['name'], $name) !== FALSE) {
$results[] = $pm;
}
}
$pms = $results;
}

// Filter $pms by $year
if (!empty($year)) {
$results = array();
foreach ($pms as $pm) {
if (strpos($pm['from'], $year) !== FALSE ||
strpos($pm['to'], $year) !== FALSE) {
$results[] = $pm;
}
}
$pms = $results;
}

// Filter $pms by $state
if (!empty($state)) {
$results = array();
foreach ($pms as $pm) {
if (stripos($pm['state'], $state) !== FALSE) {
$results[] = $pm;
}
}
$pms = $results;
}

return $pms;


}
?>

I have to modify the search function. If you need any other parts of the code, like the results.php code which prints the results in a formatted table, just comment.
 
Back
Top