michaelbeersnl
Gebruiker
- Lid geworden
- 30 nov 2011
- Berichten
- 57
Beste helpers,
Ik ben momenteel bezig met een Class voor het laden van pagina's etc.
Alleen nu wil ik alle sub paginas gaan zoeken met recursie omdat dit sneller en lichter is voor de server...
Dit lukt allemaal prima met de parents maar nog niet goed met de children.
In de onderstaande code staat mijn output wat momenteel een beetje vreemd is omdat hij een "sub-sub-child" niet meer in de parent en child zet.
De output moet worden:
Parent
- Child
-- Subchild
-- Subchild
- Child
Het is nu:
Parent
- Child
- Child
Subchild
Subchild
Alvast bedankt,
Michael
Ik ben momenteel bezig met een Class voor het laden van pagina's etc.
Alleen nu wil ik alle sub paginas gaan zoeken met recursie omdat dit sneller en lichter is voor de server...
Dit lukt allemaal prima met de parents maar nog niet goed met de children.
In de onderstaande code staat mijn output wat momenteel een beetje vreemd is omdat hij een "sub-sub-child" niet meer in de parent en child zet.
De output moet worden:
Parent
- Child
-- Subchild
-- Subchild
- Child
Het is nu:
Parent
- Child
- Child
Subchild
Subchild
Alvast bedankt,
Michael
PHP:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* This is the pages model where all the database actions will be executed
*
* @copyright michaelbeers.nl
* @author Michael Beers
* @version 1.0
*
* @TODO
*
* Nothing
*
*/
class Pages_model extends MY_Model
{
private $_pages = array();
private $_isset = false;
private $_temp_return = array();
/**
* This is the constructor
*/
public function __construct()
{
parent::__construct(); //construct the parent controller(s)
}
/**
* Get a list with all pages by the preset database values
*/
public function get_pages()
{
$this->db->select('*');
//Run each where if it is passed
if (isset($this->_where))
{
foreach ($this->_where as $where)
{
$this->db->where($where);
}
$this->_where = array(); //Reset the _where
}
//Set the limit and offset if it is passed
if (isset($this->_limit) && isset($this->_offset))
{
$this->db->limit($this->_limit, $this->_offset);
$this->_limit = NULL;
$this->_offset = NULL;
}
//Set the order if it is passed
if (isset($this->_order_by) && isset($this->_order))
{
$this->db->order_by($this->_order_by, $this->_order);
$this->_order = NULL;
$this->_order_by = NULL;
}
$this->response = $this->db->get('pages');
return $this;
}
/**
* Get a single page by the preset database values and page id
*
* @param int $page_id The id of the page
*/
public function get_page($page_id)
{
$this->limit(1);
$this->where('pages.id', $page_id);
$this->get_pages();
return $this;
}
/**
* Get all the page parents by the current page
*
* @param int $page_id The id of the page
*/
public function get_page_parents($page_id)
{
//check if all the pagedata is already loaded and cached
//prevents dataloops
if (!$this->_isset)
{
$this->order_by('parent', 'asc');
$this->order_by('order', 'asc');
$query = $this->get_pages()->result();
foreach ($query as $row)
{
$this->_pages['items'][$row->id] = $row;
$this->_pages['parents'][$row->parent][] = $row->id;
}
$this->_isset = TRUE;
}
$this->_temp_return = array();
$this->find_parents($page_id);
return $this->_temp_return;
}
/**
* Get all the page children by te parent page id
*
* @param int $page_id The id of the page
*/
public function get_page_children($page_id)
{
//check if all the pagedata is already loaded and cached
//prevents dataloops
if (!$this->_isset)
{
$this->order_by('parent', 'asc');
$this->order_by('order', 'asc');
$query = $this->get_pages()->result();
foreach ($query as $row)
{
$this->_pages['items'][$row->id] = $row;
$this->_pages['parents'][$row->parent][] = $row->id;
}
$this->_isset = TRUE;
}
$this->_temp_return = array();
$this->find_childeren($page_id);
return $this->_temp_return;
}
// ------------------------------------------------------------------------
/**
* Recursive function to find parent pages
*/
private function find_parents($item_id)
{
$item = $this->_pages['items'][$item_id];
$parent_item = $this->_pages['items'][$item->parent];
if (isset($item, $parent_item))
{
$this->_temp_return[] = $parent_item;
if ($parent_item->parent != 0)
{
$this->find_parents($parent_item->id);
}
} else
{
return FALSE;
}
return TRUE;
}
/**
* Recursive function to find child pages
*/
private function find_childeren($item_id)
{
if (isset($this->_pages['items'][$item_id]))
{
$parent_item = $this->_pages['items'][$item_id];
if (isset($this->_pages['parents'][$parent_item->id]))
{
foreach($this->_pages['parents'][$parent_item->id] as $child_id)
{
$item = $this->_pages['items'][$child_id];
if (isset($item))
{
$this->_temp_return[$parent_item->id][] = $item;
$this->find_childeren($item->id);
}
}
}
}
}
}
?>
Code:
array(2) {
[1]=>
array(2) {
[0]=>
object(stdClass)#21 (11) {
["id"]=>
string(1) "2"
["created_on"]=>
string(1) "0"
["modified_on"]=>
string(1) "0"
["publisched_on"]=>
string(1) "0"
["active"]=>
string(1) "1"
["status"]=>
string(10) "publisched"
["parent"]=>
string(1) "1"
["order"]=>
string(1) "0"
["slug"]=>
string(12) "test-child-1"
["title"]=>
string(12) "Test Child 1"
["body"]=>
string(23) "Dit is een test child 1"
}
[1]=>
object(stdClass)#22 (11) {
["id"]=>
string(1) "4"
["created_on"]=>
string(1) "0"
["modified_on"]=>
string(1) "0"
["publisched_on"]=>
string(1) "0"
["active"]=>
string(1) "1"
["status"]=>
string(10) "publisched"
["parent"]=>
string(1) "1"
["order"]=>
string(1) "0"
["slug"]=>
string(12) "test-child-3"
["title"]=>
string(12) "Test Child 3"
["body"]=>
string(22) "Dit is een testchild 3"
}
}
[2]=>
array(1) {
[0]=>
object(stdClass)#23 (11) {
["id"]=>
string(1) "3"
["created_on"]=>
string(1) "0"
["modified_on"]=>
string(1) "0"
["publisched_on"]=>
string(1) "0"
["active"]=>
string(1) "1"
["status"]=>
string(10) "publisched"
["parent"]=>
string(1) "2"
["order"]=>
string(1) "1"
["slug"]=>
string(12) "test-child-2"
["title"]=>
string(12) "Test Child 2"
["body"]=>
string(23) "Dit is een test child 2"
}
}
}