phpBB2Refugees.com Logo
Not affiliated with or endorsed by the phpBB Group

Register •  Login 

Continue the legacy...

Welcome to all phpBB2 Refugees!Wave Smilie

This site is intended to continue support for the legacy 2.x line of the phpBB2 bulletin board package. If you are a fan of phpBB2, please, by all means register, post, and help us out by offering your suggestions. We are primarily a community and support network. Our secondary goal is to provide a phpBB2 MOD Author and Styles area.

How to create an array?


 
Search this topic... | Search MOD Writing... | Search Box
Register or Login to Post    Index » MOD Writing  Previous TopicPrint TopicNext Topic
Author Message
dogs and things
Board Member



Joined: 18 Nov 2008

Posts: 628
Location: Spain


flag
PostPosted: Tue Mar 17, 2009 2:10 pm 
Post subject: How to create an array?

Hello,

I'm not even sure if the way I say this makes much sense but nevertheless I give it a try.

I want to create an if statement to make a certain function only apply to certain forums.

For this I create
Code:
   if ($forum_id == 1 || $forum_id == 4 || $forum_id == 6)

This works, but, I think this can be done in a better way turning it into an array.

How would I translate the above in something better?

Greetings.

_________________
phpBB2 will never die, I hope!
Back to top
RMcGirr83
Board Member



Joined: 01 Dec 2008

Posts: 53
Location: East Lyme, CT


flag
PostPosted: Tue Mar 17, 2009 2:41 pm 
Post subject: Re: How to create an array?

Code:


$forum_id_test = array(1,2,3);

if(in_array($forum_id, $forum_id_test))


eg

_________________
Rich McGirr
Back to top
dogs and things
Board Member



Joined: 18 Nov 2008

Posts: 628
Location: Spain


flag
PostPosted: Tue Mar 17, 2009 3:00 pm 
Post subject: Re: How to create an array?

Thank you,

That seems to work.

Why do you add $forum_id_test, why not use $forum_id only?

_________________
phpBB2 will never die, I hope!
Back to top
Ptirhiik
Board Member



Joined: 19 Nov 2008

Posts: 114


flag
PostPosted: Tue Mar 17, 2009 4:46 pm 
Post subject: Re: How to create an array?

You can indeed write:
Code:
if ( in_array($forum_id, array(1, 2, 3)) )


However, it is very rare an array of ids to be built on the fly, due to authorizations, restrictions, and so. You will more often find this kind of tests, very readable because compact:

Code:
if ( !in_array($mode, array('edit', 'delete', 'create')) )
Back to top
dogs and things
Board Member



Joined: 18 Nov 2008

Posts: 628
Location: Spain


flag
PostPosted: Tue Mar 17, 2009 5:20 pm 
Post subject: Re: How to create an array?

Merci, the first option works fine for what I want it for.

I understand what you say about permissions and such but what I want to do is modify a MOD I use on my posting.php. The rest of the MOD's code takes care of all those things, I only want add the option to only make the MOD's feature show up on the forum I want and this seems to work fine.

Thereīs probably a better way to do what I want but this array is the easy way out.

If anybody is willing to have a look at the MOD's code I'll gladly post it to discuss other options but I'm happy with the way it is now too.

Greetings.

_________________
phpBB2 will never die, I hope!
Back to top
RMcGirr83
Board Member



Joined: 01 Dec 2008

Posts: 53
Location: East Lyme, CT


flag
PostPosted: Tue Mar 17, 2009 5:28 pm 
Post subject: Re: How to create an array?

dogs and things wrote:
Thank you,

That seems to work.

Why do you add $forum_id_test, why not use $forum_id only?


Because $forum_id may already be defined and it was just an example.

_________________
Rich McGirr
Back to top
dogs and things
Board Member



Joined: 18 Nov 2008

Posts: 628
Location: Spain


flag
PostPosted: Tue Mar 17, 2009 6:17 pm 
Post subject: Re: How to create an array?

