Hi,

I'm not a programmer, so I'm not very good with PHP. I've written the following code, but each time something is flagged or unflagged, a new line of text is created instead of updating the pre-existing one.

<div class="flag-likes-counter">
    <?php
		$flag = flag_get_flag('likes') or die('no "likes" flag');
		print $flag->get_count($content_id);
		print " people like this.";
	?>
</div>

So every time the "Like / Unlike" link is clicked, I get a new line of text generated (like this):

1 people like this
0 people like this
1 people like this
etc.

How do I make it so only the counter itself is updated and I don't get additional lines of text?

Thanks.

Comments

walker2238’s picture

I'm also experiencing this behaviour. I think it has something to do with flags javascript... Instead of replacing the text it just appends more text for each click.

walker2238’s picture

Title: Help with printing flag counter » updating flag count with javascript: Appends count instead of replacing it.
walker2238’s picture

Component: Miscellaneous » User interface
Category: support » bug

Can anyone confirm this bug or is it just Po3t and myself?

quicksketch’s picture

Category: bug » support

It's not a bug if it's only started happening after doing some custom coding or theming. Most likely the output is missing a required wrapper with a class name on it that Flag uses for identification.

walker2238’s picture

I'm using the default template and haven't used anything in regards to the flag API.

Here's the output of my markup... I'm quite confused in regards to this issue.

<span class="promote-wrapper promote-176">
     0 <a href="/flag/flag/promote/176?destination=taxonomy/term/147&amp;token=tGNYDEDegG19uR7h-SUN3w_lPl8hMULDfVAqpax_yzA" title="0" class="flag-action flag-link-toggle" rel="nofollow">Promote</a><span class="flag-throbber">&nbsp;</span>
    </span>
walker2238’s picture

Just to clarify... I'm not saying you're wrong... I assume that you're right because the count (Using tokens) is working when in the title attribute but outside that it only appends itself to the current number.

quicksketch’s picture

Ah sorry @walker2238, I thought you were doing custom coding like @Po3t (in the original issue). What's the configuration for the label of your flag? Maybe I'll be able to reproduce it with enough info.

walker2238’s picture

That's ok, the only "custom coding" I've done was to move the $link_title variable outside the title attribute. I moved it beside the link because using it the title attribute would produce the desired results. Which seemed odd.

What do you mean by label? The flag export?

$flags = array();
// Exported flag: "Promote".
$flags['promote'] = array (
  'content_type' => 'node',
  'title' => 'Promote',
  'global' => '0',
  'types' => 
  array (
    0 => 'newslink',
  ),
  'flag_short' => 'Promote',
  'flag_long' => '[node:flag-promote-count]',
  'flag_message' => '',
  'unflag_short' => 'Demote',
  'unflag_long' => '[node:flag-promote-count]',
  'unflag_message' => '',
  'unflag_denied_text' => '',
  'link_type' => 'toggle',
  'roles' => 
  array (
    'flag' => 
    array (
      0 => '2',
    ),
    'unflag' => 
    array (
      0 => '2',
    ),
  ),
  'show_on_page' => 1,
  'show_on_teaser' => 1,
  'show_on_form' => 0,
  'access_author' => '',
  'i18n' => 0,
  'api_version' => 2,
);
return $flags;

Hmmm... what else did I do... I did remove the flag.css in my themes .info. But I assume that shouldn't be an issue. I've also yet to style the flag links myself (Currently trying to solve this issue beforehand).

I also have the rate module installed but not currently outputting anything from that module.

I do have a few custom module but nothing with JS and my html is valid.

Token module version is 7.x-1.0-rc1.

To produce what I'm experiencing open the flag template and move $link_title outside the link.

Also might as well point out that though I'm using my own custom theme I've tested and experienced the same result when switching over to the Bartik 7.10 theme.

dagomar’s picture

I had the same issue in my drupal 6 installation. I know now why this is happening. You are placing your custom piece of code outside the flag span. So the javascript will not know what piece to update. Try something like this:

<span class="<?php print $flag_wrapper_classes; ?>">
<div class="flag-likes-counter">
    <?php
        $flag = flag_get_flag('likes') or die('no "likes" flag');
        print $flag->get_count($content_id);
        print " people like this.";
    ?>
</div>
  <?php if ($link_href): ?>
    <a href="<?php print $link_href; ?>" title="<?php print $link_title; ?>" class="<?php print $flag_classes ?>" rel="nofollow"><?php print $link_text; ?></a><span class="flag-throbber">&nbsp;</span>
  <?php else: ?>
    <span class="<?php print $flag_classes ?>"><?php print $link_text; ?></span>
  <?php endif; ?>
  <?php if ($after_flagging): ?>
    <span class="flag-message flag-<?php print $status; ?>-message">
      <?php print $message_text; ?>
    </span>
  <?php endif; ?>
</span>
joachim’s picture

Status: Active » Fixed

Looks like this was resolved.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.