How do I reference outside of my user defined function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
lanlord
Forum Newbie
Posts: 13
Joined: Wed Sep 18, 2002 10:02 am

How do I reference outside of my user defined function?

Post by lanlord »

I need to reference a global multidimensional array inside my function, but $GLOBALS["CSSArray["$arg_1"]["$arg_2"]"] doesn't seem to work. Has anyone else run into this and solved it?



// function to read the database. it will print out the options
function printOptions($arg_1, $arg_2) {
$query = "SELECT * FROM style WHERE tag = '$arg_1' AND attrib = '$arg_2' ORDER BY val";
$result = mysql_db_query($GLOBALS["db"], $query, $GLOBALS["connection"]);
while($myrow = mysql_fetch_array($result)) {
if ($CSSArray["$arg_1"]["$arg_2"] == $myrow["val"]) {
print " <OPTION SELECTED VALUE=\"" . $myrow["val"] . "\">" . $myrow["val"] . "</OPTION>\n";
} else {
print " <OPTION VALUE=\"" . $myrow["val"] . "\">" . $myrow["val"] . "</OPTION>\n";
}
}
mysql_free_result($result);
}
User avatar
jonsyd
Forum Commoner
Posts: 36
Joined: Fri Sep 20, 2002 9:28 am
Location: Oxford, UK

Post by jonsyd »

How about:

$GLOBALS["CSSArray"]["$arg_1"]["$arg_2"]

J
User avatar
lanlord
Forum Newbie
Posts: 13
Joined: Wed Sep 18, 2002 10:02 am

duh

Post by lanlord »

Thanks, that's it! I bow before your greatness.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Something that may mean less typing for you - you don't need quotes around variable names so:

Code: Select all

$GLOBALS&#1111;"CSSArray"]&#1111;"$arg_1"]&#1111;"$arg_2"]
can become

Code: Select all

$GLOBALS&#1111;'CSSArray']&#1111;$arg_1]&#1111;$arg_2]
Mac
User avatar
lanlord
Forum Newbie
Posts: 13
Joined: Wed Sep 18, 2002 10:02 am

Post by lanlord »

I see that, Mac. Although I don't follow the theory perfectly, I'm trying to have it the same way always. In other words, if I use the double quotes with a string, I want to use them with the variables too.

Thanks for the tip tho, I'm always looking for just this sort of information. :D
Post Reply