Welcome Guest, Not a member yet? Register   Sign In
Passing Query Results into Views Effeciently
#1

[eluser]Fielder[/eluser]
Am I running this query and passing the results into my view correct?

Code:
$data['results'] = $this->Businesses->getBusiness($bus_number);
                //load database query results into array for the input form
                foreach ($data['results'] as $row)
                {
                    $data['bus_name'] = $row['bus_name'];
                    $data['bus_number'] = $row['bus_number'];
                    $data['bus_address'] = $row['bus_address'];
                    $data['bus_city'] = $row['bus_city'];
                    $data['bus_state'] = $row['bus_state'];
                    $data['bus_zip'] = $row['bus_zip'];
                    $data['bus_contact'] = $row['bus_contact'];
                    $data['bus_phone'] = $row['bus_phone'];
                    $data['bus_mobile'] = $row['bus_mobile'];
                    $data['bus_fax'] = $row['bus_fax'];
                    $data['bus_email'] = $row['bus_email'];
                    $data['cat_id'] = $row['cat_id'];
                    $data['bus_corporate'] = $row['bus_corporate'];
                    $data['user_rep'] = $row['user_rep'];
                }
                
                
                $this->load->view('include/header.php', $data);
                $this->load->view('business_new',$data);

then in the view I'm inserting the individual variables into the "value" part of their corresponding input boxes...
Code:
<li>
                    <label for="bus_name">Business Name</label>
                    <em>*</em>&lt;input type="text" name="bus_name" size="36" value="&lt;?= $bus_name; ?&gt;" class="required" /&gt;
                </li>
                <li>
                    <label for="bus_number">Business Number</label>
                    <em>*</em>&lt;input type="text" name="bus_number" size="24" value="&lt;?= $bus_number; ?&gt;" class="required" /&gt;
                </li>
etc..
etc..
etc..

Seems that my foreach statement is looping through that entire array for every result returned from the model(I don't know). But I would like to know if I could improve upon this code to efficiently and with less typing pass and display the results in the input boxes. Thx.
#2

[eluser]Dam1an[/eluser]
The easiest way would be to get the single row in the controller
Code:
// assuming getBusiness returns the query
$data['business'] = $this->Businesses->getBusiness($bus_number)->row();

And then call the object in the view, such as
Code:
&lt;input type="text" name="bus_name" size="36" value="&lt;?=$business-&gt;name; ?&gt;" class="required" />
&lt;input type="text" name="bus_name" size="36" value="&lt;?=$business-&gt;number; ?&gt;" class="required" />
...
#3

[eluser]xwero[/eluser]
If you only have to get one row you better use the row_array method instead of the result_array method and then you can do
Code:
$this->load->view('business_new',$this->Businesses->getBusiness($bus_number));
With your code get the last row of the results.

Because the variables are cached you can do
Code:
$this->load->view('include/header',$this->Businesses->getBusiness($bus_number));
$this->load->view('business_new');
This is of course if the header view needs some of the model data.
#4

[eluser]Fielder[/eluser]
xwero, I need to pass in the $data array into my view. It has other variables that my form needs.


This is what I have on my model side.. pretty straight-forward. I changed result_array() to row_array.
Code:
function getBusiness($bus_number)
    {
        $this->db->where('bus_number',$bus_number);
        $results = $this->db->get('bus');
        if ($results->num_rows() > 0)
        {
            //print_r($query);
            return; $results->row_array();
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB