Posts: 73
Threads: 6
Joined: Dec 2007
Reputation:
0
2008-01-12, 07:30 PM
(This post was last modified: 2008-01-12, 07:31 PM by AirborneFive.)
Ok, another question for you all
We need to be able to include a certain range of forum id's in an IF/ELSE statement and need a better way of doing it than we're currently using.
Right now, we have 3 forum id's and are using:
if ($fid == '1' OR $fid == '2')
{
show this
} else {
show that
}
We're possibly going to be using up to 10 forum id's for this and making multiple modifications through many files and need an easier way of updating this each time we add a forum id to the list.
I've not had any luck using in_array or array, probably because I am missing something, so I am hoping someone here will know the answer.
Any help would be greatly appreciated!
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Posts: 3,003
Threads: 20
Joined: Feb 2006
Reputation:
9
$fids = array('x','y','z');
if(in_array($fid, $fids))
{
...
}
else
{
...
}
Posts: 4,639
Threads: 68
Joined: Mar 2007
Reputation:
42
2008-01-12, 11:06 PM
(This post was last modified: 2008-01-12, 11:06 PM by Yumi.)
I would use a switch - it gives the best performance:
switch($fid)
{
case 1:
case 2:
case 3:
...etc...
}
If you must use an array, in_array works fine, though I would prefer using array keys for larger arrays (or arrays searched multiple times) as it's much faster for PHP to search for a key than it is to perform a linear search on an array. Plus, isset() is a language construct, so you don't have the extra delay of an additional function call
Eg:
$fids = array(1 => 1, 2 => 1, 3 => 1);
if(isset($fids[$fid]))
{
}
else
{
}
Posts: 73
Threads: 6
Joined: Dec 2007
Reputation:
0
Thanks much
Only issue I am having now is to get it set in just one file so I don't have to replicate the same across multiple files. I've tried placing it in global.php though it's not really working.
A good deal of the modifications reside in functions_post.php, functions_forumlist.php, forumdisplay.php and showthread.php.
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Posts: 4,639
Threads: 68
Joined: Mar 2007
Reputation:
42
If you're placing it globally, and calling from within a function, make sure to reference the correct scope, either using using global directive, or using the $GLOABLS array, eg:
In global.php
$mystuff = array(1, 2, 3);
In some function:
function myfunction()
{
global $mystuff;
var_dump($mystuff); //works
}
function myfunction2()
{
var_dump($GLOBALS['mystuff']); // works too
}
Posts: 73
Threads: 6
Joined: Dec 2007
Reputation:
0
Right now, we're not using a function, just the IF/ELSE referenced above, however, we use the same IF/ELSE multiple times, which is why we were looking to place it in global.php.
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Posts: 4,639
Threads: 68
Joined: Mar 2007
Reputation:
42
Two methods:
1) Place the code in a function - ie:
function is_valid_forum()
...
2) Place a global switch - ie
if(...)
$validForum = true;
else
$validForum = false;
Posts: 73
Threads: 6
Joined: Dec 2007
Reputation:
0
ZiNga BuRgA Wrote:I would use a switch - it gives the best performance:
switch($fid)
{
case 1:
case 2:
case 3:
...etc...
}
If you must use an array, in_array works fine, though I would prefer using array keys for larger arrays (or arrays searched multiple times) as it's much faster for PHP to search for a key than it is to perform a linear search on an array. Plus, isset() is a language construct, so you don't have the extra delay of an additional function call
Eg:
$fids = array(1 => 1, 2 => 1, 3 => 1);
if(isset($fids[$fid]))
{
}
else
{
}
Thank you very much, the forum keys actually worked out better in the end .
Given we're going to be running 10-20 forum id's through the array, would their be a more efficient method? MyBB doesn't use $fid in all of the PHP files from what I've seen, it alternates between $fid and $forum['fid'].
We're moving between base php files, such as forumdisplay and showthread to the function_* files, which is why I ask.
Thanks again everyone!
- Jon T. & Kim B.
- AirborneFive Co-Owner's
- Optimized Web Hosting Solutions | Starting @ Only $2/mo
- First Month of Service Free! | Coupon: FIRSTMONTHFREE
Posts: 4,639
Threads: 68
Joined: Mar 2007
Reputation:
42
2008-01-13, 06:47 AM
(This post was last modified: 2008-01-13, 06:48 AM by Yumi.)
For 10-20 IDs, efficiency should be a very minor factor, so long as you don't continually keep executing the same thing over again.
For grabbing an FID, this should work:
global $foruminfo, $forum, $fid;
if(!isset($fid))
{
if(isset($foruminfo['fid']))
$fid = $foruminfo['fid'];
elseif(isset($forum['fid']))
$fid = $forum['fid'];
else
$fid = 0; //not in a forum
}
|