Help with some code needed, please.

  • Hello, I was wondering if someone could help me with a coding issue.


    I am trying to find a way, to get a count of the members in a certain group, and display it on the front end. I have tried a few different ways, and just cant seem to get a working solution.


    I just need the number, in a span or whatever, I need to do it for a few groups so the least resource intensive way would be best :).


    Any Ideas?

  • On which page?

    A few different pages actually. I cant use a plugin, I need to do it with like 3+ different pages, and 20+ groups. Also quite a few of them, the number needs to be in modals.


    Our site is for a gaming community, That plays a few different games, and each of those games, people need to know how many people are members for that game. That is crucial info, to decide whether they want to join, for that game.



    I mean I guess a plugin could work, but I would have to update it alot, these groups are always being added, and I need to be able to display their numbers, each groups numbers individually, on different pages.

    I figured the easiest way would be to piggy back off Core code, as it already displays members of a group (the number) in the ACP, if I could just parrot that data back to the front end, with script or something that would work great.

  • You cannot add modals with pure CSS. Where and how did u add the content inside of the modals?

    I know you cant, I misunderstood your question. I thought you meant what kind of modals, as they are not JS modals like the rest of Woltlabs modals.


    The Modals Content Resides in HTML Boxes, hidden from users only allowed to guests, for the majority of them, 1 of them is vice versa.


    I do have the plugin PHP in boxes, and the ability to add PHP to my boxes, or could switch the content to template boxes. If that is what your getting at.

  • Ah, if you can use PHP it's easy ;)


    Edit:


    Code
    1. $sql = "SELECT COUNT(*) FROM wcf".WCF_N."_user_to_group WHERE groupID = ?";
    2. $statement = WCF::getDB()->prepareStatement($sql);
    3. $statement->execute([3]);
    4. $usersInGroup = $statement->fetchSingleColumn();

    Replace 3 by your desired group ID.

  • Ah, if you can use PHP it's easy ;)


    Edit:


    Code
    1. $sql = "SELECT COUNT(*) FROM wcf".WCF_N."_user_to_group WHERE groupID = ?";
    2. $statement = WCF::getDB()->prepareStatement($sql);
    3. $statement->execute([3]);
    4. $usersInGroup = $statement->fetchSingleColumn();

    Replace 3 by your desired group ID.

    Okay so I am pretty new to working with php, so forgive my ignorance, but I am getting this error.


    • Class 'WCF' not found
    • File:

      */lib/data/box/PhpBox.class.php(73) : eval()'d code (4)

  • Use wcf\system\WCF::getDB() instead of WCF::getDB(), we're just used to have these classes imported ;)

    Thanks, that fixed that error, but I am still not getting any print out.


    Am I missing something? to do on the other side? Tried manually inserting the php into a span.


    Do I need to echo the statement?

  • Yes, in fact you'll need to echo $usersInGroup; or <?=$usersInGroup?> when used inside a HTML block, for example:

    PHP
    1. <?php
    2. // Your fancy PHP code
    3. ?>
    4. Hello visitor, this mighty group has <?=$usersInGroup?> members. Pew pew!

    Okay thanks, Sorry for not updating sooner, I got it working with Echo.


    For one of my needs, that works great, however for the other it falls short :(.


    Code
    1. Hello visitor, this mighty group has <?=$usersInGroup?> members. Pew pew


    Would work perfectly. However it doesn't seem to work, in the boxes. I had to echo print everything for the other box, which for that one I was in a position I could.


    For the stuff inside a modal, I really cant Echo print all that lol.


    Any idea why the code you linked isnt working?

  • Did you try


    PHP
    1. Hello visitor, this mighty group has <?php echo $usersInGroup!; ?> members. Pew pew

    Sorry thought I replied to this the other day. I did try that, it did not work, could have been the box type as it was html, but the plug in gives a separate area for php.

    I just made it all echo, not the easiest way to manage it all, but it is working so there is that lol.



    2 other things, if you guys could. Is there a way to change this simple from groups to forum posts and Threads? I want to have a statistics, with total members, total posts, and total threads like on the forum page, on other pages.

    The system box Statistics, adds a bunch I dont want, and forces sidebar which is not where I want it, I want it to be footer box like the forum page :(.



    Alternatively if there is a way to just replicate the box that already does that on other pages, that would be cool.

  • I just made it all echo, not the easiest way to manage it all, but it is working so there is that lol.

    You can split it up into PHP Code and Template Code.

    Example:

    PHP
    1. <?php
    2. use system\wcf\WCF;
    3. $usersInGroupCount = 20;
    4. WCF::getTPL()->assign('usersInGroup', $usersInGroupCount);


    Template:

    Smarty
    1. Hello visitor, this mighty group has {$usersInGroup} members. Pew pew

    In the template code you can use plain HTML but just with the ability to add placeholders or template scripting code.

  • You can split it up into PHP Code and Template Code.

    Example:

    PHP
    1. <?php
    2. use system\wcf\WCF;
    3. $usersInGroupCount = 20;
    4. WCF::getTPL()->assign('usersInGroup', $usersInGroupCount);


    Template:

    Smarty
    1. Hello visitor, this mighty group has {$usersInGroup} members. Pew pew

    In the template code you can use plain HTML but just with the ability to add placeholders or template scripting code.

    That not only does not work, it throws a fatal error lol.



    His idea, doesn't throw a fatal, just doesn't print the numbers. His code with your idea, no numbers. your code, and your layout, fatal error.



    That said, something may be wrong with my template boxs. Anytime i have tried to use them for anything, I get that same fatal error that nothing is found.


    If it helps, I am using 2.0 with 3.1, and that has not worked.

  • Awesome man, that works perfectly!


    While I have you guys here, can I ask one more thing?


    So the pulling the number of members, how can I pull the number of Forum posts, and the number of events?


    I know the statistics box does this, but I dont want all the extra stuff thats there. Just a box with "Total Members" "Total Posts" and "Events" that I can style and move to where I need.


    I have been trying combinations, but what I have found that didn't throw an error of no Table found was,


    Code
    1. $sql = "SELECT COUNT(*) FROM wcf"calendar1_event"";


    However that's not seeming to work with either, set of code. It doesn't throw an error, but doesn't give an total either.