Welcome Guest, Not a member yet? Register   Sign In
help with query
#1

[eluser]Fenix[/eluser]
I am trying to pull records from a database and pass the results from a model into a controller. what is the best way to do this? i know i am doing this wrong right now so if somebody could point me in the right direction, that would be great.

my model (there is a field in the database for each of the items in the array):
Code:
function get_assets()
    {
        $this->db->select('*');
        $Q = $this->db->get('assets');
        if($Q->num_rows() > 0)
        {
            foreach ($Q->result_array() as $row)
            {
                $assets = array
                (
                'asset_id'     => $row['asset_id'],
                'file_name'      => $row['file_name'],
                'file_type'      => $row['file_type'],
                'file_path'      => $row['file_path'],
                'full_path'      => $row['full_path'],
                'raw_name'      => $row['raw_name'],
                'orig_name'      => $row['orig_name'],
                'file_ext'      => $row['file_ext'],
                'file_size'      => $row['file_size'],
                'is_image'      => $row['is_image'],
                'image_width'      => $row['image_width'],
                'image_height'      => $row['image_height'],
                'image_type'      => $row['image_type'],
                'image_size_str' => $row['image_size_str']
                );
            }
        }
        else
        {
            // no results
        }
        $Q->free_result();
        return $assets;
    }

my controller:
Code:
function view_assets()
    {
        $this->load->model('assets');
        
        $data['all_assets'] = $this->assets->get_assets();
        
        // Page Data
        $data['page_title'] = $this->config->item('title').': View Assets';
        $data['left_col'] = $this->load->view('admin/anav_view',$data,true);
        $data['center_col'] = $this->load->view('admin/vassets_view',$data,true);
        $this->load->view('template_view',$data);
    }
#2

[eluser]sl3dg3hamm3r[/eluser]
I don't see any need to fill first your $asset-array. You could directly return $Q and pass it to your view. Besides, you override $asset with each iteration.
#3

[eluser]Fenix[/eluser]
so if my model is like this:
Code:
function get_assets()
    {
        $this->db->select('*');
        $Q = $this->db->get('assets');
        $Q->free_result();
        return $Q;
    }

how would i pass it from my controller to my view and how would i iterate through it to put each record into a table? an example would help a lot, thanks.
#4

[eluser]iainco[/eluser]
Code:
class Assets_Model extends Model
{
  function get_assets()
  {
    return $this->db->get('assets')->result();
  }
}

Code:
class Assets_Controller extends Controller
{
  function view_assets()
  {
    $this->load->model('Assets_Model');
    $data['assets'] = $this->Assets_Model->get_assets();
    $this->load->view('assets', $data);
  }
}

assets.php view

Code:
foreach($assets as $asset)
  {
    &lt;?=$asset->asset_id?&gt; <br />
  }

Very crude example but that is how I would go about printing out each asset_id.

(Not sure if you need to use the free_result function or if codeigniter takes care of that sort of thing for you)
#5

[eluser]Fenix[/eluser]
thanks, i'll give this a try Smile
#6

[eluser]iainco[/eluser]
If you run into problems it might be because I usually use the "result_array" method which would mean changing "return $this->db->get('assets')->result();" to "return $this->db->get('assets')->result_array();" and in the view changing "&lt;?=$asset->asset_id?&gt;" to "&lt;?=$asset['asset_id']?&gt;".

PHP's print_r function is helpful when figuring out what is contained in result sets.




Theme © iAndrew 2016 - Forum software by © MyBB