D
D
Dmitry2014-04-04 11:54:57
PHP
Dmitry, 2014-04-04 11:54:57

How can I pass a value from one function to another?

The code:

<?php
class Page_Portfolio extends Page_Basic
{
    function actionIndex() {
        $cms_class = cms_class::getInstance();
        $sql =  "SELECT * FROM site_portfoliotheme WHERE portfoliotheme_active='Y'";
        $menu = $cms_class->getRecords($sql, 3);
        
        $sql = "SELECT * FROM site_portfolio WHERE portfolio_active='Y' ORDER BY portfolio_id DESC";
        $viewportf = $cms_class->getRecords($sql, 150, "portfolio_image");
        
        return array("menu"=>$menu, "viewportf"=>$viewportf);
    }
    function actionHtml()
    {
        $cms_class = cms_class::getInstance();
        $sql = "SELECT * FROM site_portfolio WHERE portfolio_active='Y' AND portfolio_theme='".Router::$pageParams["html"]."'";    
  $records = $cms_class->getRecords($sql, 150, "portfolio_image");
        return array("records"=>$records, "menu"=>$menu);
        }
    }

It is necessary to transfer the value of the $menu variable from the first function to the second function in order to return it there in an array. How to check it? Or is there another way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Litvinov, 2014-04-04
@another_dream

you can create a $menu property and access it via $this

<?php
class Page_Portfolio extends Page_Basic{

    private $menu;

    public function actionIndex() {
        $this->menu;
    }

    public function actionHtml(){
         $this->menu;
    }

}

Or maybe you need something like this
<?php
class Page_Portfolio extends Page_Basic{

    public function actionIndex() {

    }

    public function actionHtml(){
         $this->actionIndex();
    }

}

S
Stanislav Markin, 2014-06-05
@stasmarkin

If closer to OOP, then like this, perhaps:

<?php
<?php
class Page_Portfolio extends Page_Basic
{
    function actionIndex() {
        $sql = "SELECT * FROM site_portfolio WHERE portfolio_active='Y' ORDER BY portfolio_id DESC";
        $viewportf = cms_class::getInstance()->getRecords($sql, 150, "portfolio_image");
        return array("menu"=>$this->getMenu(), "viewportf"=>$viewportf);
    }
  
  function getMenu() {
    $cms_class = cms_class::getInstance();
    return $cms_class->getRecords($sql, 3);
  }
  
    function actionHtml()    {
        $cms_class = cms_class::getInstance();
        $sql = "SELECT * FROM site_portfolio WHERE portfolio_active='Y' AND portfolio_theme='".Router::$pageParams["html"]."'";    
    $records = $cms_class->getRecords($sql, 150, "portfolio_image");
        return array("records"=>$records, "menu"=>$this->getMenu());
        }
    }

And if you need to optimize, then you can cache getMenu () with one hand movement

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question