php – 数组变量不会打印出while循环
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 数组变量不会打印出while循环,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2312字,纯文字阅读大概需要4分钟。
内容图文
![php – 数组变量不会打印出while循环](/upload/InfoBanner/zyjiaocheng/820/fee1bd28a1dd47a3b6c2e0ca0b4aabe3.jpg)
我在while循环中初始化一个数组变量,问题是当这个数组变量打印出while循环时,那么数组没有显示,当这个数组变量在循环中打印时,数组就会显示出来.
<?php
$all_rollno="";
//Get Current Session
$Res_Sess = mysql_query("SELECT sessionid from tbl_session where status=1 ORDER BY sessionid desc limit 1");
$Row_Sess = mysql_fetch_array($Res_Sess);
$Session = $Row_Sess[0];
//Get Batch
$Res_Bat = mysql_query("SELECT batchid,batchname,code FROM tbl_batch where status=1 and batchid!=12 ORDER BY batchid asc");
while($Row_Bat = mysql_fetch_array($Res_Bat)){
$Batch = $Row_Bat['batchid'];
$Bat_Code = $Row_Bat['code'];
//Present - Attendance
$Patt4 = mysql_query("select count(id),rollno from tbl_attendance where batchid = '$Batch' and sessionid = '$Session' and attend = 1 GROUP by rollno");
$arraytpresent="";
$array_rollno="";
$ipresent=0;
while($Row_P4 = mysql_fetch_array($Patt4)){
$arraytpresent[$ipresent] = round(($Row_P4[0]/$maxValue*100),0);
$array_rollno[$ipresent] = $Row_P4[1];
$ipresent++;
}
if(sizeof($all_rollno)>0){
$all_rollno = array_merge($all_rollno,$array_rollno);
} else {
$all_rollno = $array_rollno;
}
//print_r($all_rollno);
}
print_r($all_rollno);
解决方法:
在下面的代码中,我做了4个更改:
<?php
$all_rollno=array(); // 1. changed it to array
// get the session
$Res_Sess = mysql_query("SELECT sessionid from tbl_session where status=1 ORDER BY sessionid desc limit 1");
$Row_Sess = mysql_fetch_array($Res_Sess);
$Session = $Row_Sess[0];
// get the list of badges
$Res_Bat = mysql_query("SELECT batchid,batchname,code FROM tbl_batch where status=1 and batchid!=12 ORDER BY batchid asc");
// loop and get the total attendance
while($Row_Bat = mysql_fetch_array($Res_Bat)){
$Batch = $Row_Bat['batchid'];
$Bat_Code = $Row_Bat['code'];
$Patt4 = mysql_query("select count(id),rollno from tbl_attendance where batchid = '$Batch' and sessionid = '$Session' and attend = 1 GROUP by rollno");
$arraytpresent=array(); // 2. changed it to array
$array_rollno=array(); // 3. changed it to array
$ipresent=0;
while($Row_P4 = mysql_fetch_array($Patt4)){
$arraytpresent[$ipresent] = round(($Row_P4[0]/$maxValue*100),0);
$array_rollno[$ipresent] = $Row_P4[1];
$ipresent++;
}
// merge the data
$all_rollno = array_merge($all_rollno,$array_rollno); // 4.Merge with the array no need for the if condition.
}
print_r($all_rollno);
请注意,不推荐使用mysql,因此请使用mysqli / PDO.
内容总结
以上是互联网集市为您收集整理的php – 数组变量不会打印出while循环全部内容,希望文章能够帮你解决php – 数组变量不会打印出while循环所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。