Welcome Guest, Not a member yet? Register   Sign In
how to do this..[query inside query]
#2

[eluser]Phil Sturgeon[/eluser]
With active record
Code:
$this->load->database();

$this->db->where('parentid', 0);
$query = $this->db->get('category');
foreach($query->result_array() as $row)
{
  echo $row["name"]."<br />";

  $this->db->where('parentid', $row["id"]);
  $sub_query = $this->db->get('category');

  foreach($sub_query->result_array() as $sub_row)
  {
    echo "- ".$sub_row["name"]."<br />";
  }
}

Without active record

Code:
$this->load->database();

$query = $this->db->query("SELECT * FROM category WHERE parentid=0");
foreach($query->result_array() as $row)
{
  echo $row["name"]."<br />";

  $sub_query = $this->db->query("SELECT * FROM category WHERE parentid='".$row["id"]."'");

  foreach($sub_query->result_array() as $sub_row)
  {
    echo "- ".$sub_row["name"]."<br />";
  }
}


This is all in the manual my friend.


Messages In This Thread
how to do this..[query inside query] - by El Forum - 02-02-2009, 05:25 AM
how to do this..[query inside query] - by El Forum - 02-02-2009, 06:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB