PDA

View Full Version : Using an array in an if statement....


Link14716
02-01-2003, 11:32 PM
I want to know how to use several values, seperated by commas, in an if statement. This is probably very basic, but I need to know. Thanks :)

mr e
02-02-2003, 12:36 AM
can you be more specific?

Link14716
02-02-2003, 12:42 AM
Say a variable contains this:
5,6,7,10,56,63
I want to drop an if statement, checking each of them.
if ($bbuserinfo[usergroupid]=="5" OR $bbuserinfo[usergroupid]=="6" OR $bbuserinfo[usergroupid]=="7" OR $bbuserinfo[usergroupid]=="10" OR $bbuserinfo[usergroupid]=="56" OR $bbuserinfo[usergroupid]=="63") {
How would I make it so that all of that is combined into something like
if ($bbuserinfo[usergroupid]==$variable) {
Where variable has all of those values mentioned above and actualy work?

mr e
02-02-2003, 01:06 AM
well this is what i would do...


$vars = "1,2,4,5,9";
$var = explode (",", $vars);

if (preg_match($var, $bbuserinfo[userid])) {
do code here;
} else {
more code here;
}


i think that should work

Xenon
02-02-2003, 11:42 AM
Link, you should read more of my posts, i use the correct code very often:


if(in_array($bbuserinfo[usergroupid], array(5,6,7,10,56,63
)) {

}

Link14716
02-02-2003, 05:33 PM
That's what it is. I just didn't remember it becuase I haven't had any need for it until now. ;)

Thanks. :)

mr e
02-02-2003, 05:56 PM
i'll have to remember that :D

Link14716
02-02-2003, 07:02 PM
In forumdisplay.php, I have this code:
if (in_array($bbuserinfo[usergroupid], array($privateusergroupable))) {
$getthreadids=$DB_site->query("
SELECT
".iif($sortfield=="voteavg",$votequery,"")."
thread.threadid
FROM thread
WHERE thread.forumid = $foruminfo[forumid]
AND thread.sticky=0
$datecut
$limitothers
ORDER BY sticky DESC, $sortfield $sqlsortorder
LIMIT ".($sel_limitlower-1).",$perpage");
} else {
$getthreadids=$DB_site->query("
SELECT
".iif($sortfield=="voteavg",$votequery,"")."
thread.threadid
FROM thread
WHERE thread.forumid = $foruminfo[forumid]
AND thread.sticky=0
AND thread.visible=1
$datecut
$limitothers
ORDER BY sticky DESC, $sortfield $sqlsortorder
LIMIT ".($sel_limitlower-1).",$perpage");
}
But this doesn't seem to be working - it doesn't show the invisible threads, even though usergroupid 6 is in the array. Any ideas?

Furthermore, if the variable $allowusercreateprivate is set to 1, I want it to get all of that user's threads even if visible!=1. Any ideas on that?

mr e
02-02-2003, 07:20 PM
try this


if (in_array($bbuserinfo[usergroupid], $privateusergroupable)) {
$getthreadids=$DB_site->query("
SELECT
".iif($sortfield=="voteavg",$votequery,"")."
thread.threadid
FROM thread
WHERE thread.forumid = $foruminfo[forumid]
AND thread.sticky=0
$datecut
$limitothers
ORDER BY sticky DESC, $sortfield $sqlsortorder
LIMIT ".($sel_limitlower-1).",$perpage");
} else {
$getthreadids=$DB_site->query("
SELECT
".iif($sortfield=="voteavg",$votequery,"")."
thread.threadid
FROM thread
WHERE thread.forumid = $foruminfo[forumid]
AND thread.sticky=0
AND thread.visible=1
".iif($allowusercreateprivate==1,"AND thread.visible=0","")."
$datecut
$limitothers
ORDER BY sticky DESC, $sortfield $sqlsortorder
LIMIT ".($sel_limitlower-1).",$perpage");
}


not sure if this'll work though

Link14716
02-02-2003, 07:28 PM
Nope...

Warning: Wrong datatype for second argument in call to in_array() in /home/sites/site68/web/forums/forumdisplay.php on line 493

Warning: Missing argument 2 for iif() in /home/sites/site68/web/forums/admin/functions.php on line 618

Warning: Missing argument 3 for iif() in /home/sites/site68/web/forums/admin/functions.php on line 618

mr e
02-02-2003, 07:34 PM
oh well, i didn't now how those if querys work anyways, just a stab in the dark :D

Xenon
02-03-2003, 03:12 PM
link it seems $privateusergroupable is not an array but a string..., instead of that:
if (in_array($bbuserinfo[usergroupid], array($privateusergroupable))) {

try that:

eval("$privatearray = array(".$privateusergroupable.");");
if (in_array($bbuserinfo[usergroupid], array($privatearray))) {

fla5h
02-03-2003, 06:41 PM
Sorry to jump on this thread but how would I alter this


$DB_site->query_first("SELECT username,userid FROM user WHERE userid=$vouchid AND usergroupid!=5");


So that I can input a variable in the admin options say $banuserid and the values of 5,1,2


$DB_site->query_first("SELECT username,userid FROM user WHERE userid=$vouchid AND usergroupid!=$banuserid");

Xenon
02-04-2003, 04:17 PM
its this:
$DB_site->query_first("SELECT username,userid FROM user WHERE userid=$vouchid AND usergroupid NOT IN ($banuserid)");

fla5h
02-05-2003, 07:20 PM
Worked a treat. Thanks once again Xenon.