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
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
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
Share on other sites

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.

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.