That's what I guessed you did it for.

What I don't understand is how can the forum_id be taken from the array that you define for forum_id_test?

I mean, using the code as you posted I didnīt expect it to give the forum_id but it did. Where's the magic.

_________________
phpBB2 will never die, I hope!
Back to top
RMcGirr83
Board Member



Joined: 01 Dec 2008

Posts: 53
Location: East Lyme, CT


flag
PostPosted: Tue Mar 17, 2009 9:02 pm 
Post subject: Re: How to create an array?

As far as I know, via Ptirhiik's awesome tutoring, the variable has to be set in the code already.

You could name your array anything you want, but to do the test (in_array) you must have one thing to compare to the array.

Is this a custom script? Sort of difficult to understand how $forum_id is being populated without seeing the code.

_________________
Rich McGirr
Back to top
drathbun
Board Member



Joined: 24 Jul 2008

Posts: 729
Location: Texas


flag
PostPosted: Fri Aug 28, 2009 11:16 pm 
Post subject: Re: How to create an array?

I realize this is an old topic but I want to add my two cents.

I'm reading between the lines, but it seems that the MOD is trying to check to see if a $forum_id value is on a certain list. Only forums on that list have something special available to them, so the original question was looking for an effective way to do that. If I might offer a different approach...

There are a number of MODs that let me set certain flags by forum. The best example is whether the post count increments or not, such as the Off Topic forum here. Rather than hard-coding the forum ID in an array or otherwise, I add a column to the phpbb_forums table.

Code:
alter table phpbb_forums
add new_flag tinyint(1) unsigned default 0

The default is zero if I want the feature to be off by default, or 1 if I want the feature to be on by default. It's then fairly simple to add a field in the admin panel to let me toggle the flag on or off.

Finally, on any page where I reference a forum, I make sure that flag is part of the query for forum information. If the query includes:
Code:
select f.*
from ' . FORUMS_TABLE . ' f
where...

... then the flag is already there. If the SQL code selects a specific field list, then I add my flag. At that point I quit checking to see if the forum is on a list, and just check the flag.

So, again using the post count as an example, here's how that code looks on this site.

In posting.php the SQL looks like this:

Code:
select f.*, other stuff


Later I set the flag using this:
Code:
        // BEGIN Forum Post Count
        $forum_post_count = $post_info['post_count'];
        // END Forum Post Count


That's easy enough. From that point forward, I can reference that flag or pass it as a function argument or do whatever I want, and I don't have to worry about checking for a forum on a list. Plus, if I change the forum configuration via the Admin panel I don't have to edit any code. icon_smile.gif

I hope that helps... and that it was at least remotely close to why the question was asked in the first place.

_________________
phpBBDoctor Blog
Back to top
dogs and things
Board Member



Joined: 18 Nov 2008

Posts: 628
Location: Spain


flag
PostPosted: Sat Aug 29, 2009 3:35 am 
Post subject: Re: How to create an array?

It actually is pretty close to what I want Dave. icon_wink.gif

Quote:
It's then fairly simple to add a field in the admin panel to let me toggle the flag on or off.
Now this is the point that has me thinking about how to do this. icon_razz.gif

The MOD I want to mod is a topic and post helpful points MOD.
As the MOD is now it is either ON for all forums or OFF for all forums.

I want to be able to turn it ON or OFF on a per forum basis.

So I will have to add a checkbox to the admin/admin_forums.php to add a flag to the forums settings in the phpbb_forums table.

I'll see if I'll be able to do this, I guess that I might be able but it'll cost me a lot of work. And that's ok as I'll learn from it and it keeps me entertained. icon_razz.gif

If any unsolvable problems come up I'll get back here. icon_smile.gif

Thanks for the info.

_________________
phpBB2 will never die, I hope!
Back to top
drathbun
Board Member



Joined: 24 Jul 2008

Posts: 729
Location: Texas


