Jump to content

ARRAY HELP


david4456

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/210445-array-help/
Share on other sites

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  :confused:

 

any help would be great.

 

Link to comment
https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098307
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098446
Share on other sites

Hi Nosbod & MrAdam,

 

Thanks for your help.

 

the code sugestion have not worked  :shrug:

 

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.

Link to comment
https://forums.phpfreaks.com/topic/210445-array-help/#findComment-1098769
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.