Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Returning multiple datasets
  • Hi,

    I'm new to this so be gentle..lol

    I was to build a dashboard page full of different charts etc. So I planned to put the sql statements into 1 model named dashboard. This works fine with 1 data set but I'm not sure how to pass both to the page. How do I do this correctly? I'm obviously doing something wrong.

    Below is my current model, below that is my current controller:

    class Model_dashboard extends Orm\Model
    {
       // protected static $_properties = array('Customer_id', 'Name', 'Address1');

        public static function getResults()
    {
        $results = array();
        $results = \DB::query("SELECT
                                 Customer_id
                                ,Name
                                ,Address1
                               FROM
                                 Customer_Details
                              ")->execute()->as_array();
                              
        $results2 = array();
        $results2 = \DB::query("SELECT
                                 Customer_id
                                ,Date
                                ,ChartA_xaxis
                                ,ChartA_series1
                                ,ChartA_series2
                               FROM
                                 Dashboard
                              ")->execute()->as_array();                      
                              
        return $results;
        return $results2;                      

    }}
  • //////////////////////////////////////////////////////////////////////////////////////////////
    Controller starts now:

    /////////////////////////////////////////////////////////////////////////////////////////////
    Class Controller_flot extends Controller_test
    {

    public function action_index()
    {   
        $results = Model_dashboard::getResults();
            
            $this->data['results'] = $results;
            
            $results2 = Model_dashboard::getResults();
            
            $this->data2['results2'] = $results2;
     
    $this->view->title = 'Flot » Flot';
        
        
            $this->view->content = View::forge('pages/flot', $this->data);
            
            $this->view->content = View::forge('pages/flot', $this->data2);
            
            return $this->view;
    }
    }
  • A method can only return one value, that's PHP I'm afraid.

    Create two methods for it, and your problem is solved.
  • So for each query I need to return data for I need to create a new model? Seems a bit inefficient...?
  • I didn't say that. I said method. As in getAddress() and getChartInfo() or something like that.
  • I'm confused now how I can call 2 datasets on one controller? 
  • HarroHarro
    Accepted Answer
    Sorry, but this has nothing to do with FuelPHP, it's plain PHP. Perhaps read over the OOP stuff again?

    Class Controller_flot extends Controller_test
    {
        public function action_index()
        {   
            $data =  array(
                'address' => Model_dashboard::getAddress(),
                'chart' => Model_dashboard::getChart(),
            );
     
            $this->view->title = 'Flot » Flot';
           
            $this->view->content = View::forge('pages/flot', $data);
            return $this->view;
        }
    }

    p.s. if you use Controller_Template as base controller (for your Controller_Test perhaps?), you don't have to return the view, the base controller will take care of this.
  • As I said I'm a newbie but thanks it works now! Probably should have said I was newbie to php!
  • Hi newbie,

    There are some problem in the code.

    1)        $this->view->content = View::forge('pages/flot', $this->data);
    2)        $this->view->content = View::forge('pages/flot', $this->data2);

    The $this->view->content has been overwritten at line 2. So the data in line 1 will never output.
    I believe you are new to programming too. You can learn more about OOP (perhaps PHP OOP as it is different with others).

    Cheers.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion