Support Forum

Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
coding-topic
Conditional for forum id
Avatar
Focus97
Member
Free Members
sp_UserOfflineSmall Offline
May 3, 2012 - 3:23 pm

In the TopicView and ForumView (or any template file, for that matter) - I'd like to change a few things, like buttons + text, graphic elements, etc. I'd like to run a simple conditional statement. I've switched on the Data Inspector, which tells me forum ID, slug, etc. But how can I run a simple conditional in the above templates? Something akin to:

if (forum_id == 2) {

do stuff

} else {

do other stuff

}

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
May 3, 2012 - 3:35 pm

Yes - just like that. Except you will need to use the object name and element. So - depending on what view you are in, it would not be just forum_id but $spThisForum->forum_id.

The data inspector (nice to see someone using it) should be able to show you which top level object names to use and what data is available in each data object but if you need help identifying one just shout.

andy-signature.png
YELLOW
SWORDFISH
Avatar
Focus97
Member
Free Members
sp_UserOfflineSmall Offline
May 3, 2012 - 4:25 pm

Perfect! To be sure, the data inspector's data was a bit...data heavy. I'm a novice with PHP, but it was helpful at least in showing what I can target from the array of data.

I just tried a conditional (yours above) and it works great in spForumView.

A simple question: I simply want to hide the "start a new topic" button on one of our forums. We don't want people starting a new topic within this specific forum.

This works in the spForumView:

if ($spThisForum->forum_id == 8) {
                echo ''; 
                } else {
                sp_TopicNewButton('tagClass=spButton spRight login-btn-grn', __sp('Start New Topic'), __sp('Start a new topic'), __sp('This forum is locked'));
                }

How can I adapt this to the spTopicView? We want the "add new topic" button to go away in any of the four topics we have in this forum (id = 8). Do I have to run something like $spThisTopic for each of the four, or is there a more global conditional based on the parent forum's id I can run?

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
May 3, 2012 - 5:04 pm

If it is all topics in a single forum then you can use $spThisForum there as well.

andy-signature.png
YELLOW
SWORDFISH
Avatar
Focus97
Member
Free Members
sp_UserOfflineSmall Offline
May 3, 2012 - 5:19 pm

Well, it doesn't seem to work, alas. Yes indeed, all four topics are in the one forum. But in spTopicView, this works:

if ($spThisTopic->topic_id == 738) {
                echo '';
                } else {
                sp_PostNewTopicButton('tagId=spPostNewTopicButtonTop&tagClass=spButton spRight login-btn-grn', __sp('Start New Topic'), __sp('Start a new topic'), __sp('This forum is locked'));
                }

While this does not:

if ($spThisForum->forum_id == 8) {
                echo '';
                } else {
                sp_PostNewTopicButton('tagId=spPostNewTopicButtonTop&tagClass=spButton spRight login-btn-grn', __sp('Start New Topic'), __sp('Start a new topic'), __sp('This forum is locked'));
                }

I suppose I can use the "or" statement to match the ID's of all four topics, but was hoping to slim it down with the more global "if is forum ID" statement.

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
May 3, 2012 - 6:47 pm

I wasn't thinking straight was I?
Use $spThisTopic->forum_id == 8

andy-signature.png
YELLOW
SWORDFISH
Avatar
Focus97
Member
Free Members
sp_UserOfflineSmall Offline
May 4, 2012 - 12:52 am

Terrific! Just tried it and erased 3 "or" statements as a result. 🙂 Many thanks.

Avatar
Yellow Swordfish
Glinton, England
SP Master
sp_UserOfflineSmall Offline
May 4, 2012 - 3:19 am

You can save more and clersan it up firther by just testing for a false. So instead of

if ($spThisForum->forum_id == 8) {

which needs the dummy echo and the 'else' statement - just test for that being false. So it becomes if NOT forum_id of 8:

if ($spThisForum->forum_id != 8) {

which you do with the != comparator.

andy-signature.png
YELLOW
SWORDFISH
Avatar
Focus97
Member
Free Members
sp_UserOfflineSmall Offline
May 4, 2012 - 3:32 pm

Ah, indeed. I ended up using the if/else format so that we could commandeer the text on one of the buttons, in one of the forums.

http://www.ornishspectrum.com/.....-to-knows/

In that forum, we're using a disabled "add new topic" button (we only want to keep responses tailored to the four Topics), and we're using different text on the forum's "add your reply" button.

Your code above works great, though - many thanks for the help!

Avatar
Mr Papa
Simi Valley, CA
SP Master
Free Members
sp_UserOfflineSmall Offline
May 4, 2012 - 4:00 pm

good work. glad we could help!

Forum Timezone: Europe/Stockholm
Most Users Ever Online: 1170
Currently Online:
Guest(s) 1
Top Posters:
Mr Papa: 19448
Ike: 2086
Brandon: 864
kvr28: 804
jim: 650
FidoSysop: 577
Conrad_Farlow: 531
fiddlerman: 358
Stefano Prete: 325
Member Stats:
Guest Posters: 620
Members: 17365
Moderators: 0
Admins: 4
Forum Stats:
Groups: 7
Forums: 17
Topics: 10128
Posts: 79626