3

I want to get the number of items (content) flagged by the logged in user.

<?php
  $flag = flag_get_flag('flag1');
  print $flag->get_count($nid);
?>

The above line would display the total number of times that content is flagged. How can i get number of items flagged by the logged in user. Lets say user A has flagged Content 1, Content2 and Content 3. Then the output needed is something as:

Number of Items flagged by [current:user]: 3

Thanks

3 Answers 3

3

This work for me in normal block (no in views):

<?php
global $user;
$uid = $user->uid; //Logged in user ID
$result = db_query("SELECT COUNT(*) FROM flagging WHERE uid=:uid and fid=2", array(':uid' => $uid))->fetchField(); //fid = ID your flag
print $result ." flagged items";
?>
1

This should work , untested:

$result = db_select('flag_content', 'fc')
->fields('fc', array('content_id'))
->condition('content_type', $flag->content_type)
->condition('uid', $user->uid)
->condition('fid', $flag->fid)
->execute();

$flagged = $result->fetchCol();
return array('content_flagged_by_user' => $flagged);

You could also try Rules integration with Flag

2
  • i will try this...however is it not possible to do so using Views
    – why
    Commented Feb 20, 2013 at 10:50
  • The "Views" keyword is not found in your question ... please edit it
    – Pan Chrono
    Commented Feb 20, 2013 at 12:42
0

You can use this Flag API function: flag_get_user_flags()

Suppose you want to get number of times node A was flagged by current logged in user:

$flag = flag_get_user_flags('node', 'NID_OF_A');

Your Answer

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

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