Forums

Display a Count from a different table in a CC connection

momentis 08 Aug, 2012
I am using the following code in a CF form (custom code) in order to provide a count of a user's logins to the website:

In the table header, I use this to count the user's logins:
<?php
$db =& JFactory::getDBO();
$query="SELECT `created_by`, COUNT(`created_by`) AS logins
FROM `#__activities_activities`
WHERE `type` = 'com' AND `package` = 'users' AND `action` = 'login' AND `status` = 'logged in'
GROUP BY `created_by`";
$db->setQuery($query);
$login_count = $db->loadObjectList('created_by');
?>


In the detail of the table, I use this to display each user's login count:
<?php
foreach($form->data['VIPList'] as $detail):
?>
<?php
$logins = $login_count[$detail['acct_user_id']];
$logins = $logins->logins;
echo $logins;
?>
<?php
endforeach;
?>


This works perfectly in the CF form. I am trying to do the same thing, but within a CC connection. Each row in the connection contains the User ID. No matter HOW I try to adapt this code I cannot get it to work. I have tried this code (in a <td> on each row in the table body), but it does not display the login count:

<?php
$user="{USERS.id}";
$db =& JFactory::getDBO();
$query="SELECT COUNT(`created_by`) AS logins
FROM `#__activities_activities`
WHERE `type` = 'com' AND `package` = 'users' AND `action` = 'login' AND `status` = 'logged in' AND `created_by` = '$user'";
$db->setQuery($query);
$login_count = $db->loadResult('logins');
echo $login_count;
?>


It displays "0" for every row. When I display the query and run it in phpMyAdmin it returns the correct count for each user. Any idea why this will not work?
momentis 10 Aug, 2012
Aarrgghh!! I just cannot get this to work. Any ideas, anyone?
GreyHead 10 Aug, 2012
Hi Rick,

You probably need to add this at the beginning of each block you use it in:
global $login_count;
Otherwise the variable scope will be limited to the current code snippet.

Bob
momentis 10 Aug, 2012
I have the following in place now in the Custom Listing:

<?php
global $login_count;
$db =& JFactory::getDBO();
$query="SELECT `created_by`, COUNT(`created_by`) AS logins
FROM `#__activities_activities`
WHERE `type` = 'com' AND `package` = 'users' AND `action` = 'login' AND `status` = 'logged in'
GROUP BY `created_by`";
$db->setQuery($query);
$login_count = $db->loadObjectList('created_by');
?>

<body><table class="zebra" width="98%" border="0">
  <thead>
    <tr>
    <td style="font-weight:bold;" width="45%">{CUSTOMERS.cust_co_name}</td>
    <td style="font-weight:bold;">{USERS.name}</td>
    <td>Logins</td>
    <td style="font-weight:bold;" width="25%">{USERS.email}</td>
    <td style="font-weight:bold;" width="10%">{USERS.username}</td>  
  </tr>
  </thead>

<tr>
  <td><a href="index.php?option=com_chronoforms&chronoform=view_customer_detail_internal&token=<?php echo $row['CUSTOMERS']['cust_id']; ?>" title="Click to View Details">{CUSTOMERS.cust_co_name}</a></td>
  <td>{USERS.name}</td>
 
  <td><?php
global $login_count;
$logins = $login_count[$detail['USERS']['id']];
$logins = $logins->logins;
echo $logins;
?></td>

  <td><a href="mailto:{USERS.email}">{USERS.email}</a></td>
  <td>{USERS.username}</td>
</tr>


And I get nothing at all in the "Logins" column. So cunfused!! 😶
Max_admin 11 Aug, 2012
Hi,

I'm not sure I can understand what you are doing here perfectly but here are some thoughts:

#1- you shouldn't use this:
$user="{USERS.id}";

instead:
$user= $row['USERS']['id'];


#2- it looks like your extra query can be associated through a multi table relation, why not doing this instead of running a new query per row ?

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
momentis 31 Jan, 2013
It's been a LONG time on this thread, but I find myself in need of this same thing again.

Max, you mentioned in your last reply using a multi-table relation. How can I do this while running a count on that table?
momentis 31 Jan, 2013
Let me post some info which may help:

I have a CC connection with one table and two existing one-to-one relations. This is working perfectly. I want to display in the connection list the number of times each user has logged in (using Joomlatools Logman extension and DB table). I have it working now by including the following in the custom list:

