The aim of what I would like to do is allow the user to select a classification and a list of jobs for that selected classification is displayed. Currently, I have the search bar displaying the classifications and all of the jobs are displayed. What is do I have to change in my code to allow the search option to function? I’m using two files which are a json file and a php file. The json file contains classifications such as Tradesperson and related workers. In each classification are a list of two jobs.
jobsfinal_v3.json file looks like this:
[
{
“id”: 1,
“cname”: “Generalist managers”,
“jobs”: [“Directors”, “Building and Construction manager”]
},
{
“id”: 2,
“cname”: “Specialist managers”,
“jobs”: [“School Principal”, “Finance managers”]
},
{
“id”: 3,
“cname”: “Science, Building and Engineering professionals”,
“jobs”: [“Mining Engineer”, “Architects”]
},
{
“id”: 4,
“cname”: “Business and Information professionals”,
“jobs”: [“Accountants”, “Marketing and advertising”]
},
{
“id”: 5,
“cname”: “Tradespersons and related workers”,
“jobs”: [“Electrician”, “Mechanics”]
},
{
“id”: 6,
“cname”: “Production and Transport Workers”,
“jobs”: [“Road and Rail Transport drivers”, “Miners”]
}
]
jobsfinal_v5.php file looks like this:
<?php
//new code
// an array of ids for each classification
$jobtitles = array(
'Select a classification' => '0',
'General managers' => '1',
'Specialist managers' => '2',
'Science, Building and Engineering professionals' => '3',
'Business and Information professionals' => '4',
'Tradesperson and related workers' => '5'
);
// check if a classifications has already been selected, otherwise set it to the first key
if (isset($_GET["jobtitle"])) {
$jobtitle = $_GET["jobtitle"];
} else {
$jobtitle = key($jobtitles);
}
// retrieve the classifications from the array for the selected classification and build the url
$url = 'jobsfinal_v3.'.$jobtitles[$jobtitle].'.json';
// set up a form that loads the current page (with get method)
echo '<form name="classification" method="get" action="'.htmlentities($_SERVER['PHP_SELF']).'">';
// a drop down select box that will submit the form when changed
echo 'Classifications for <select name="jobtitle" onchange="this.form.submit()">';
// iterate through the classifications in the array to build the drop down box options
foreach ($jobtitles as $l => $u) {
if ($jobtitle == $l) {
$selected = ' selected';
} else {
$selected = '';
}
echo '<option'.$selected.' value="'.$l.'">'.$l.'</option>';
}
// close the select and form
echo '</select>';
echo '</form>';
//To display all of the contents of the json file
// Define recursive function to extract nested values
function printValues($arr) {
global $count;
global $values;
// Check input is an array
if(!is_array($arr)){
die("ERROR: Input is not an array");
}
/*
Loop through array, if value is itself an array recursively call the
function else add the value found to the output items array,
and increment counter by 1 for each value found
*/
foreach($arr as $key=>$value){
if(is_array($value)){
printValues($value);
} else{
$values[] = $value;
$count++;
}
}
// Return total count and values found in array
return array('total' => $count, 'values' => $values);
}
$json = file_get_contents('jobsfinal_v3.json');
// Decode JSON data into PHP associative array format
$arr = json_decode($json, true);
// Call the function and print all the values
$result = printValues($arr);
echo "<h3>" . $result["total"] . " value(s) found: </h3>";
echo implode("<br>", $result["values"]);
?>
Output from these two files: