UBB.Dev
Posted By: Ian_W Can this be shortened? - 08/23/2003 2:20 AM
Hi,

I can't find a way - but just wondered if this piece of code could be shortned in any way?

Code
<br /><br />elseif ( (strstr($user['U_Groups'], '-5-'))  ||  (strstr($user['U_Groups'], '-501-'))   ||  (strstr($user['U_Groups'], '-502-'))  ||  (strstr($user['U_Groups'], '-522-'))  ) {<br />$leaguelink = "<a href = \"$phpurl/conference\" $target>Conference Forums</a> | ";<br /><br />  


I have curtailed the example above otherwise it would have been far too long - but essentially I could have 70 + groups that would result in the leaguelink being set to a particular URL.

As I have 30 different possible links, I am going to have some very long lines in ubbt.inc.php - so just wondered if the string can be written better?

Posted By: Dave_L_dup1 Re: Can this be shortened? - 08/23/2003 3:12 AM
First of all, if you're using that if-condition more than once, write a function that returns true/false, so you don't have to duplicate code.

Code
function in_particular_groups($groups) {<br />   return<br />      strstr($groups, '-5-')   or<br />      strstr($groups, '-501-') or<br />      ... ;<br />}


Then you can do:

Code
elseif (in_particular_groups($user['U_Groups'])) {<br />   $leaguelink = "<a href = \"$phpurl/conference\" $target>Conference Forums</a> | ";


I assume that the group numbers you're checking don't all start with "5" or something like that, so you can't do a regex match?

Offhand I can't think of anything shorter, but if I think of anything I'll post it.
Posted By: Ian_W Re: Can this be shortened? - 08/23/2003 3:19 AM
Thanks. No the groups vary.

I will look at the use of a function.
Posted By: Dave_L_dup1 Re: Can this be shortened? - 08/23/2003 9:43 PM
Here's another approach that may be more efficient:

Code
$particular_groups = array( <br />   5, <br />   501, <br />   502, <br />   522, <br />); <br /> <br />elseif (array_intersect(explode('-', $user['U_Groups']), $particular_groups)) { <br />   $leaguelink = "<a href = \"$phpurl/conference\" $target>Conference Forums</a> | ";


Posted By: Ian_W Re: Can this be shortened? - 08/23/2003 11:19 PM
Thanks - some of that is new to me - so I will look into it.

Thanks.
© UBB.Developers