1

im using codeigntier framework im trying to solve a problem to do with retrieving information from a database for example:

model.php:

public function read(){
   $query = $this->db->get('table');
   return $query->result();
}

controller.php:

  public function doSomething () {
       $exampleArray['name'] = "Bob's Database";
       $getModel = $this->load->model('model','getData');
       $modelData = $this->getData->read();
       // i want to assign the the $modelData to a array element like so
       $exampleArray['modelData'] = $modelData // here's where im stuck :(
}

thanks for your help!! p.s. this is not an error, its just a question :)

}

1 Answer 1

2

If you want to be able to access $exampleArray outside of that method, you'll have to do one of two things:

a) set it as a class variable

class MyClass {
    public $exampleArray;
    .
    .
    . 
}

then refer to it using

$this->exampleArray['index']

or b) pass it by reference into your doSomething() function:

public function doSomething(&$exampleArray) { ... }

The php manual has a section on variable scope that should help you better understand this.

Sign up to request clarification or add additional context in comments.

2 Comments

i think you got me wrong, but nice answer, im not talking about scope here, im trying to assign the array of objects that i get from my model to the $exampleArray['modelData'] variable, i was wondering if i did it right on my code!
Yes, you did. After the assignment, use var_dump($exampleArray) to verify. (You may want to echo <pre> tags around your var_dump() to make it clearer in your browser.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.