Look at the page, which address I gave in some previous message (“…code-snippets-for-this-theme/#post-16559“).
That needs some JavaScript too for open/close functionality + an another add_action in order to create the actual list of forums. Button without JavaScript and HTML code of the function list doesn’t do anything.
If you want to do that with database queries try to use internal functions of bbPress or wordPress and not direct generic database query functions.
With internal functions you should get at least forum id:s, apparently also forum titles. You need also parent-child-relation information, if you use child forums.
But you trial creating a loop might fail because WordPress generates the web-address with its own method. You should also find the function, which converts the name of the title as web-address and you should use it to create a part of the web-address. In the case of sub-forums, you must use it twice in a single web-address. You might need also nested loops
See for example
“fixed-part-of-the-web-address/sanaristikot/suomen-kuvalehti/”
You would need fist level loop for “sanaristikot/” and if-statement for possible second level loop to get “suomen-kuvalehti/”. The PHP file would be quite complicated!
Creating a loop you must take account also possible parent-child forum relation, which makes loop more complicated at least if you want to use nested ul and li elements. If you create DIV, you must add class for sub-forums in order to get indents. That makes loop simpler.
Creating pop-up menu was easy for me because of just few forum. I didn’t need loop neither I didn’t need to use a menu.
You might have more forums. I recommend you to create a menu for this. You can also set indents for sub-forums easily just putting indents with the menu builder tool.