Page 1 of 1

Another Class problem [SOLVED]

Posted: Sat Jul 22, 2006 3:23 pm
by anthony88guy
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

Code: Select all

print_r($config);

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.

Posted: Sat Jul 22, 2006 3:26 pm
by John Cartwright
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());

Posted: Sat Jul 22, 2006 3:33 pm
by anthony88guy
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...

Posted: Sat Jul 22, 2006 3:37 pm
by John Cartwright
Like you would any other array.

Code: Select all

$main = new Main;
$config = $main->config();

echo $config['test'];

Posted: Sat Jul 22, 2006 3:43 pm
by anthony88guy
wow... thank you very much.