1

Controller

public function execute()
    {

        $bags_result = array(   'height'=>$height,
                                'weight'=>$weight
                            );

        $result = $this->resultJsonFactory->create();
        $resultPage = $this->resultPageFactory->create();

        $block = $resultPage->getLayout()
                ->createBlock('Vendor\Module\Block\Index')
                ->setTemplate('Vendor_Module::calculation.phtml')
                ->setData('bags_result',$bags_result)
                ->toHtml();

        $result->setData(['output' => $block]);
        return $result;
    } 

Block File function

public function getBagsResultData()
    {
        return $this->getBagsResult();
    }

calculations.phtml

<label><?php echo __('Height')?><span id="final-bmi-data"><?php echo $this->getBagsResultData('height')?></span></label>
<label><?php echo __('Weight')?><span id="final-bmi-data"><?php echo $this->getBagsResultData('weight')?></span></label>

Even if I change and use $block->getBagsResultData(); it gives the below error.

Error: 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion

How do I fetch and display array data set in controller using block or directly in phtml ?

2

Try this

public function getBagsResultData($var)
    {
        return $this->getBagsResult($var);
    }

or

 public function getBagsResultData($var)
    {
        $data = $this->getBagsResult();
        return $data[$var];
    }

In phtml you are passing the index but in method you output the array.

 <label><?php echo __('Height')?><span id="final-bmi-data"><?php echo $this->getBagsResultData('height')?></span></label>
<label><?php echo __('Weight')?><span id="final-bmi-data"><?php echo $this->getBagsResultData('weight')?></span></label>
1
  • 1
    It's a good way. Upvoted :) Oct 10 '17 at 11:46
0

In controller, you have to convert array in string and then send the data.

In phtml file, you have to explode the string.

Controller

public function execute()
    {


        $bags_height = 10;
        $bags_weight = 20;

        $result = $this->resultJsonFactory->create();
        $resultPage = $this->resultPageFactory->create();

        $block = $resultPage->getLayout()
                ->createBlock('Vendor\Module\Block\Index')
                ->setTemplate('Vendor_Module::calculation.phtml')
                ->setData('height',$bags_height)
                ->setData('weight',$bags_weight)
                ->toHtml();

        $result->setData(['output' => $block]);
        return $result;
    } 

Block File function

public function getBagsHeight()
    {
        return $this->getHeight();
    }

    public function getBagsWeight()
    {
        return $this->getWeight();
    }

calculations.phtml

<label><?php echo __('Height')?><span id="final-bmi-data"><?php echo $block->getBagsHeight(); ?></span></label>
<label><?php echo __('Weight')?><span id="final-bmi-data"><?php echo $block->getBagsWeight(); ?></span></label>
6
  • We didn't implode twice. We imploded in controller and exploded in phtml file. check the answer. Oct 10 '17 at 10:33
  • But then it will give - Notice: Undefined index as we are converting it in array Oct 10 '17 at 10:40
  • comment your full error Oct 10 '17 at 10:42
  • The array generates like this - Array([0]=>11, [1]=>10). It gives index and not array key like height or weight Oct 10 '17 at 10:46
  • Why are you passing array in $bags_result ? set data seperately for both height and weight Oct 10 '17 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.