我希望显示一个时间表,下面是我怀疑错误所在的代码段。其中的错误是,尽管类数组中有两个元素,但是绑定仅使用我已命名为类的数组的第一个元素。请谁能帮我弄清楚我要去哪里错了?
while($classes = $stmt->fetch(PDO::FETCH_ASSOC)){
$showtimetable.= "{\"class\":\"";
$showtimetable.= $classes["Form"].$classes["Label"];
$showtimetable.= "\",\"subjects\":[";
//iterate every class in different times to check if they are having a session in the timetable
foreach($time as $times){
$selectlessonquery="SELECT `staffnumber`, `subject` FROM `".$timetablename."` WHERE
`Form`=? AND `Label`=? AND daytime=?";
try{
//this variable contain the array of variable classes
echo $classes["Form"].$classes["Label"];
//this works fine with output of classes as 1E and IN
//on adding these code that will use the value in $classes only 1E it binding
in prepared statement
$stmt=$conn->prepare($selectlessonquery);
$stmt->bindParam(1,$classes["Form"]);
$stmt->bindParam(2,$classes["Label"]);
$stmt->bindParam(3,$times);
$stmt->execute();
while($subject = $stmt->fetch(PDO::FETCH_ASSOC)){
if(!$subject){
//if no matching row then make the value to be empty string
$showtimetable.= "{\"subject\":\"";
$showtimetable.= " \"";
$showtimetable.= " "."},";
}else{
$showtimetable.= "{\"subject\":\"";
$showtimetable.= $subject["subject"]." ";
$showtimetable.= $subject["staffnumber"]."\"},";
}
}
}catch(Exception $e){
echo "error Ocuured ". $e->getMessage();
}
}
$showtimetable=rtrim($showtimetable,",");
$showtimetable.="]";
$showtimetable.="},";
}
echo $showtimetable
//the output contains on data relevant to 1E and it isnt looping to the other value as expected
问题是if的位置在一段时间内会导致代码意外停止,即使在没有读取行的情况下它旨在为主题提供默认值也是如此。正确的方法是
foreach($time as $times){
$selectlessonquery="SELECT `staffnumber`, `subject` FROM `".$timetablename."`
WHERE `Form`=? AND `Label`=? AND daytime=?";
try{
//this variable contain the array of variable classes
$subjects=$conn->prepare($selectlessonquery);
$subjects->bindParam(1,$classes["Form"]);
$subjects->bindParam(2,$classes["Label"]);
$subjects->bindParam(3,$times);
$subjects->execute();
if($subjects->rowCount()==0){
$showtimetable.= "{\"subject\":\"";
$showtimetable.= " \"";
$showtimetable.= " "."},";
}else{
while($subject=$subjects->fetch(PDO::FETCH_ASSOC)){
$showtimetable.= "{\"subject\":\"";
$showtimetable.= $subject["subject"]." ";
$showtimetable.= $subject["staffnumber"]."\"},";
}
}
}catch(Exception $e){
echo "error Ocuured ". $e->getMessage();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句