将值添加到数组php中,然后放入json for ajax
伙计们,我仍然有一个问题,就是如何将其添加到数组中,然后将其放入json中,以便在ajax中使用。这是我的代码将值添加到数组php中,然后放入json for ajax,php,ajax,Php,Ajax,伙计们,我仍然有一个问题,就是如何将其添加到数组中,然后将其放入json中,以便在ajax中使用。这是我的代码 global $school_id; $get_section = "SELECT * FROM section a LEFT JOIN advisory_sections b ON(a.section_id = b.section_id)
global $school_id;
$get_section = "SELECT *
FROM section a
LEFT JOIN advisory_sections b ON(a.section_id = b.section_id)
LEFT JOIN school_faculty c ON (b.faculty_id = c.faculty_id)
WHERE a.school_id = '$school_id' ORDER BY section_level ASC";
$result=mysql_query($get_section)or die(mysql_error());
$i=0;
$data=array();
while ($row = mysql_fetch_array($result))
{
$data[] = array(
'sec_id'=>$row['section_id'],
'sec_name'=>$row['section_name'],
'sec_dept'=>$row['section_department'],
'sec_lvl'=>$row['section_level'],
'advisory_id'=>$row['advisory_id'],
'first_name'=>$row['f_firstname'],
'last_name'=>$row['f_lastname'],
'middle_name'=>$row['f_middlename'],
'advisor_id'=>$row['faculty_id'],
);
$get_subjects = "SELECT subject_name
FROM subjects
WHERE level = '".$row['section_level']."' ";
$result_get_subjects =mysql_query($get_subjects)or die(mysql_error());
$subjects_count = mysql_num_rows($result_get_subjects);
$check_archive_subjects = " SELECT b.subject_name
FROM registrar_grade_archive a
LEFT JOIN subjects b ON(a.subject_id=b.subject_id)
WHERE a.advisor_faculty_id = '".$row['faculty_id']."'
GROUP BY b.subject_name ASC
" ;
$query_checking =mysql_query($check_archive_subjects)or die(mysql_error());
$subjects_count_sent = mysql_num_rows($query_checking);
if($subjects_count_sent == $subjects_count){
array_push($data, 'status:complete');
}else{
array_push($data, 'status:Incomplete');
}
$i++;
}
echo json_encode($data);
阿贾克斯:
如您所见,状态不在阵列中。这就是为什么我会有这种不愉快
在我的输出中,我有一个状态为但有其他未定义值的新行,其他行有值但状态为未定义。。请帮帮我。。提前感谢您需要在数据结构的不同级别添加状态 尝试以下操作(请参见代码中的注释):
是的,您应该考虑切换到mysqli或PDO。以及最终的JSON应该是什么样子?总是只有一个条目吗?否则,所有结果行的状态总是相同的?没有足够的代码,我们需要更多的代码来确定问题。试着在这里发布你的整个项目,可能还有网站中所有页面的一些屏幕截图。谢谢它们不再得到维护。看到了吗?改为了解,并使用或-将帮助您决定在本项目结束后使用mysqli切换哪个。目前仍在研究中。非常感谢代码。但是你能告诉我为什么$data[]不附加状态吗?。将它放在$data[]中会得到如下结果:
$data=array(array(),'status'=>,array(),'status'=>)
因此状态本身被视为一项。状态需要在项目内部,如下所示:$data=array(array(,'Status'=>),array(,'Status'=>)
function get_sections_status(){
$.ajax({
url: 'teacher_class_get.php',
dataType: 'json',
type: 'POST', //u missed this line.
data:{'func_num':'6'},
success: function (data){
$.each(data, function(i, item) {
html = "<tr>";
html += "<td style='width:10%;'><input type='radio' name='section_id' rel='"+data[i].advisory_id+"' value='"+data[i].sec_id+"'></td>";
html += "<td style='width:25%;'><label>"+data[i].status+"</label></td>";
html += "<td style='width:15%;'><label id='year_level' rel='"+data[i].sec_lvl+"''>"+data[i].sec_lvl+"</label></td>";
html += "<td style='width:20%;'><label>"+data[i].sec_name+"</label></td>";
html += "<td style='width:30%;'><label id='faculty_id' rel='"+data[i].advisor_id+"'>"+data[i].last_name+", "+data[i].first_name+" "+data[i].middle_name+"</label></td>";
html += "</tr>";
$('#table-sections-content').append(html);
});
}
});
}
get_sections_status();
[{"sec_id":"36","sec_name":"black","sec_dept":"highschool","sec_lvl":"firstyear","advisory_id":"60","first_name":"asdf","last_name":"asdf","middle_name":"asdf","advisor_id":"1"},"status:Incomplete",{"sec_id":"32","sec_name":"level-up","sec_dept":"highschool","sec_lvl":"firstyear","advisory_id":"53","first_name":"asdf","last_name":"asdf","middle_name":"asdf","advisor_id":"1"},"status:Incomplete"
....
while ($row = mysql_fetch_array($result))
{
//Create a new item to add to $data later
$item = array(
'sec_id'=>$row['section_id'],
'sec_name'=>$row['section_name'],
'sec_dept'=>$row['section_department']
....
//Add status to $item, rather than $data
//I don't think array_push will give you what you want here
if($subjects_count_sent == $subjects_count){
$item['status']='complete';
}else{
$item['status']='incomplete';
}
//Add the new item after status has been set
$data[]=$item;
$i++;
}
....