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
anthony88guy
Forum Contributor
Posts: 246 Joined: Thu Jan 20, 2005 8:22 pm
Post
by anthony88guy » Sat Jul 22, 2006 3:23 pm
I'm really glad I finally tried classes. But I'm a newbie and here is my problem:
Code: Select all
class Main
{
function config()
{
global $tb;
$config = array();
$query['config'] = mysql_query("SELECT * FROM `" . $tb['config'] . "") or die(mysql_error());
while($row = mysql_fetch_array($query['config']))
{
$config[$row['config_name']] = $row['config_value'];
}
return $config;
}
}
Code: Select all
$main = new Main;
$main->config();
print_r($config);
Notice: Undefined variable: config in /home/*/public_html/includes/header.php on line 28
If I do
inside the function config() it works.
What I want to do is when I call $main->config(); I have an array with my config settings.
Last edited by
anthony88guy on Sat Jul 22, 2006 3:47 pm, edited 1 time in total.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Jul 22, 2006 3:26 pm
The actual variable $config will only exist within the
scope of the function. What your wanting to do is
Code: Select all
$main = new Main;
print_r($main->config());
anthony88guy
Forum Contributor
Posts: 246 Joined: Thu Jan 20, 2005 8:22 pm
Post
by anthony88guy » Sat Jul 22, 2006 3:33 pm
Jcart wrote: The actual variable $config will only exist within the
scope of the function. What your wanting to do is
Code: Select all
$main = new Main;
print_r($main->config());
Output:
Array ( [test] => 500 )
Great it works,
Now I need to print out the value of 'test' in the array? How would I go about doing that?
Thanks...
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sat Jul 22, 2006 3:37 pm
Like you would any other array.
Code: Select all
$main = new Main;
$config = $main->config();
echo $config['test'];