flag
PostPosted: Sat Aug 29, 2009 10:27 am 
Post subject: Re: How to create an array?

You can get a good template on how to add a forum flag by reviewing the instructions for my "Forum Auth by Post Count" MOD. It includes four new fields added to the forums table. First, some SQL... here you would obviously add only the field(s) you need rather than these...
Code:
#
#-----[ SQL ]-------------------------------------
# Special Instructions: Be sure to change the prefix phpbb_ to
# whatever is required for your installation
#
alter table phpbb_forums add min_posts_to_view mediumint(8) default 0;
alter table phpbb_forums add max_posts_to_view mediumint(8) default -1;
alter table phpbb_forums add min_posts_to_post mediumint(8) default 0;
alter table phpbb_forums add max_posts_to_post mediumint(8) default -1;


Next, the code changes for admin_forums:
Code:
#
#-----[ OPEN ]-------------------------------------
#
admin/admin_forums.php

#
#-----[ FIND ]-------------------------------------
# On or about line 268; find text might not be a complete line
#
            $forumstatus = $row['forum_status'];

#
#-----[ AFTER, ADD ]-------------------------------------
#
            // BEGIN Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)
            $min_posts_to_view = $row['min_posts_to_view'];
            $max_posts_to_view = $row['max_posts_to_view'];
            $min_posts_to_post = $row['min_posts_to_post'];
            $max_posts_to_post = $row['max_posts_to_post'];
            // END Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)


#
#-----[ FIND ]-------------------------------------
# On or about line 297; find text might not be a complete line
#
$forumdesc = '';

#
#-----[ BEFORE, ADD ]-------------------------------------
#
            // BEGIN Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)
            $min_posts_to_view = DEFAULT_MIN_POSTS_TO_VIEW;
            $max_posts_to_view = DEFAULT_MAX_POSTS_TO_VIEW;
            $min_posts_to_post = DEFAULT_MIN_POSTS_TO_POST;
            $max_posts_to_post = DEFAULT_MAX_POSTS_TO_POST;
            // END Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)


#
#-----[ FIND ]-------------------------------------
# On or about line 327; find text might not be a complete line
#
'S_PRUNE_ENABLED' =>

#
#-----[ AFTER, ADD ]-------------------------------------
#
            // BEGIN Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)
            'MIN_POSTS_TO_VIEW' => $min_posts_to_view,
            'MAX_POSTS_TO_VIEW' => $max_posts_to_view,
            'L_POSTS_TO_VIEW' => $lang['Posts_to_view'],
            'MIN_POSTS_TO_POST' => $min_posts_to_post,
            'MAX_POSTS_TO_POST' => $max_posts_to_post,
            'L_POSTS_TO_POST' => $lang['Posts_to_post'],
            'L_MIN' => $lang['Min_post_limit'],
            'L_MAX' => $lang['Max_post_limit'],
            // END Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)


#
#-----[ FIND ]-------------------------------------
# On or about line 395; find text might not be a complete line
#
forum_desc, forum_order, forum_status,

#
#-----[ IN-LINE FIND ]-------------------------------------
#
forum_status,

#
#-----[ IN-LINE AFTER, ADD ]-------------------------------------
#
min_posts_to_view, max_posts_to_view, min_posts_to_post, max_posts_to_post,

#
#-----[ FIND ]-------------------------------------
# On or about line 396; find text might not be a complete line
#
intval($HTTP_POST_VARS['forumstatus']) . ", "

#
#-----[ IN-LINE FIND ]-------------------------------------
#
intval($HTTP_POST_VARS['forumstatus']) . ", "

#
#-----[ IN-LINE AFTER, ADD ]-------------------------------------
#
. intval($HTTP_POST_VARS['min_posts_to_view']) . ", " . intval($HTTP_POST_VARS['max_posts_to_view']) . ", " . intval($HTTP_POST_VARS['min_posts_to_post']) . ", " . intval($HTTP_POST_VARS['max_posts_to_post']) . ", "

