Advertisement:

Author Topic: User Items Count By Type  (Read 204 times)

bigazone

  • Newbie
  • *
  • Posts: 6
User Items Count By Type
« on: February 01, 2019, 04:11:29 am »
Hi, I'm having problem with this for a few days now.
Using <?php echo osc_count_items(); ?> I can count current user's all items. But I want to count items by type. For example, all items blocked, all items expired, all items pending validate etc...

Is it possible?

Thanks in advance.

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: User Items Count By Type
« Reply #1 on: February 04, 2019, 12:16:42 am »
Hello!

Paste this function in functions.php of your theme.

Code: [Select]
<?php
function wm_count_user_items_by_type($type$user null) { // Define type. Default user is logged one.
    
if($user == null$user osc_logged_user_id();

    switch(
$type) {
        case 
'blocked':
            
$query sprintf('SELECT COUNT(*) as num_items FROM %st_item WHERE fk_i_user_id = %s AND b_blocked = 1'DB_TABLE_PREFIX$user);
        break;
        case 
'expired':
            
$query sprintf('SELECT COUNT(*) as num_items FROM %st_item WHERE fk_i_user_id = %s AND dt_expiration < NOW()'DB_TABLE_PREFIX$user);
        break;
        case 
'pending':
            
$query sprintf('SELECT COUNT(*) as num_items FROM %st_item WHERE fk_i_user_id = %s AND b_enabled = 0'DB_TABLE_PREFIX$user); // or b_active = 0
        
break;
    }
    
    
$dao = new DAO();
    
$dao->dao->query($query);
    if(
count($query)) {
        
$count $query->result();
        
$count $count['num_items'];
    } else {
        
$count 0;
    }

    return 
$count;
}

Then use this to get count:

Code: [Select]
<?php
echo wm_count_user_items_by_type('blocked'); // get blocked
echo wm_count_user_items_by_type('expired'); // get expired
echo wm_count_user_items_by_type('pending'); // get pending
?>


Regards.