[eluser]Jelmer[/eluser]
Some examples...
Let's say we do a query to the database with a function that gets everything with level 0 and then checks if each of its items has children (with their ID as 'parent'). Something like the following function:
Code:
function _get_structure($parent = 0) {
$query = $this->db->get_where('structure', array('parent'=>$parent));
$result = $query->result_array();
$i = 0;
foreach($result as $key => $item)
{
$result[$i]['subs'] = $this->_get_structure($item['id']);
$i++;
}
return $result;
}
(the _get_structure() method is untested, it's a quick rewrite of one of my own only as an example - all the
generate_list_helper output shown below was created by the helper using the array below)
Which would output something like:
Code:
array(
1=>array(
'id' => '3',
'parent' => '0',
'title' => 'Testitem',
'description' => 'Some text to describe the contents'
),
2=>array(
'id' => '4',
'parent' => '0',
'title' => 'Second item',
'description' => 'Another line of text to describe this item',
'subs' => array(
1=>array(
'id' => '9',
'parent' => '4',
'title' => 'Childitem',
'description' => 'Children need a description too'
),
2=>array(
'id' => '11',
'parent' => '4',
'title' => 'A second child',
'description' => 'Explain the second child of the second item'
),
3=>array(
'id' => '12',
'parent' => '4',
'title' => 'And a third to close off',
'description' => 'I\'m really out of ideas for placeholder text ;-)'
)
)
),
3=>array(
'id' => '6',
'parent' => '0',
'title' => 'A third item',
'description' => 'A last description for this item, which is the third.'
)
)
So we'll assign the aforementioned output to a variable and set some options.
Code:
$this->load->helper('generate_list');
$resource = $this->_get_structure();
$options = array(
'template_head'=>'<ul>',
'template_foot'=>'</ul>',
'alternate'=>array('color: blue;', 'color: red;')
);
$template = '<li style="{ALTERNATE}"><strong>{TITLE}</strong><br />{DESCRIPTION}{SUBS}</li>';
echo generate_list($resource, $options, $template);
Which will generate the following list (I added spaces and linebreaks for readability):
Code:
<ul>
<li style="color: red;"><strong>Testitem</strong><br />
Some text to describe the contents</li>
<li style="color: blue;"><strong>Second item</strong><br />
Another line of text to describe this item
<ul>
<li style="color: red;"><strong>Childitem</strong><br />
Children need a description too</li>
<li style="color: blue;"><strong>A second child</strong><br />
Explain the second child of the second item</li>
<li style="color: red;"><strong>And a third to close off</strong><br />
I'm really out of ideas for placeholder text ;-)</li>
</ul></li>
<li style="color: red;"><strong>A third item</strong><br />
A last description for this item, which is the third.</li>
</ul>
A values/text string to generate a Script.aculo.us inPlaceCollectionEditor
But from the same data we could also create values for a Script.aculo.us inPlaceCollectionEditor, by changing the $template & $options variables to:
Code:
$options = array(
'template_head'=>'[',
'template_foot'=>']',
'link'=>','
);
$template = '["{ID}", "{TITLE}"]{SUBS}';
Which in turn will output:
Code:
[["3", "Testitem"],["4", "Second item"],[["9", "Childitem"],["11", "A second child"],["12", "And a third to close off"]],["6", "A third item"]]
A select/pulldown box
Or create a pulldownbox with some indentation for structure clarity:
Code:
$options = array('level'=>0,'indent'=>' |');
$template = '<option value="edit/{ID}.htm">{INDENT}- {TITLE}</option>{SUBS}';
Which would output (again with added linebreaks):
Code:
<option value="edit/3.htm">- Testitem</option>
<option value="edit/4.htm">- Second item</option>
<option value="edit/9.htm"> |- Childitem</option>
<option value="edit/11.htm"> |- A second child</option>
<option value="edit/12.htm"> |- And a third to close off</option>
<option value="edit/6.htm">- A third item</option>
(In this last case you'd have to add the <select> tag outside this function, I thought about adding a setting that would only add a "template_head" & "tamplate_foot" to the top-list, but in my view that's only clutter - it works just as well to add it after the output was created)