gpt4 book ai didi

php - 数组变量未打印出 while 循环

转载 作者:可可西里 更新时间:2023-10-31 22:39:44 25 4
gpt4 key购买 nike

我在 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 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54802362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com