0

I'm trying to fill an array in my PHP ZF2 application controller and pass it onto the view by doing the following:

$brandIds = $this->getConfig()['brandIds'];
        $array = array();
        $size = count($brandIds);
        for ($i = 0; $i < $size; $i++)
        {
            $brand = $this->getBrandModel()->getBrandById($brandIds[$i]);
            $array = array_fill($i,$size, $brand);
        }
        $signInSession = $this->signInUser->user;
        if(empty($signInSession))
        {
            $this->redirect()->toUrl('/index/landing');
        }
        $viewModel = new ViewModel();
        $viewModel->setVariable("b",$array);
        return $viewModel;

Please note that:

$this->getConfig()['brandIds']

is an static array which I've predefined in the config file like following:

  "brandIds" => array(
        "AUTO" => 16,
        "FINANCE" => 18,
        "EVENTS" => 19,
        "HEALTH" => 21,
        "GADGETS" => 25
    ),

And in the view I'm doing the following:

  <?php foreach ($brands as $bId){ ?>
                <h1> <?=$bId?> </h1>
               <?php }?>

But when I open up the view in my browser, nothing appears. My question here is, or more of a problem is that I don't understand why nothing shows up on my view. Also, how can I access each element of this array which is fetched from the DB. Like if the table in the DB has column called "id", I'd like to access it by following:

<?=$bId['id']?>

And write it out on my view (this is just an example, I wouldn't actually print ID from DB lol)...

Can someone help me out with this guys?

Thanks heaps!!

1 Answer 1

1

first of all the name of the variable is b and not bId as stated on this line $viewModel->setVariable("b",$array);.

Then to print the said value, you have to call in your view $this->b['id'];

Hope this'll work,

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

1 Comment

Oddly? It works now... Not sure why I was unable to access the elements of the array...

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.