php while循环数组打印,php – 数组变量不会打印出while循环

我在while循环中初始化一个数组变量,问题是当这个数组变量打印出while循环时,那么数组没有显示,当这个数组变量在循环中打印时,数组就会显示出来.

$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个更改:

$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,arrays

来源: https://codeday.me/bug/20190527/1161392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值