[英]How do I create a multi-dimensional associative array in PHP which JavaScript can then process?
I'm writing a PHP script which interacts with a MySQL database and a JavaScript script which uses AJAX calls to retrieve information from the PHP script. 我正在编写一个与MySQL数据库交互的PHP脚本,以及一个使用AJAX调用从PHP脚本检索信息的JavaScript脚本。
What I'm trying to do, is to create a <select>
box where my Subjects
are the <optgroup label="Subject">
and the Courses
are the <option value="1">Course</option>
within the subjects. 我要尝试做的是创建一个
<select>
框,其中我的Subjects
为Subjects
中的<optgroup label="Subject">
,而Courses
为<optgroup label="Subject">
中的<option value="1">Course</option>
。
What I currently have is a JS script which simply doesn't process, though there's no console error in Firebug. 我目前拥有的是一个JS脚本,该脚本根本无法处理,尽管Firebug中没有控制台错误。
I've written this SQL statement: 我写了这个SQL语句:
$sql = "SELECT
s.Title AS Subject,
s.Subject_ID AS Subject_ID,
c.Title AS Course,
c.Course_ID AS Course_ID
FROM
subjects s
LEFT JOIN courses c ON s.Subject_ID = c.Subject_ID
WHERE s.Faculty_ID = $faculty";
Which, if $faculty == 1
, returns this data: 如果
$faculty == 1
,则返回以下数据:
I'm then using the following code to return a multi-dimensional array where the Subject
level contains that subject's Courses
: 然后,我使用以下代码返回多维数组,其中“
Subject
级别包含该主题的“ Courses
:
$res = mysql_query( $sql );
while ( $row = mysql_fetch_assoc( $res ) ) {
$return[$row["Subject_ID"]][] = array( "Course_ID" => $row["Course_ID"], "Title" => $row["Course"] );
}
print_r( $return );
EDIT : I realise that print_r
doesn't work for sending information to my JS script (I've got a return $return
set up for that), I was just using it for debugging purposes. 编辑 :我意识到
print_r
不能用于将信息发送到我的JS脚本(为此我设置了return $return
),我只是将其用于调试目的。
The full PHP function looks like: 完整的PHP函数如下所示:
switch($_GET["cmd"]) {
case "populateForm" :
$return = json_encode( populateForm() );
break;
case "populateCourses" :
$return = json_encode( populateCourses( $_GET["faculty"] ) );
break;
}
echo $return;
- -
function populateCourses( $faculty ) {
$sql = "SELECT
s.Title AS Subject,
s.Subject_ID AS Subject_ID,
c.Title AS Course,
c.Course_ID AS Course_ID
FROM
subjects s
LEFT JOIN courses c ON s.Subject_ID = c.Subject_ID
WHERE s.Faculty_ID = $faculty";
$res = mysql_query( $sql );
while ( $row = mysql_fetch_assoc( $res ) ) {
$return[$row["Subject_ID"]][] = array( "Course_ID" => $row["Course_ID"], "Title" => $row["Course"] );
}
return $return;
}
The data from that looks like: 来自该数据的数据如下所示:
Array
(
[8] => Array
(
[0] => Array
(
[Course_ID] => 59
[Title] => Core ICT
)
[1] => Array
(
[Course_ID] => 60
[Title] => BTEC Business
)
[2] => Array
(
[Course_ID] => 61
[Title] => BTEC ICT
)
[3] => Array
(
[Course_ID] => 62
[Title] => GCSE Business
)
[4] => Array
(
[Course_ID] => 63
[Title] => GCSE ICT
)
)
[9] => Array
(
[0] => Array
(
[Course_ID] => 64
[Title] => Advance BTEC Business
)
[1] => Array
(
[Course_ID] => 65
[Title] => Advance BTEC ICT
)
[2] => Array
(
[Course_ID] => 66
[Title] => AS Applied Business
)
[3] => Array
(
[Course_ID] => 67
[Title] => AS Applied ICT
)
[4] => Array
(
[Course_ID] => 68
[Title] => A2 Applied Business
)
[5] => Array
(
[Course_ID] => 69
[Title] => A2 Applied ICT
)
[6] => Array
(
[Course_ID] => 70
[Title] => A2 Economics
)
[7] => Array
(
[Course_ID] => 71
[Title] => A2 Law
)
[8] => Array
(
[Course_ID] => 72
[Title] => GCSE Maths
)
[9] => Array
(
[Course_ID] => 73
[Title] => Maths
)
[10] => Array
(
[Course_ID] => 74
[Title] => AS Further Maths
)
[11] => Array
(
[Course_ID] => 75
[Title] => AS Maths
)
[12] => Array
(
[Course_ID] => 76
[Title] => GSE Maths Rs-Sit
)
[13] => Array
(
[Course_ID] => 77
[Title] => A2 Further Maths
)
[14] => Array
(
[Course_ID] => 78
[Title] => A2 Maths
)
)
)
However, once I get in to my JavaScript, I have no clue how to process this data into the <SELECT>
box that I'm after. 但是,一旦接触到JavaScript,就不知道如何将这些数据处理到后面的
<SELECT>
框中。 PLEASE NOTE the data is being received correctly. 请注意 ,数据已正确接收。 If I
console.log(data)
in this function, it shows all of the data that has been sent by my PHP script, as expected. 如果我在此函数中使用
console.log(data)
,它将按预期显示我的PHP脚本已发送的所有数据。
I'm trying this: 我正在尝试:
$('#courses').on("click", "option", function(event) {
var id = $(this).val();
UWA.Data.getJson(Input.URL + '?cmd=populateCourses&faculty=' + id, Input.populateCourses);
})
}
Input.populateCourses = function(data) {
$('#courses').empty();
for (var i = 0; i < data.length; i++) {
alert(data[i]);
$('#courses').append('<optgroup label="' + data[i] + '>');
for (var x = 0; x < data[i].length; x++) {
$('#courses').append('<option value="' + data[i][x].Course_ID + '">' + data[i][x].Title + '</option>');
}
$('#courses').append('</optgroup>');
}
}
With #courses
being <select id="courses"></select>
. #courses
为<select id="courses"></select>
。
I'm presuming that the way I've written these for
loops in my JS script means that the data isn't being accessed/found and as such it's failing the script. 我以为我在JS脚本中
for
循环编写这些代码的方式意味着未访问/找到数据,因此使脚本失败。
What's the best way for me to manipulate the data returned from the SQL statement to produce the <select>
box I require? 对我来说,处理从SQL语句返回的数据以产生所需的
<select>
框的最佳方法是什么? Or, should I improve the SQL statement to make things easier? 还是应该改进SQL语句以使事情变得更容易?
Thanks in advance, 提前致谢,
Use echo json_encode( array() ); exit;
使用
echo json_encode( array() ); exit;
echo json_encode( array() ); exit;
instead of print_r()
. 而不是
print_r()
。
If you reseve the data correctly there is no need to show us all that php code. 如果您正确重新设置数据,则无需向我们展示所有这些php代码。
Im pretty sure that the function you have to look at is: 我很确定您必须查看的功能是:
Input.populateCourses = function(data) {
$('#courses').empty();
for (var i = 0; i < data.length; i++) {
alert(data[i]);
$('#courses').append('<optgroup label="' + data[i] + '>');
for (var x = 0; x < data[i].length; x++) {
$('#courses').append('<option value="' + data[i][x].Course_ID + '">' + data[i][x].Title + '</option>');
}
$('#courses').append('</optgroup>');
}
}
Solution one: 解决方法一:
Input.populateCourses = function(data) {
var html = "";
for (var i = 0; i < data.length; i++) {
html += <optgroup label="' + data[i] + '>';
for (var x = 0; x < data[i].length; x++) {
html += '<option value="' + data[i][x].Course_ID + '">' + data[i][x].Title + '</option>';
}
html += '</optgroup>';
}
$('#courses').empty().html(html);
}
Solution two: 解决方案二:
Input.populateCourses = function(data) {
var $courses = $('#courses').empty();
var $optgroup;
for (var i = 0; i < data.length; i++) {
$optgroup = $('<optgroup label="' + data[i] + '>').appendTo($courses);
for (var x = 0; x < data[i].length; x++) {
$('<option value="' + data[i][x].Course_ID + '">' + data[i][x].Title + '</option>').appendTo($optgroup);
}
}
}
Haven't tried it but i'm guessing the problem is that you're data is not a zero-based array, your indexes aren't 0,1,2,3, etc so your loop doesn't work. 还没有尝试过,但是我猜问题是您的数据不是基于零的数组,索引不是0、1、2、3等,因此循环不起作用。 You should try something like this:
您应该尝试这样的事情:
Input.populateCourses = function(data) {
$('#courses').empty();
for (var i in data) {
alert(data[i]);
/*...*/
}
}
data
is an associative array in php, which translate to an object with numeric properties in javascript for (var i in data)
would iterate through the object's properties. data
是php中的一个关联数组,它转换为具有javascript中数值属性的对象, for (var i in data)
会迭代该对象的属性。
You want to json_encode
your PHP array. 您想对您的PHP数组进行
json_encode
。 JS would have no idea what to do with that string output. JS不知道如何处理该字符串输出。
Then, on the JS-side, you need to JSON.parse
the text into a JS object which you can use as normal. 然后,在JS端,您需要
JSON.parse
将文本JSON.parse
为可以正常使用的JS对象。
If you also need this to work in IE6/7 then you need to have Douglas Crockford's json2.js on the page. 如果您还需要在IE6 / 7中使用它,则需要在页面上安装Douglas Crockford的json2.js。 If you aren't supporting GhettoIE, then you don't.
如果您不支持GhettoIE,那么您不支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.