I am working on making a website that will display a list of the states and then a sublist of each national park in that state. I am currently able to get the states to show up, and the correct number of sublist elements to appear (the correct number of bullet points show up for the corresponding number of parks in each state), but I am unable to get the park name and url to work. I did this a couple weeks ago in Javascript, but now I need it in php.
Here is a sample of my array:
{
"stateParks": [
{"state": "Alabama", "parks": []},
{"state": "Alaska", "parks": [{"parkName":"Denali", "url":"https://www.nps.gov/dena/index.htm"},{"parkName":"Glacier Bay", "url":"https://www.nps.gov/glba/index.htm"}]},
And here is my current php:
<?php
$jsonData = file_get_contents("../data/stateParks.json");
$jsonDecoded = json_decode($jsonData, true);
$output = '<ul class="stateParks">';
foreach($jsonDecoded['stateParks'] as $statePark)
{
$counter = 0;
$output .= '<li class="superlist">'.$statePark['state'].'<ul>';
foreach($statePark['parks'] as $parks)
{
$output .= '<li class="sublist"><a href="'.$parks[$counter]['url'].'">'.$parks[$counter]['parkName'].'</a></li>';
$counter++;
}
$output .= '</ul></li>';
}
$output .= '</ul>';
echo $output;
?>
This is an assignment, so I would appreciate if you didn't tell me the direct answer because I want to make sure I understand what is wrong and I don't like cheating. But any pointers, tips, and hints are very appreciated.
You just need to remove $counter
use $parks['url'] instead $parks[$counter]['url']
and
use $parks['parkName'] instead $parks[$counter]['parkName']