<?php
$user = $row['userinfo']['id'];
$db =& JFactory::getDBO();
$query = "SELECT COUNT(`activities_activity_id`) AS num FROM `#__activities_activities` WHERE `application` =  'Site' AND `type` = 'com' AND `package` = 'users' AND `name` = 'user' AND `action` = 'login' AND `created_by` = '$user'"; 
$db->setQuery($query);
$db->query();
$logins = $db->loadResult();
echo $logins;
?>


This is fine for a small data set but I fear it may choke out when the user count gets to be high. I am having difficulty figuring out how to use the multi-table relation to mirror what I have in the code above.

Hope that helps!
Rick
Max_admin 03 Feb, 2013
Hi Rick,

You may move this query to the "header" box and change it to "loadResultList" (or LoadObjectList), and add a GROUP BY `created_by`, that should return the count for all users (I think), which you may then loop on for all rows, you can follow the same steps to get it done using a new "multi table" relation, just define a unique model id and make it a "one to many" relationship and include the group by in the where box, and the COUNT(*) as count in the fields box.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
momentis 07 Feb, 2013
I just cannot get this to work. I know that I can put the query in the header, but I'd really like to understand how to use the multi-table relation area to get this to work.

Here's what I have in the multi-table relation--
General Tab:
Table: #__activities_activities
Model ID: logins
Join Rule: vipinfo.vip_user_id = logins.created_by
Related to: vipinfo
Advanced Tab:
Relation: One to Many
Foreign key: created_by
Where: logins.application='site' AND logins.package='users' AND logins.name='user' AND logins.action='login' GROUP BY logins.created_by
Fields Tab:
Field list: count(*) as count


For the WHERE clause, I have tried with the Model ID (as shown) and without. I have tried including the Model ID in the Foreign key field as well. Nothing seems to be working. The above code generates the following error when viewing the connection:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'logins.application='site' AND logins.package='users' AND logins.name='user' AND ' at line 1 SQL=SELECT count(*) AS `count` FROM `cabo_activities_activities` AS `logins` WHERE `logins`.`created_by` IN ('11','9') logins.application='site' AND logins.package='users' AND logins.name='user' AND logins.action='login' GROUP BY logins.created_by
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'logins.application='site' AND logins.package='users' AND logins.name='user' AND ' at line 1 SQL=SELECT count(*) AS `count` FROM `cabo_activities_activities` AS `logins` WHERE `logins`.`created_by` IN ('11','9') logins.application='site' AND logins.package='users' AND logins.name='user' AND logins.action='login' GROUP BY logins.created_by



I just don't know how to structure this to work. Sorry for being so obtuse!!!!

Rick
momentis 07 Feb, 2013
I am also trying the method of placing the query in the header and then looping through the records, which I also cannot get to work! I wish I was better at this stuff!!! 😶 😶 😶

Here's the code I have in the connection header:
<?php
$db =& JFactory::getDBO();
$query = "SELECT `created_by`, COUNT(`created_by`) AS logins FROM `#__activities_activities` WHERE `type` = 'com' AND `package` = 'users' AND `name` = 'user' AND `action` = 'login' GROUP BY `created_by`";
$db->setQuery($query);
$login_count = $db->loadObjectList('created_by');
?>


Then, I have the following in the table body:
  <td>
    <?php
    $logins = $login_count[$detail['vipinfo']['vip_user_id']];
    $logins = $logins->logins;
    echo $logins;
    ?>
  </td>


The field "vipinfo.vip_user_id" is what matches to the field "created_by" in the query. There are no results being returned in the output.😟
momentis 07 Feb, 2013
Mistake in prior post. I changed:
$logins = $login_count[$detail['vipinfo']['vip_user_id']];

To:
$logins = $login_count[$row['vipinfo']['vip_user_id']];

Didn't make a difference - still no results.
GreyHead 08 Feb, 2013
Hi Rick,

Random observations:

In the post with the MySQL errors there's an AND missing in
WHERE `logins`.`created_by` IN ('11','9') **AND** logins.application='site' AND logins.package='users'
(the ** are not needed!

In the post with the code in the header you probably need global $login_count; just after tne <?php in each case. Without this is the scope of the variable is limited to the current snippet and not carried on to the next one.

Bob
Max_admin 10 Feb, 2013
Hi Rick,

Please try to add extra "AND " to the string in the WHERE SQL field as Bob has mentioned, see if this helps ?

I would also debug the value returned after this line:
$login_count = $db->loadObjectList('created_by');


just add
print_r2($login_count);


I'm not sure what does the first parameter of the "loadObjectList" function, are you sure of that ?

If the value returned is correct then you can use Bob's suggestion to globalize the variable so that you can use it in the body.

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.