This topic contains a post which is marked as Best Answer. Press here if you would like to see it.
*

wfaiiigy

  • **
  • 10 posts
[PHP] Function for count your favorites
« on: July 20, 2017, 09:51:02 AM »
Function for count your favorites

Code: [Select]
<?php function fi_count_favorites() {
    
//Lazy function :P
    
$user_id osc_logged_user_id();
    
$lists ModelFI::newInstance()->getAllFavoriteListsByUserId$user_id );
    foreach(
$lists as $l) {
        
$count count(ModelFI::newInstance()->getActiveFavoriteItemsByListId$l['list_id'] ));
    }
    
//Fix for blank count
    
if ($count == ''){
        echo 
$count 0;
    } else {
        echo 
$count;
    }
}     
?>


Call function to your notification class

Code: [Select]

<span class="counter"><?php echo fi_count_favorites(); ?></span>


*

MB Themes

Re: [PHP] Function for count your favorites
« Reply #1 on: July 20, 2017, 11:16:28 AM »
@wfaiiigy
Thanks for sharing nice piece of code ;)
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

Re: [PHP] Function for count your favorites
« Reply #2 on: August 25, 2017, 01:31:45 PM »
Good morning!

Function for count your favorites

Code: [Select]
<?php function fi_count_favorites() {
    
//Lazy function :P
    
$user_id osc_logged_user_id();
    
$lists ModelFI::newInstance()->getAllFavoriteListsByUserId$user_id );
    foreach(
$lists as $l) {
        
$count count(ModelFI::newInstance()->getActiveFavoriteItemsByListId$l['list_id'] ));
    }
    
//Fix for blank count
    
if ($count == ''){
        echo 
$count 0;
    } else {
        echo 
$count;
    }
}     
?>


Call function to your notification class

Code: [Select]

<span class="counter"><?php echo fi_count_favorites(); ?></span>


This function displays the number of lists. :-\ How do I output the number of items from all lists?
« Last Edit: August 25, 2017, 06:40:32 PM by Алексей Захаров »

*

MB Themes

Re: [PHP] Function for count your favorites
« Reply #3 on: August 25, 2017, 03:29:26 PM »
That does not make sense as 1 item can be in multiple lists.
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots

Re: [PHP] Function for count your favorites
« Reply #4 on: August 25, 2017, 06:49:08 PM »
That does not make sense as 1 item can be in multiple lists.

The header shows 2 lists. I need to output the 7 items from the two lists.

*

atba

  • ****
  • 187 posts
Re: [PHP] Function for count your favorites
« Reply #5 on: February 28, 2019, 09:15:25 AM »
Cool solution! But do not tell me how to install the counter in the mobile version, for example Veronika theme.... When you try to add code, it is automatically commented....

echo '<li><a href="' . osc_route_url('favorite-lists', array('list-id' => '0', 'current-update' => '0', 'notification-update' => '0', 'list-remove' => '0', 'iPage' => '0')) .'" ><i class="fa fa-heart-o"></i>'.__('Favorite items', 'favorite_items').' <span class="im-user-account-count1"><?php echo fi_count_favorites(); ?></span></a></li>';






Marked as best answer by frosticek on November 20, 2019, 09:24:32 AM
*

MB Themes

Re: [PHP] Function for count your favorites
« Reply #6 on: November 20, 2019, 09:24:17 AM »
Updated function to count items for non-logged users as well.

Code: [Select]
<?php 
function fi_count_favorites() {
  
$user_id = (osc_is_web_user_logged_in() ? osc_logged_user_id() : mb_get_cookie('fi_user_id'));

  
$lists ModelFI::newInstance()->getAllFavoriteListsByUserId($user_id);
  
$count 0;

  foreach(
$lists as $l) {
    
$count += count(ModelFI::newInstance()->getActiveFavoriteItemsByListId($l['list_id']));
  }

  return 
$count;
}     
?>


Usage:
Code: [Select]
<span class="counter"><?php echo fi_count_favorites(); ?></span>
  To get fast support, we need following details: Detail description, URL to reproduce problem, Screenshots