简体   繁体   中英

Filling an array in PHP Zend Framework and fetching it in the View

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!!

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM