david4456 Posted August 11, 2010 Share Posted August 11, 2010 Hi All and thank for your help, Below is the current code: <? class jobs extends generic_module { var $allowed = Array( '1', '5', '6' ); } ?> What I have been trying to do is get the array out of a database insead of having to manually add them to the above code. once again thank for any help you can provide. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/210445-array-help/ Share on other sites More sharing options...
Adam Posted August 11, 2010 Share Posted August 11, 2010 Okay, and what have you tried so far? Also you'll need to define the array within the constructor of your class. Quote Link to comment https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098153 Share on other sites More sharing options...
david4456 Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks for the reply. the class generic.php code is below <?php class generic_module { var $company_id = 0; var $allowed = Array(); function __construct( $company_id=0 ) { $this->company_id = $company_id; } function valid() { return in_array( $this->company_id, $this->allowed ); } function buffer() { ob_start(); $this->render(); $contents = ob_get_contents(); ob_end_clean(); return $contents; } function admin_only() { global $admin; if ( !$admin['admin'] ) die('Insufficent Privliages'); } } ?> the class module.php code is below public static function allowed( $module ) { $root = $_SERVER['DOCUMENT_ROOT']; $module_name = strtolower($module); $module_file = $module_name . ".php"; $module_folder = "modules"; $general_file = $root . DS . $module_folder . DS . $module_file; if ( file_exists( $general_file ) ) { require_once( $general_file ); $$module_name = new $module_name(0); return $$module_name->allowed; } else { return false; } } We have tried just about ever combination of the following code: <?php class jobs extends generic_module { $root = $_SERVER['DOCUMENT_ROOT']; require_once( $root . "/classes/config.php" ); require_once( $root . "/classes/mysql.php" ); $mysql = new mysql(); $mysql->connect(); $query = 'SELECT * FROM Company WHERE `jobs` = 1'; $results = $mysql->query( $query ) or die( mysql_error() ); while( $row = mysql_fetch_array( $results ) ) { var $allowed = $row["company_id"]; } } ?> I think I have the problem of I can not see the forest thru the trees now any help would be great. Quote Link to comment https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098307 Share on other sites More sharing options...
Adam Posted August 12, 2010 Share Posted August 12, 2010 As I mentioned before you need to place the code within the constructor of your class, outside of methods you can only declare properties: class jobs extends generic_module { public function __construct() { $root = $_SERVER['DOCUMENT_ROOT']; require_once( $root . "/classes/config.php" ); require_once( $root . "/classes/mysql.php" ); $mysql = new mysql(); $mysql->connect(); $query = 'SELECT * FROM Company WHERE `jobs` = 1'; $results = $mysql->query( $query ) or die( mysql_error() ); while( $row = mysql_fetch_array( $results ) ) { var $allowed = $row["company_id"]; } } } One thing to note is that any variables defined not as class properties (i.e. $foo instead of $this->foo) will only be created as local variables in the constructor. That means from other methods you won't be able to access them. If that doesn't make much sense I'd read up on variable scope before you continue. Quote Link to comment https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098446 Share on other sites More sharing options...
Nosbod Posted August 12, 2010 Share Posted August 12, 2010 Hello Change var $allowed = $row["company_id"]; To var $allowed[] = $row["company_id"]; At the moment $allowed is just a variable that you are overwriting in the loop adding [] turns $allowed into array adding $row['company_id'] to the next key value, 1,2 etc. Quote Link to comment https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098449 Share on other sites More sharing options...
david4456 Posted August 13, 2010 Author Share Posted August 13, 2010 Hi Nosbod & MrAdam, Thanks for your help. the code sugestion have not worked would you have any more idears as I never wrote the orginal code I am not sure if the variable will need to formated in the excat same way as the orginal string var $allowed = Array( '1', '5', '6' ); Please let me know what you think. Thanks again for your help. Quote Link to comment https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.