Welcome Guest, Not a member yet? Register   Sign In
Loop within loop: in controller or in the view?
#11

[eluser]codex[/eluser]
[quote author="llbbl" date="1194587404"]
Code:
$sql = $this->db->query("SELECT * FROM `people` ORDER by peopleid ASC ");        
        if($sql->num_rows()>0){            
            foreach($sql->result() as $row){                
        
            $query = $this->db->query("SELECT * FROM `address` WHERE peopleid='$row->peopleid' ORDER by addressid ASC ");            
                if($query->num_rows()>0){
                    foreach($query->result() as $obj){
                        echo "<div>$obj->address, $row->city</div><br />";
                    }
                }            
            }
        }
Get the idea?[/quote]

Yeah, but the part that I don't get: this goes into your controller, so you're echoing from within the controller? Isn't that, well, like bad practice?
#12

[eluser]llbbl[/eluser]
Yea sorta .. but who cares if your not using a view file, like if you are waiting for the designer to finish the HTML template. Something like this I do before I hassle with a view file to make sure I am getting the right data, to make sure that my php is working how I want it.

Here is same example except assuming you are going to use a view file.

Code:
$sql = $this->db->query("SELECT * FROM `people` ORDER by peopleid ASC ");        
        if($sql->num_rows()>0){            
            foreach($sql->result() as $row){                
        
            $query = $this->db->query("SELECT * FROM `address` WHERE peopleid='$row->peopleid' ORDER by addressid ASC ");            
                if($query->num_rows()>0){
                    foreach($query->result() as $obj){
                        $address[]= "<div>$row->firstname, $obj->address</div><br />";
                    }
                }            
            }
        }

$data['address'] = $address;

$this->load->view('whatever', $data);

Edited it again to make little more sense.
edited again .. forgot the equals ><
#13

[eluser]codex[/eluser]
[quote author="llbbl" date="1194591988"]Yea sorta .. but who cares if your not using a view file, like if you are waiting for the designer to finish the HTML template. Something like this I do before I hassle with a view file to make sure I am getting the right data, to make sure that my php is working how I want it.

Here is same example except assuming you are going to use a view file.

Code:
$sql = $this->db->query("SELECT * FROM `people` ORDER by peopleid ASC ");        
        if($sql->num_rows()>0){            
            foreach($sql->result() as $row){                
        
            $query = $this->db->query("SELECT * FROM `address` WHERE peopleid='$row->peopleid' ORDER by addressid ASC ");            
                if($query->num_rows()>0){
                    foreach($query->result() as $obj){
                        $address[]= "<div>$row->firstname, $obj->address</div><br />";
                    }
                }            
            }
        }

$data['address'] = $address;

$this->load->view('whatever', $data);

Edited it again to make little more sense.
edited again .. forgot the equals ><[/quote]

I'm with you so far. But the trick is not to output the result of one query, but of two, like so:

Code:
output loop 1: category name 1
  output loop 2: category item 1 (belongs to category name 1)
                 category item 2 (belongs to category name 1)
                 category item 3 (belongs to category name 1)
output loop 1: category name 2
  output loop 2: category item 1 (belongs to category name 2)
                 category item 2 (belongs to category name 2)
                 category item 3 (belongs to category name 2)
output loop 1: category name 3
  output loop 2: category item 1 (belongs to category name 3)
                 category item 2 (belongs to category name 3)
                 category item 3 (belongs to category name 3)
This seems impossible to do in a controller and output it to the view. I wonder how the rest of you is doing this. I bet there's plenty of instances where you (=other CI users) need to do the same.
#14

[eluser]llbbl[/eluser]
Not impossible at all. Use an if statements in 2nd loop to check to see if the category changed. If it has than add <h1>$row->catagory</h1> to your $address array.

Paypal me $20 I'll write the loop for you. Tongue

Just keep working at it, I'm sure you will get it. I find the times that I learn the best (retain and apply the information for later) is when I figure it out on my own, rather than have someone else just do it.
#15

[eluser]codex[/eluser]
Ok, I think I got it. But it's basically the same as armchair's solution, which is a little less 'complex'. So I'll stick to that for the moment. But thanks for your time!!
#16

[eluser]llbbl[/eluser]
His solution is meant for a loop in a view file. It is also only one loop. You asked for two, so yes it is more complex.
#17

[eluser]John_Betong[/eluser]
[quote author="codex" date="1194595412"]Ok, I think I got it. But it's basically the same as armchair's solution, which is a little less 'complex'. So I'll stick to that for the moment. But thanks for your time!![/quote]

Hi Codex,

I was wondering if you could supply the two relevant tables. I believe it will be easier to supply an exact solution.

Cheers,

John_Betong
&nbsp;
#18

[eluser]obiron2[/eluser]
you want to end up with a data set that looks like this:

$animals = array('dogs' => array('rover','gnasher'),'cats'=> array('kitty','fluffy','max'))

in your view you can then nest the loops


Code:
foreach ($animals as $animaltype => $animalnames)
{
  print "$animaltype<BR>";
  foreach ($animalnames as $animalname)
  {
    print "    - $animalname<BR>";
  }
  print "<BR>";
}

how you get the data into that format is up to you.

obiron




Theme © iAndrew 2016 - Forum software by © MyBB