#
#-----[ FIND ]-------------------------------------
# On or about line 435; find text might not be a complete line
#
"', forum_status = " . intval($HTTP_POST_VARS['forumstatus']) .

#
#-----[ IN-LINE FIND ]-------------------------------------
#
intval($HTTP_POST_VARS['forumstatus']) .

#
#-----[ IN-LINE AFTER, ADD ]-------------------------------------
#
", min_posts_to_view = " . intval($HTTP_POST_VARS['min_posts_to_view']) . ", max_posts_to_view = " . intval($HTTP_POST_VARS['max_posts_to_view']) . ", min_posts_to_post = " . intval($HTTP_POST_VARS['min_posts_to_post']) . ", max_posts_to_post = " . intval($HTTP_POST_VARS['max_posts_to_post']) .


That gives you all the places you need to add your new field as far as adding a new forum / editing an existing forum. Next, the language entries:
Code:
#
#-----[ OPEN ]-------------------------------------
#
language/lang_english/lang_admin.php

#
#-----[ FIND ]-------------------------------------
# On or about line 766; find text might not be a complete line
#
?>

#
#-----[ BEFORE, ADD ]-------------------------------------
#
// BEGIN Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)
$lang['Posts_to_view'] = 'Posts required to view this forum';
$lang['Posts_to_post'] = 'Posts required to post in this forum';
$lang['Min_post_limit'] = 'Minimum';
$lang['Max_post_limit'] = 'Maximum, use -1 for no limit';
// END Forum Auth by Post Count 1.0.0 (www.phpBBDoctor.com)

Finally, the template file
Code:
#
#-----[ OPEN ]-------------------------------------
#
templates/subSilver/admin/forum_edit_body.tpl

#
#-----[ FIND ]-------------------------------------
# On or about line 25 to 26; find text might not be a complete line
#
     <td class="row2"><select name="forumstatus">{S_STATUS_LIST}</select></td>
   </tr>


#
#-----[ AFTER, ADD ]-------------------------------------
#
   <tr>
     <td class="row1">{L_POSTS_TO_VIEW}</td>
     <td class="row2">{L_MIN}: <input type="text" name="min_posts_to_view" size="10" value="{MIN_POSTS_TO_VIEW}" />&nbsp;{L_MAX}: <input type="text" name="max_posts_to_view" size="10" value="{MAX_POSTS_TO_VIEW}" /></td>
   </tr>
   <tr>
     <td class="row1">{L_POSTS_TO_POST}</td>
     <td class="row2">{L_MIN}: <input type="text" name="min_posts_to_post" size="10" value="{MIN_POSTS_TO_POST}" />&nbsp;{L_MAX}: <input type="text" name="max_posts_to_post" size="10" value="{MAX_POSTS_TO_POST}" /></td>
   </tr>

This will hopefully help. I find that once I've written something (like adding a new field to the user profile, or adding a new field for the forums table) I go back and reuse the edit pattern rather than writing it from scratch each time. So while these fields are input fields rather than checkboxes, you can use them as a start.

Look at how the "Prune Enabled" logic works to add your new checkbox.

Last, you would go into your other pages (index, viewforum, viewtopic, posting, whatever you need) and make sure you retrieve the new field and use it as needed.

Next, the template file for the admin page,

_________________
phpBBDoctor Blog
Back to top
Display posts from previous:   
Register or Login to Post    Index » MOD Writing  Previous TopicPrint TopicNext Topic
Page 1 of 1 All times are GMT - 4 Hours
 
Jump to:  

Index • About • FAQ • Rules • Privacy • Search •  Register •  Login 
Not affiliated with or endorsed by the phpBB Group
Powered by phpBB2 © phpBB Group
Generated in 0.0685 seconds using 16 queries. (SQL 0.0105 Parse 0.0012 Other 0.0569)
phpBB Customizations by the phpBBDoctor.com
Template Design by DeLFlo and MomentsOfLight.com Moments of Light Logo