This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit: Removed revised code after reading this meta post this meta post.
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit: Removed revised code after reading this meta post.
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit: Removed revised code after reading this meta post.
- 121
- 4
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit:
As Brythan said, there was no need for Removed revised code after reading $hidden
this meta post.
<? if ($count = $this->likes->count()) {
$visible = 12;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; }
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $count - $visible ?> more…</span>
<? } ?>
</div>
<? } ?>
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit:
As Brythan said, there was no need for $hidden
.
<? if ($count = $this->likes->count()) {
$visible = 12;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; }
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $count - $visible ?> more…</span>
<? } ?>
</div>
<? } ?>
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit: Removed revised code after reading this meta post.
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit:
As Brythan said, there was no need for $hidden
.
<? if ($count = $this->likes->count()) {
$visible = 12;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; }
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $count - $visible ?> more…</span>
<? } ?>
</div>
<? } ?>
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
This code mimics the "likes" tooltip from Facebook.
Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.
It works as expected, but I'm sure it could be better written.
<? if ($count = $this->likes->count()) {
$visible = 12;
$hidden = $count > $visible ? $count - $visible : 0;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; };
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $hidden ?> more…</span>
<? } ?>
</div>
<? } ?>
Edit:
As Brythan said, there was no need for $hidden
.
<? if ($count = $this->likes->count()) {
$visible = 12;
?>
<div class="likes_detail">
<? $r = 0;
foreach ($this->likes as $like) {
?>
<span><?= $like->name ?></span>
<? $r++;
if ($r >= $visible) { break; }
}
if ($count > $visible) {
?>
<span class="more_users">and <?= $count - $visible ?> more…</span>
<? } ?>
</div>
<? } ?>