Welcome Guest, Not a member yet? Register   Sign In
Extract multiple rows from DB and pass values to view
#1

[eluser]Dandy_andy[/eluser]
I have been searching for a simple explanation to my question but can't find one. I have a DB table from which I want to return only a specific field that could apply to various rows. For example:-

ID mem_id photo_path
-- ------ ----------
1 1001 somepath1
2 1002 somepath2
3 1003 somepath3
4 1003 somepath4
5 1003 somepath5
6 1003 somepath6
7 1004 somepath7
8 1005 somepath8

I am just interest in the 'photo_path's that apply for each 'mem_id'.

I have a model set up to extract rows or each instance of a particular MEM_ID:-

Code:
function return_photo_paths($id) {
  
  $this->db->select('photo_path');
  $this->db->from('photos');
  $this->db->where('mem_id', $id);
  $query = $this->db->get();
  
  if ($query->num_rows() > 0) {
   $row = $query->result_array();
   return $row;
   }
  
}

The problem I have is passing the array through to my controller in the most simplest way so that I can call on each of the paths by using $photo_path[1], $photo_path[2]... but I can't seem to achieve this as the result returned is a multidimensional array.

My controller looks like this at the moment:-

Code:
$photopath = $this->Image_file->return_photo_paths($id);
$per_id['photopath_inview'] = $photopath;
$this->load->view('profile', $per_id);

And in my view, I want to be able to call on the various paths by using a loop as the number will vary depending on which photos are assigned to which user...

Code:
<?php
  foreach ($photopath_inview as $value)
   {
   echo $value;
   }
?>

Any help appreciated. Thanks.
#2

[eluser]vitoco[/eluser]
First, always try to return something even when there's no matches to the query, in this case an empty array

Code:
function return_photo_paths($id) {
  
  $this->db->select('photo_path');
  $this->db->from('photos');
  $this->db->where('mem_id', $id);
  $query = $this->db->get();
  
    if ($query->num_rows() > 0)
    {
        // NO NEED TO STORE IN A VARIABLE BEFORE RETURN
        // UNLESS YOU HAVE TO PROCESS THE DATA
        return $query->result_array();
    }
    // DEFAULT RETURN ( SAME TYPE OF SUCCESS RESULT OR NULL/FALSE )
    return array();
}

In the controller you don't need to store it before either.

Code:
$per_id['photopath_inview'] = $this->Image_file->return_photo_paths($id) ;
$this->load->view('profile', $per_id);

IN THE VIEW

Code:
<?php
    // CHECK IF THE NUMBER OF ROWS RETURNED ARE == 0
    if( count( $photopath_inview ) == 0 )
    {
        echo "THERE'S NO RESULTS";
    }

    foreach( $photopath_inview as $value ) // VALUE IS A ROW
    {
        // EACH $value IS A RECORD FROM THE DB
        // SO IT'S AN ARRAY ( OR AN OBJECT DEPENDING OF THE METHOD USED TO GET IT )
        // AND THE VALUES ARE RETURNED BY INDEX
        echo $value['ID'].'<br/>';
        echo $value['mem_id'].'<br/>';
        echo $value['photo_path'].'<br/>';
        echo '<hr/>';
        // ALSO YOU CAN PRINT THE ARRAY WITH
        // echo '<pre>'.print_r( $value , true ).'</pre>' ;
    }
?&gt;

Saludos
#3

[eluser]Dandy_andy[/eluser]
Thanks, it seems obvious when someone else tells you!
#4

[eluser]cartalot[/eluser]
in the model you might want to try returning an object, it can be much simpler to deal with
Code:
return $query->row();

would also suggest doing as much logic as you can in the model and controller
so like if no results back from model -- do your logic in the controller as to what gets passed and shown in view
then keep view as simple as possible




Theme © iAndrew 2016 - Forum software by © MyBB