Count the number of records of a section
I would like to add a script in the modify_homepage plugin to show the current number of records in specific sections. I assume that I need to add a PHP script, like
<?php echo COUNT("Variable"); ?>
Unfortunately, all my efforts of editing a "Variable" have failed. May I have a hint?
I believe something like this should work:
global $CURRENT_USER; echo mysql_count('section_name', ['createdByUserNum' => $CURRENT_USER['num']]);
This assumes that the section has been set up to use the createdByUserNum field, though most sections should by default.
Let me know if this works for you!
This is great! It seems it does work. And a final issue. I need to count the number of records from the current section (i.e. section_a) that a field takes the value of this specific field from the $CURRENT_USER. In my case each $CURRENT_USER works in a 'hospital' (field name). I want to count the records from the specific section (i.e. 'section_a') where the field 'hospital' has the value of the 'hospital' of the specific '$CURRENT_USER'. Based on your script, I am using the following one, but it turns out 0 (zero).
global $CURRENT_USER; echo mysql_count('section_a', ['hospital' => $CURRENT_USER['hospital']]);
Obviously, there is a mistake.
You're using the right approach, however, there is clearly a mismatch somewhere.The first things that come to mind are:
- Are the 'hospital' fields single- or multiple-choice? This can impact how we perform the query.
- Are the 'hospital' choices configured the exact same between the user and section_a?
It worked. Actually, the script was correct but turned out 0 because the username I used was not assigned to any hospital!! When I used a different username (assigned to a specific hospital) it gave the correct number.
Thank you very much for your assistance.
I would appreciate if you gave me an extra hint on the previous issue.
If I wanted to limit the count in a certain period of time (for example after January 1, 2019), should I use the following code?
mysql_count('section_name', ['hospital' => $CURRENT_USER['hospital']], ['date_of_treatment' >= '01/01/2019']);
or, between Jan 1, 2019 and June 1, 2019
mysql_count('section_name', ['hospital' => $CURRENT_USER['hospital']], ['date_of_treatment' >= '01/01/2019' && 'date_of_treatment' <= '01/06/2019')];
That sort of filtering format with mysql_count() (and a few other mysql_ functions) only works when trying to match values. If you want to start doing more advanced comparisons, it requires explicitly creating the "WHERE" portion of the MySQL query (similar to the "where" parameter for getRecords()). So for your examples, it could look something like this:
$countWhere = "hospital = '" . mysql_escape($CURRENT_USER['hospital']) . "' AND date_of_treatment >= '2019-01-01 00:00:00'"; mysql_count('section_name', $countWhere);
$countWhere = "hospital = '" . mysql_escape($CURRENT_USER['hospital']) . "' AND date_of_treatment >= '2019-01-01 00:00:00' AND date_of_treatment <= '2019-06-01 00:00:00'"; mysql_count('section_name', $countWhere);
Note that the standard date/time format in a MySQL context is "y-m-d h:m:s"
Let me know if you have any other questions!