4

I am initialize a array variable out of while loop and issue is that when this array variable is print out of the while loop then array is not show and when this array variable is print within the loop then array is show.

<?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);
5
  • 1
    You should debug inside your while loop and check why your variable is not filled :) Feb 21, 2019 at 8:25
  • 2
    Yikes! You should not be using those old deprecated mysql_ calls. Mission #1: Avoid executing queries in a loop. Feb 21, 2019 at 8:26
  • 1
    You init all_rollno as empty string, why? why not as empty array? Same goes for $array_rollno...
    – dWinder
    Feb 21, 2019 at 8:26
  • just at the top of your PHP code declare variable $all_rollno =[]; this will make the variable global then you don;t need to worry about all those loops; of you open error debugging of PHP you will get undefine offset all_rolllno notice error. this is because you have declare it as string and using it as an array... Feb 21, 2019 at 8:35
  • Can you explain the problem a bit more clear and tell us what you've tried to debug it?
    – Nico Haase
    Feb 21, 2019 at 8:39

3 Answers 3

6

In the below code I have done 4 changes:

<?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);

Please note that mysql is deprecated so use mysqli/PDO instead.

0
0

Your $array_rollno should be defined an empty array instead of string:

$array_rollno = [];
1
  • I have change it but issue is still same
    – Hayam khan
    Feb 21, 2019 at 8:53
0

Just change your first line with this, it will work.

$all_rollno=[];

$all_rollno=""; this will initialize variable as a string not as an array so change this line with above syntax.

1
  • I have change it but issue is still same
    – Hayam khan
    Feb 21, 2019 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.