Welcome Guest, Not a member yet? Register   Sign In
Referencing SQL Statement Within an SQL Statement
#2

[eluser]smith[/eluser]
You are using $row in nested loop, after first iteration $row will be wrong.
in inside loop use something like $row2 or anything you like, just not $row, because you are using $row in outside loop...

Code:
$sql = "SELECT schoolName FROM schools GROUP BY schoolName";
$query = $this->db->query($sql);

<?
foreach($query->result() as $row)
{
    $sql2 = "SELECT d.forename, d.surname, s.schoolName FROM delegates d LEFT JOIN schools s ON d.schoolId=s.id WHERE d.schoolId=s.id AND s.schoolName = '".$row->schoolName ."'";
    $query2 = $this->db->query($sql2);

    print $row->schoolName;
    foreach($query2->result() as $delegate)
    {
        print $delegate->forename." ".$delegate->surname;
    }
}
?>

try something like this in your view ...


Messages In This Thread
Referencing SQL Statement Within an SQL Statement - by El Forum - 10-15-2007, 10:41 AM
Referencing SQL Statement Within an SQL Statement - by El Forum - 10-15-2007, 11:25 AM
Referencing SQL Statement Within an SQL Statement - by El Forum - 10-15-2007, 02:35 PM
Referencing SQL Statement Within an SQL Statement - by El Forum - 10-15-2007, 04:59 PM



Theme © iAndrew 2016 - Forum software by © MyBB