0

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,

3
  • I don't know why you're not getting 4 elements in $result. But there's another bug: you need to set $rows = array() at the beginning of each iteration of the while loop. Otherwise, you'll keep appending to the data element of the same array. Commented Jul 5, 2014 at 7:35
  • Barmar, you mean I should put $rows = array() right under the while loop? Commented Jul 5, 2014 at 7:41
  • Yes, right before $rows['name'] = ... Commented Jul 5, 2014 at 7:41

2 Answers 2

1

Here the solution that you are looking for:

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)){
    $row = array();     
    $row['name']=$rec_sTeam['sale_fname'];//sale name
    $row['data'] = array();

    foreach($monArr as $key=>$val){

        $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);
        $row['data'][] =(int)$num_mLog;//(int) will remove double qoutes around numbers        
    }//foreach

    $result[] = $row;
}//while
echo json_encode($result)
Sign up to request clarification or add additional context in comments.

3 Comments

This is working John. Was array_push($result,$rows); causing a problem? Please give me some lessons on this.
No! array_push is doing the exact same thing as []. According to the documentation php.net/manual/en/function.array-push.php "Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function." So always prefer the [] instead if you have only one element to push. The "magic code" was the: $row = array(); Always make sure that in every loop you initialize your array/object and then push it to your array.
John, that's a bible! Thank you very much for a short but valuable lesson.
0

Any MySQL errors? In this line:

    $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'");

You need to brake the string in order to add an array value or use curly braces... like this:

    $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'");

OR

    $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}' ");

An example:

    $a = array("name"=>"predte4a");
echo "My name is: $a['name']";
// will echo My name is array()['name']
echo "My name is: {$a['name']}";
//will echo My name is predte4a

And do not forget the quotes in your associative array keys

2 Comments

Predte4a, Thanks for reply. But no error at all. :(
Well it is not actually an error, just you`ll get different strings:

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.