JavaScript:从PHP脚本中提取信息
JavaScript: extracting information from a PHP script
我遇到的问题是我不确定正确的代码,以便开始从我在前面的函数中解析的PHP脚本中提取。首先是代码
function getExibitions()
{
myExhibitionsView = document.getElementById('exhibitioncontent');
option = document.createElement('ul');
for (var i = 0; i < 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;
}
}
我认为问题是在json.something.length
之后必须有一些东西作为遍历数据的手段。
<?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);
?>
我确信json.exhibits.length
会做到这一点,但这会导致几个错误,如json
和length
都是未定义的,所以我知道我的错误在这里。我已经做了一段类似的代码,并遵循相同的修复,但没有成功。
编辑: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 is complete");
json = JSON.parse(xmlhttp.responseText);
console.log(json);
getExibitions();
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
如果你想获得所有的展品,你需要两个循环:
for (var i = 0; i < json.length; i++) {
for (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);
}
}
您的问题有点难以理解,但我认为问题在于您没有正确地遍历JSON数据。
var exhibition = json[i];
var exhibit = json[i].exhibits[j];
相关文章:
- 如何从..中提取信息..我称之为数组
- 从外部页面提取信息
- 查询在强循环中基于一个表从另一个表中提取信息
- 从 API 中提取信息并将其添加到数据库(平均堆栈)
- 从字符串中提取信息并将其保存到数据库
- 如何使用 JavaScript 从 HTML 中提取信息
- 正在从字符串中提取信息
- 从api中提取信息
- 如何从多行字符串中提取信息
- 我如何使用javascript从同一工作表上的2个不同的excel单元格中提取信息?我
- 从远程mongoDB提取信息
- 使用DOM从XML中提取信息
- 从数组中提取信息
- 如何查找以字符串"word"并从带有char #的字符串中提取信息
- 从生成的网站中提取信息
- 从javascript文件中提取信息到远程站点
- 使用jQuery从表和包中提取信息到JSON对象
- 从html防油精中提取信息
- WebApi从网站中提取信息
- JavaScript:从PHP脚本中提取信息