0

To start, I went through many other stack overflow posts about similar things but they never worked for me.

I'm using code igniter in one of my pages, I need to use an array sent from my controller to my page within a function

function brandnames(){
foreach ($database as $value) {
    $Brand = $value['Brand'];
}

The error is saying the $database array is undeclared despite sending the info from the controller.

$data["database"] = $this->Get_model->brandnames();

$this->view("update", $data);

When use the same forloop outside of the function it works but the issue is I need it in the function or ill have 400+ lines of extra code using if statements.

5
  • 3
    $database is in a different scope. You must pass as parameter in your function or have in a object attribute. Commented Nov 23, 2016 at 19:21
  • What are you expecting $Brand to be? A single string or an array? Commented Nov 23, 2016 at 19:24
  • Also, the function doesn't have any return. Commented Nov 23, 2016 at 19:25
  • $Brand is a string. Commented Nov 24, 2016 at 17:44
  • and to Felippe, it doesnt need a return. the code i showed is just the relevant part. Commented Nov 24, 2016 at 17:44

3 Answers 3

1

$database is outside the brandnames() scope so you will you need to pass it into the function like so:

function brandnames($database){
foreach ($database as $value) {
    $Brand = $value['Brand'];
}
$database = $data["database"];
brandnames($database);

I would also recommend that you do all data processing before passing it to the view. In my opinion views should not have functions and should have as little PHP code in them as possible.

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

1 Comment

Hey this worked so thank you. Also, the function isn't for data processing. Its for displaying a different set of values based on whatever the page it was redirected from.
0

Model file change:

function brandnames(){
   $Brand = array();
   foreach ($database as $value) {
    $Brand[] = $value['Brand'];
  }
  return $Brand;
}

Controller:

$data["database"] = $this->Get_model->brandnames();

$this->view("update", $data);

Hope this helps

Comments

0

Try to write more self descriptive clean code,

  • getBrandNames() instead of brandnames
  • $brandNames[] = $value['brand']; instead $Brand = $value['Brand']; (you have change 'Brand' in many places.
  • use spaces and align code properly, fn() {

some sample code

function getBrandNames($data = []) {
    $brandNames = [];
    if ($data) {
        foreach ($data as $value) {
            $brandNames[] = $value['brand'];
        }
    }

    return $brandNames;
}

//pass $database data as an argument or get them within the function by calling the relevant function. Instead of $database use exact name of the data set; ex $brandData

Comments

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.