I am having some issues printing out the subarray homemain=>image
This is my JSON
{
"_id": ObjectId("4f7d0b9638fc5dc54c000000"),
"station": "hits",
"homemain": {
"image": "URL",
"link": "URL LINK"
}
}
As you can see under homemain it has image I want the word URL to be printed out on the page.
This is my PHP script
public function main($stationname)
{
// select a collection (analogous to a relational database's table)
$collection = $this->db->Stations_Banner;
// find everything in the collection
$cursor = $collection->find(array("station"=>"hits"),array("homemain"));
$test = array();
// iterate through the results
while( $cursor->hasNext() ) {
$test[] = ($cursor->getNext());
}
//Print Results
return json_encode($test);
}
then on my index.php page I call this
<?php
$banner = $fetch->main("hits");
echo $banner;
?>
I have tried to use $banner->homemain->image I have also tried it like $banner->homemain[‘image’]
Instead of this:
Just do this:
You don’t need to json_encode that because you’re still working with it in php. Then in your index.php you can just do this:
I assume that you’ll also want to loop through the results instead of just showing the image for the first one, but you didn’t say you needed help with that, so I’m just echoing the first result here.