phpcountfacebook-fqlexplodefql.multiquery

FQL PHP COUNT male and female from variable


This query returns the gender (male and female) of each user of a particular event.

$fql = "select uid, sex from user where uid in (SELECT uid FROM event_member WHERE eid = $festaid AND rsvp_status = 'attending' AND start_time >= now() ORDER BY start_time desc) LIMIT 5000";  

$param  =   array(
    'method'    => 'fql.query',
    'query'     => $fql,
    'callback'  => ''
);

$fqlResult   =   $facebook->api($param);

//looping through retrieved data

$getgender = '';
foreach( $fqlResult as $keys => $values ){

    $start_date = date( 'l, F d, Y', $values['start_time'] );
    $start_time = date( 'g:i a', $values['start_time'] );

    //printing the data
  $getgender .= "{$values['sex']}";   
}

I made 2 functions: One should count all the "male" and other variables should count all "female" variables.

$gender_female = count(explode('female', $getgender));
$gender_male = count(explode('male', $getgender));

But the result of the count goes wrong, because doing the counting of "male", he also too, count a piece of the female variable "(as male);


Solution

  • Try

    $getgender = '';
    $femaleCount = 0;
    $maleCount = 0;
    foreach( $fqlResult as $keys => $values ){
    
        $start_date = date( 'l, F d, Y', $values['start_time'] );
        $start_time = date( 'g:i a', $values['start_time'] );
    
        //printing the data
        $getgender .= "{$values['sex']}";   
        if($values['sex']=="female"){
           $femaleCount++; //Female Count
        }else{
           $maleCount++; // Male Count
        }
    }