0

The issue i am having is i am unable to figure out the correct code through which to extract the information i am looking for

Here firstly is the exhibitions.php

<?php
    $exhibitionsarray = array(
            array("exhibition_id" => "1", "exhibition_title" => "New York, New York", "exhibition_subject" => "New York", "ticket_price" => "10",
                "exhibits" => array(
                    array("exhibit_id" => "3", "exhibit_title" => "Brooklyn Bridge from City Hall Park", "exhibit_description" => "New York, June 2005", "exhibit_image" => "brooklynbridge.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "6", "exhibit_title" => "Central Park, New York", "exhibit_description" => "New York, June 2005", "exhibit_image" => "centralpark.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "7", "exhibit_title" => "Chrysler Building at night, New York", "exhibit_description" => "New York, July 2001", "exhibit_image" => "chrysler_building.jpg", "photographer" => "MLG")
                ),
                "locations" => array(
                    array("location_id" => "1", "location_name" => "Kelvingrove Art Gallery and Museum", "location_postcode" => "G3 8AG"),
                    array("location_id" => "3", "location_name" => "Walker Art Gallery", "location_postcode" => "L3 8EL"),
                    array("location_id" => "5", "location_name" => "Tate Modern", "location_postcode" => "SE1 9TG")
                )
            ),
            array("exhibition_id" => "2", "exhibition_title" => "Spanish Cities", "exhibition_subject" => "Spain", "ticket_price" => "15",
                "exhibits" => array(
                    array("exhibit_id" => "9", "exhibit_title" => "Eiffel Bridge, Girona", "exhibit_description" => "Girona, March 2008", "exhibit_image" => "eiffel_bridge.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "13", "exhibit_title" => "Plaza Mayor, Madrid", "exhibit_description" => "Madrid, June 2010", "exhibit_image" => "plazamayor.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "14", "exhibit_title" => "Puppy (Day)", "exhibit_description" => "Bilbao, September 2006 - Puppy in daylight", "exhibit_image" => "puppy_day.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "15", "exhibit_title" => "Puppy (Night)", "exhibit_description" => "Bilbao, March 2011 - Puppy at night", "exhibit_image" => "puppy_night.jpg", "photographer" => "LPK")                                                                      
                ),
                "locations" => array(
                    array("location_id" => "2", "location_name" => "Scottish National Gallery", "location_postcode" => "EH2 2EL")
                )                                               
            ),
            array("exhibition_id" => "3", "exhibition_title" => "A Glasgow Viewpoint", "exhibition_subject" => "Glasgow", "ticket_price" => "5",
                "exhibits" => array(
                    array("exhibit_id" => "2", "exhibit_title" => "Bothwell Castle", "exhibit_description" => "Bothwell, April 2011", "exhibit_image" => "bothwell_castle.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "10", "exhibit_title" => "Hampden Park", "exhibit_description" => "Glasgow, June 2007 - SQA Event", "exhibit_image" => "hampden.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "11", "exhibit_title" => "Hogganfield Loch, Glasgow, Winter", "exhibit_description" => "Glasgow, January 2010", "exhibit_image" => "hogganfield_loch.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "16", "exhibit_title" => "Ramblas", "exhibit_description" => "Barcelona, July 1999 - Ramblas (Two Old Friends from Glasgow)", "exhibit_image" => "ramblas.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "17", "exhibit_title" => "River Clyde at Bothwell", "exhibit_description" => "Bothwell, April 2011 - River Clyde from Bothwell", "exhibit_image" => "river_clyde.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "18", "exhibit_title" => "River Kelvin", "exhibit_description" => "Glasgow, July 2011", "exhibit_image" => "river_kelvin.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "21", "exhibit_title" => "University Avenue", "exhibit_description" => "Glasgow, July 2011", "exhibit_image" => "university_avenue.jpg", "photographer" => "LPK")                                                                     
                ),
                "locations" => array(
                    array("location_id" => "1", "location_name" => "Kelvingrove Art Gallery and Museum", "location_postcode" => "G3 8AG"),
                    array("location_id" => "2", "location_name" => "Scottish National Gallery", "location_postcode" => "EH2 2EL"),
                    array("location_id" => "3", "location_name" => "Walker Art Gallery", "location_postcode" => "L3 8EL"),
                    array("location_id" => "4", "location_name" => "The Lowry", "location_postcode" => "M50 3AZ"),
                    array("location_id" => "5", "location_name" => "Tate Modern", "location_postcode" => "SE1 9TG")
                )                                                   
            ),
            array("exhibition_id" => "4", "exhibition_title" => "Some Churches", "exhibition_subject" => "Religious Architecture", "ticket_price" => "5",
                "exhibits" => array(
                    array("exhibit_id" => "1", "exhibit_title" => "Big Ben", "exhibit_description" => "London, September 2011", "exhibit_image" => "big_ben.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "12", "exhibit_title" => "Louvre, Paris", "exhibit_description" => "Paris, June 1998", "exhibit_image" => "louvre.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "19", "exhibit_title" => "Sagrada Familia - Honeymoon", "exhibit_description" => "Barcelona, June 1997 - Honeymoon", "exhibit_image" => "sagrada.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "20", "exhibit_title" => "Mormon Temple", "exhibit_description" => "Salt Lake City, July 2005", "exhibit_image" => "salt_lake.jpg", "photographer" => "MLG")                                                                      
                ),
                "locations" => array(
                    array("location_id" => "1", "location_name" => "Kelvingrove Art Gallery and Museum", "location_postcode" => "G3 8AG"),
                    array("location_id" => "2", "location_name" => "Scottish National Gallery", "location_postcode" => "EH2 2EL")
                )                                                   
            ),
            array("exhibition_id" => "5", "exhibition_title" => "Barcelona Highlights", "exhibition_subject" => "Barcelona", "ticket_price" => "15",
                "exhibits" => array(
                    array("exhibit_id" => "4", "exhibit_title" => "Martin at Camp Nou, Honeymoon", "exhibit_description" => "Barcelona, June 1997 - Honeymoon", "exhibit_image" => "campnou.jpg", "photographer" => "LPK"),
                    array("exhibit_id" => "5", "exhibit_title" => "Placa de Catalunya, Barcelona - Honeymoon", "exhibit_description" => "Barcelona, June 1997 - Honeymoon", "exhibit_image" => "catalunya.jpg", "photographer" => "MLG")
                ),
                "locations" => array(
                    array("location_id" => "3", "location_name" => "Walker Art Gallery", "location_postcode" => "L3 8EL"),
                    array("location_id" => "4", "location_name" => "The Lowry", "location_postcode" => "M50 3AZ"),
                    array("location_id" => "5", "location_name" => "Tate Modern", "location_postcode" => "SE1 9TG")
                )                                                   
            ),
            array("exhibition_id" => "6", "exhibition_title" => "Martin’s Pictures", "exhibition_subject" => "Martin", "ticket_price" => "5",
                "exhibits" => array(
                    array("exhibit_id" => "8", "exhibit_title" => "David Crosby at David Gilmour gig", "exhibit_description" => "Glasgow, May 2006", "exhibit_image" => "davidcrosby.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "22", "exhibit_title" => "Wolves", "exhibit_description" => "Yellowstone Park, October 2004", "exhibit_image" => "wolves.jpg", "photographer" => "MLG")
                ),
                "locations" => array(
                    array("location_id" => "1", "location_name" => "Kelvingrove Art Gallery and Museum", "location_postcode" => "G3 8AG"),
                    array("location_id" => "3", "location_name" => "Walker Art Gallery", "location_postcode" => "L3 8EL"),
                    array("location_id" => "4", "location_name" => "The Lowry", "location_postcode" => "M50 3AZ"),
                    array("location_id" => "5", "location_name" => "Tate Modern", "location_postcode" => "SE1 9TG")
                )
            )               
    );
    echo json_encode($exhibitionsarray);
?>

The information i wish depends on the specific exhibition id... and only use exhibits not location so for example when the id = 1 i wish this information...

array("exhibition_id" => "1", "exhibition_title" => "New York, New York", "exhibition_subject" => "New York", "ticket_price" => "10",
                "exhibits" => array(
                    array("exhibit_id" => "3", "exhibit_title" => "Brooklyn Bridge from City Hall Park", "exhibit_description" => "New York, June 2005", "exhibit_image" => "brooklynbridge.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "6", "exhibit_title" => "Central Park, New York", "exhibit_description" => "New York, June 2005", "exhibit_image" => "centralpark.jpg", "photographer" => "MLG"),
                    array("exhibit_id" => "7", "exhibit_title" => "Chrysler Building at night, New York", "exhibit_description" => "New York, July 2001", "exhibit_image" => "chrysler_building.jpg", "photographer" => "MLG")
                ),

Now here is the code i currently have to get all exhibit info

function getExibitions()
{
    console.log(json);
myExhibitionsView = document.getElementById('exhibitioncontent');
images = document.createElement('ul');

for (var i = 0; i < json.length; i++) {
    for (var j = 0; j < json[i].exhibits.length; j++) {
        listCheck = document.createElement('p');
        listCheck.id = 'image';
        listCheck.innerHTML = "<img src = " + "./images/" + json[i].exhibits[j].exhibit_image + " Photo Cover' height='200' width='200'>";
        console.log(listCheck);
        myExhibitionsView.appendChild(images);
        images.appendChild(listCheck);
        console.log(listCheck);
    }
}

}

This code allows me to extract the json and then use the image urls... this is what i wish to do here as well but only if its of the specific id. Thank you very much for the assistance

6
  • I would be easier if you could use the exhibition_id as the keys for the exhibition arrays. Now you will have to search. So if that an option, I would change that. Commented Jun 5, 2015 at 14:58
  • this is the php script i was given... could you explain what u mean @jeroen Commented Jun 5, 2015 at 14:59
  • how would i find this information with this php script? Commented Jun 5, 2015 at 15:00
  • $exhibitionsarray = array(1 => array("exhibition_id" => "1", ... Commented Jun 5, 2015 at 15:00
  • brilliant now how would i do this using the code i have added? im not great at using php thanks Commented Jun 5, 2015 at 15:02

1 Answer 1

1

To get the data in PHP use:

foreach( $exhibitionsarray as $val ) {
    if( $val['exhibition_id'] == 1 ) {
        echo "That's the right one, do something now.";
    }
}

In Javascript use:

ar.forEach( function(element) {
    if( element['exhibition_id'] == 1 ){
        console.log(element);
    }
});

JSFiddle: http://jsfiddle.net/mhxwg2u5/

Am I missing something or was this everything?

Sign up to request clarification or add additional context in comments.

1 Comment

great and a +1 for the jsFiddle link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.