JavaScript: extracting information from a PHP script -
the issue i'm having i'm unsure of correct code in order begin extracting php script parsed in earlier function. firstly here code
function getexibitions() { myexhibitionsview = document.getelementbyid('exhibitioncontent'); option = document.createelement('ul'); (var = 0; < json.length; i++) { option.id = 'exhibitionsall'; listcheck = document.createelement('src'); listcheck.id = "<img src = " + "./images/" + json.exhibits[i].exhibit_image; myexhibitionsview.appendchild(option); option.appendchild(listcheck); //= json.states[i].image; } } the issue believe there must after json.something.length means of traversing data.
here php script:
<?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 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 @ 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 , 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 @ 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 glasgow)", "exhibit_image" => "ramblas.jpg", "photographer" => "mlg"), array("exhibit_id" => "17", "exhibit_title" => "river clyde @ bothwell", "exhibit_description" => "bothwell, april 2011 - river clyde 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 , 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 , 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 @ 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 @ 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 , 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); ?> i sure json.exhibits.length trick leads several errors such json , length both being undefined know error here. have done similar piece of code , followed same fixes no success.
edit: json parse
function jsonall() { console.log("in function!"); url = "exhibitions.php"; xmlhttp.onreadystatechange = function() { if (xmlhttp.readystate == 4 && xmlhttp.status == 200) { console.log("the connection complete"); json = json.parse(xmlhttp.responsetext); console.log(json); getexibitions(); } } xmlhttp.open("get", url, true); xmlhttp.send(); }
if you're trying exhibits, need 2 loops:
for (var = 0; < json.length; i++) { (var j = 0; j < json[i].exhibits.length; j++) { listcheck = document.createelement('src'); listcheck.id = "<img src = " + "./images/" + json[i].exhibits[j].exhibit_image; myexhibitionsview.appendchild(option); option.appendchild(listcheck); } }
Comments
Post a Comment