I'm so tired of finding out why the code is not loop with foreach in place. 1st loop is fetching the person name from db I use while loop as usual and it works. 2nd loop I test the total action each one of them doing in a month so I use a foreach loop for each month. But the problem is here. I can't make it loop again because of no clue.
Here's my codes:
error_reporting(E_ALL);
$thisYear=intval(date("Y"));
for($i=1;$i<13;$i++){
$i=sprintf("%02d",$i);
$monArr[]=$i;
}
//select all team members
$sql_sTeam=mysqli_query($con,"select * from TEAMNAMES order by fname asc");
$result=array();
while($rec_sTeam=mysqli_fetch_array($sql_sTeam)){
$rows['name']=$rec_sTeam['sale_fname'];//sale name
foreach($monArr as $key=>$val){
//$rows['data'][]=(int)$key;
$mon=intval($val);
//n action each member\
$sql_mLog=mysqli_query($con,"select * from mail_log where mlog_sid='$rec_sTeam[imap_sid]' and year(mlog_dtime)='$thisYear' and month(mlog_dtime)='$mon'");
$num_mLog=mysqli_num_rows($sql_mLog);
$rows['data'][] =(int)$num_mLog;//(int) will remove double qoutes around numbers
}//foreach
array_push($result,$rows);
}//while
echo json_encode($result);
There're 4 people in TEAMNAMES but this is the only result from json_encode:
[{"name":"Ar-eshah","data":[0,0,0,0,0,0,6,0,0,0,0,0]}]
Please point me out of here coz I'm stuck for several hours. Regards,
$result. But there's another bug: you need to set$rows = array()at the beginning of each iteration of thewhileloop. Otherwise, you'll keep appending to thedataelement of the same array.$rows['name'] = ...