Welcome Guest, Not a member yet? Register   Sign In
Passing an Array from a Model to a Controller?
#1

[eluser]Solarpitch[/eluser]
Hey Guys,

Just wondering if it possible to pass an array from a Model to a Controller. Basically in the below model function I need to return both the results of the query and the pagination to the controller for output in the view.

Code:
function sales_breakdown()
{  
$this->load->library('pagination');
    
$query = $this->client->query("SELECT * FROM sales");
    
if ($query->num_rows() > 0)
{
$this->display .=
" <table border='0' cellspacing='0' cellpadding='0' id='breakdown_table'>
<tr>
<td>Item</td>
<td>Price</td>
<td>Type</td>
<td>Category</td>
<td>Time</td>
</tr>";


foreach ($query->result() as $row)
{
        
$this->display .=
"
<tr >
<td>".$row->item."</td>
<td>&euro;".$row->price."</td>
<td>".$row->type."</td>
<td>".$row->temp3."</td>
<td>".$row->time."</td>
</tr>";
       }
    }

$config['base_url'] = 'http://mysite.ie/index.php/enterprise/sales_breakdown/';
$config['total_rows'] = '200';
$config['per_page'] = '20';
$this->pagination->initialize($config);
$this->display .= "</table>";
    
return $this->display;
}

You see, instead of
Code:
return $this->display;
I'd like to set an array like...
Code:
$myarray = array();

//then do something like...

$myarray[0] = //this would be what I have set as "$this->display .=" at the min
$myarray[1] = //which would be equal to "$this->pagination->initialize($config);"

//then I could return the array to the controller with both the formatted query and the pagination links

return $myarray;

Not sure how I could get the value of $myarray[0] and $myarray[1] in the controller...

Code:
function sales_breakdown(){
    
$this->load->helper('url');
        
$this->load->model('report_model');
        
$data['sales_breakdown'] = $this->report_model->sales_breakdown(); // ???
    
$data['right_box'] = "right_box/sales_breakdown";
$this->load->view('template/page_1', $data);
        
    }
#2

[eluser]Solarpitch[/eluser]
Come to think of it. I wouldnt have to do anything with the controller but I could probably do something like this in the view.

Code:
&lt;? echo $sales_breakdown[0]; ?&gt; //The formatted results

&lt;? echo $sales_breakdown[1]; ?&gt; //The pagination

This might work?
#3

[eluser]xwero[/eluser]
Why do you add the pagination to the model method? Why do you add a html output to the method? Both things are view related.

The initialize method doesn't return anything so if you want to return something from the pagination to the controller it would be the config array.

To answer your question, just create an array with the items you want to return
Code:
return array($this->display,$config);
And you get it in the controller
Code:
$temp = $this->report_model->sales_breakdown();
$data['sales_breakdown'] = $temp[0];
#4

[eluser]xwero[/eluser]
[quote author="Solarpitch" date="1231264077"]
Code:
&lt;? echo $sales_breakdown[0]; ?&gt; //The formatted results

&lt;? echo $sales_breakdown[1]; ?&gt; //The pagination
[/quote]
I think you want the return of the pagiantion class but that is returned by the create_links method, so you are missing a step.
#5

[eluser]Solarpitch[/eluser]
[quote author="xwero" date="1231264537"]Why do you add the pagination to the model method? Why do you add a html output to the method? Both things are view related.
[/quote]

I was thinking that but if the model is to handle DB activity I dont understand where else I would put the html output if I didnt want to have that query in my Controller. It just makes sense to me to do it this way unless I'm missing something?

Are you not supposed to add pagination to the model? As above I dont understand how pagination can work in the controller when id have my DB activity in the model. I'm a little confused now Smile
#6

[eluser]therealmaloy[/eluser]
Solarpitch

Here's a sample code for you to base with, might not be perfect, i haven't totally run this, but i guess the ideas and implementation are all in here... good luck.

let me know if something comes up Smile

Model //assume the model class exists and the function below to be in it
Code:
function sales_breakdown($limit = FALSE, $offset = FALSE)
    {
        $this->db->limit($limit);
        $this->db->offset($offset);

        $query = $this->db->get('sales');
                
        return $query;
    }

Controller //assume that the class exists and the model was properly loaded
Code:
function sales_breakdown()
    {  
        $this->load->library('pagination');

        $config['base_url'] = 'http://mysite.ie/enterprise/index.php/enterprise/sales_breakdown';
        $config['total_rows'] = '200';
        $config['per_page'] = '20';
        $this->pagination->initialize($config);

        $query = $this->report_model->sales_breakdown($this->pagination->per_page, $this->uri->segment(x)); //x is the segment position to get the offset for pagination to get the next set of items

        if ($query->result())
        {
            $data['sales'] = $query->result();
             $data['pagination']     = $this->pagination->create_links();
        
            $this->load->view('sales_view',$data);
        }

    }

View //can be saved in the view directory as sales_view.php

Code:
<table border='0' cellspacing='0' cellpadding='0' id='breakdown_table'>
    <tr>
    <td>Item</td>
    <td>Price</td>
    <td>Type</td>
    <td>Category</td>
    <td>Time</td>
    </tr>

    &lt;?php foreach ($sales as $row) { ?&gt;

    <tr >
        <td>&lt;?=$row->item?&gt;</td>
        <td>&euro;&lt;?=$row->price?&gt;</td>
        <td>&lt;?=$row->type?&gt;</td>
        <td>&lt;?=$row->temp3?&gt;</td>
        <td>&lt;?=$row->time?&gt;</td>
        </tr>
    &lt;?php } >

    </table>
    
    <br/>
    <br/>
    &lt;?=$pagination?&gt;
#7

[eluser]Solarpitch[/eluser]
That makes perfect sense now! Smile So in theory, the model should really only contain the bare bones of the query itself and the controller will process it, then you use the view to loop through the results.

That way is alot easier too.

Thanks for that post!
#8

[eluser]xwero[/eluser]
one tip for the pagination setting base_url use $this->config->site_url() or site_url, if you loaded the url helper, instead of adding the full url.

therealmaloy be careful using class variables, $this->pagination->per_page. It's safer to use $config['per_page'] because you have added that yourself. Lets say there is a change in the pagination library and the settings are all put in one array your code will break if you update the library.
#9

[eluser]Solarpitch[/eluser]
Just one question. What this saying?

Code:
if ($query->result())
{
....

If $query has been set or something?
#10

[eluser]xwero[/eluser]
This is better
Code:
//model
function sales_breakdown($limit = FALSE, $offset = FALSE)
    {
        $this->db->limit($limit);
        $this->db->offset($offset);

        $query = $this->db->get('sales');
                
        return $query->result();
    }
// controller
function sales_breakdown()
    {  
        $this->load->library('pagination');

        $config['base_url'] = $this->config->site_url('enterprise/sales_breakdown');
        $config['total_rows'] = '200';
        $config['per_page'] = '20';
        $this->pagination->initialize($config);

        //x is the segment position to get the offset for pagination to get the next set of items
$data['sales'] = $this->report_model->sales_breakdown($config['per_page'], $this->uri->segment(x));
        $data['pagination']     = $this->pagination->create_links();
        
        $this->load->view('sales_view',$data);

    }




Theme © iAndrew 2016 - Forum software by © MyBB