Custom Counters on Tab Headers

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
elgaucho
Posts: 7
Joined: 2020-07-15 14:37

Custom Counters on Tab Headers

Post by elgaucho » 2025-04-30 12:38

Hi all,

I've been trying my best to get into the use of hooks in Appgini. I have a good understanding of data models and SQL, but php and jscript for now remain beyond my comfortable knowledge. I've had some success despite this and thanks to the great resources in the community, but I'm struggling with things like this, even after trying the Udemy course to pick up some tips and tricks.

So my problem statement is fairly simple.

I have a Companies table which allows my users to add companies that they have to do checks on.

I have a Documents_Checklist tab (one of several) which is pre-filled with a checklist of documents to be checked by the users (so that they do not forget to check for each type of document).

The Documents_Checklist tab shows a counter by default in the sub tab for the parent record of ALL records in the view, which was added by Appgini some time ago.

I want to customise what is being counted.

I have tried to leverage the learnings from this page:
https://blog.appgini.com/appgini/displa ... tab-title/

and apply them to my script. I have also tried to use an AI to help me get the syntax right for the SQL element, but I'm not sure if I haven't made more of a hash of things with it.

I have the following that I have tried to add into footer_extras.php:

Code: Select all

<?php $is_homepage = (basename($_SERVER['PHP_SELF']) === 'index.php' && !isset($_REQUEST['signIn'])); if ($is_homepage) //pretty sure this is entirely wrong
<script>
  $j(function() {
    setInterval(function() {
      // Target the specific tab based on its ID
      var tabId = 'tab_Documents_Checklist-Company_ID'; //found this using inspect element on the first of the tabs

      // Use PHP to execute the SQL query to count incomplete records
      <?php
      $counter = SQL("SELECT COUNT(*) FROM Documents_Checklist WHERE status <> 'Complete'"); //i know i need to add an ID limiter for the count per record, but I want to get the basic syntax right first
      ?>

      // Update the badge within the specified tab
      if (!$j('#' + tabId + ' .badge').length) {
        $j('<span class="badge hspacer-md"></span>').appendTo('#' + tabId);
      }
      $j('#' + tabId + ' .badge').html(<?php echo $counter; ?>);
    }, 1000);
  });
</script>


I do have access to all the plugins, should there be any function in there that I've missed that could help me here.

Any advice would be welcome. I've searched and searched for existing posts with more guidance on this, but I haven't found one, nor the use case I'm after in the various youtube videos. Thanks!

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1698
Joined: 2018-04-01 10:12

Re: Custom Counters on Tab Headers

Post by pbottcher » 2025-05-03 07:14

Hi,

looking at the code there seems to be a small mistake.
The

Code: Select all

 $counter = SQL("SELECT COUNT(*) FROM Documents_Checklist WHERE status <> 'Complete'");
should be

Code: Select all

 $counter = SQL("SELECT COUNT(*) FROM Documents_Checklist WHERE status <> 'Complete'",$eo);
For the rest (your id, that append, ...) it is hard to judge as these are internal informations that cannot be looked at without seeing what you try to achieve.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

elgaucho
Posts: 7
Joined: 2020-07-15 14:37

Re: Custom Counters on Tab Headers

Post by elgaucho » 2025-05-05 06:37

Thanks pbottcher! I've updated this, and I've tried to fix my header.

Let me try to add additional context, as I'm still not seeing the expected outcome from the script.

Here is a view of the application: picture of companies table.

I want the counter in the KYC tab (currently 18 for this record per core Appgini functionality) to be the custom SQL counter.

So in footer_extras.php I have:

Code: Select all

<?php 
$is_Companies_view = (basename($_SERVER['PHP_SELF']) === 'Companies_view.php'); 
?>

<?php if ($is_companies_view) { ?>
    <script>
        $j(function() {
            setInterval(function() {
                // Target the specific tab based on its ID
                var tabId = 'tab_Documents_Checklist-Company_ID';

                // Use PHP to execute the SQL query to count incomplete records
                <?php
                $counter = SQL("SELECT COUNT(*) FROM Documents_Checklist WHERE status <> 'Accepted' "; $eo );
                ?>

                // Update the badge within the specified tab
                if (!$j('#' + tabId + ' .badge').length) {
                    $j('<span class="badge hspacer-md"></span>').appendTo('#' + tabId);
                }
                $j('#' + tabId + ' .badge').html(<?php echo $counter; ?>);
            }, 1000);
        });
    </script>
<?php } ?>
However, I'm still struggling to get anything to show, visually. Any ideas would be welcome.

Do i need to do something in addition to prevent the default appgini count?

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1698
Joined: 2018-04-01 10:12

Re: Custom Counters on Tab Headers

Post by pbottcher » 2025-05-06 06:01

Hi again,

I think you have a typo in your function call to the SQL function. The delimiter for the parameters is a , not a ;.

So try to use

Code: Select all

 SQL("SELECT COUNT(*) FROM Documents_Checklist WHERE status <> 'Accepted' ", $eo );
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

elgaucho
Posts: 7
Joined: 2020-07-15 14:37

Re: Custom Counters on Tab Headers

Post by elgaucho » 2025-05-07 13:43

Thanks pbottcher! That was a silly mistake indeed!

For everyone's benefit, I now have a working script for this (with a little external help from an acquaintance). Sharing the final version in case it helps anyone else.

This now only counts the records in the table where the status isn't accepted for the parent record, and gives users a bit better of an indication of what is outstanding to do in each tab.

Code: Select all

<?php 
$is_Companies_view = (basename($_SERVER['PHP_SELF']) === 'Companies_view.php'); 
?>

<?php if ($is_companies_view) 
{ ?>
    <script>
        $j(function() {
            setTimeout(function() {
                // Target the specific tab based on its ID
                var tabId = 'tab_Documents_Checklist-Company_ID';

                // Use PHP to execute the SQL query to count incomplete records
                <?php
                if (isset($_REQUEST['SelectedID'])) { // this gets the record that we're currently looking at, if we're looking at a record (which will always be true)
                $elem_id = makesafe($_REQUEST['SelectedID']); 
                $counter = SQL("SELECT COUNT(*) FROM Documents_Checklist WHERE status <> 'Accepted' AND Company_ID = {$elem_id}", $eo );
                }

                ?>
                $j('#' + tabId + ' .badge').html(<?php echo $counter->fetch_array() ['count']; ?>);
            }, 1000);
        });
    </script>
<?php } ?>
I then repeat the script and customise the tab identifiers and SQL for each tab to be correct.

I have a small residual problem, in that when i edit a sub-record, and then close the popup window, the counter resets to the Appgini default counter (all records), until someone does a hard refresh (F5 / Ctrl+F5).

I wonder if this script shouldn't instead be placed in my Companies.php in the Companies_init function - would this make it count every time the page refreshes?

But overall this is progress! :)

Post Reply