[eluser]Unknown[/eluser]
I wrote this to make working with multi-dimensional arrays in code igniter a little easier.
I don't claim to be a PHP guru but thought others may find this helpful.
This will probably only be helpful if your view files are page fragments and not full page templates.
(1) Place bucket.php at /system/application/libraries/
(2) In your controller file load the bucket library
Note: This example uses three properties: name age gender. You can use as many as you need!
Code:
$this->load->library('bucket', array('name', 'age', 'gender'));
(3) Now fill your bucket
Code:
$this->bucket->fill('Jeff', '23', 'Male');
$this->bucket->fill('Andrew', '47', 'Male');
$this->bucket->fill('Stacy', '34', 'Female');
(4) Now spill your bucket (this example uses a view file called list.php)
Code:
$this->load->view('list', $this->bucket->spill('people'));
(5) Place list.php at /system/application/views/
Code:
<?php foreach($people as $person) : ?>
<p><?=$person['name']?></p>
<p><?=$person['age']?></p>
<p><?=$person['gender']?></p>
<?php endforeach; ?>
Here is the bucket.php library file...
Code:
<?php
// Author: Michael D. Gross
// Email: mike-at-michaeldgross-dot-com
class bucket {
var $contents = array();
var $columns = array();
function bucket($elements = array())
{
$this->columns = $elements;
}
function fill()
{
if(func_num_args() == sizeof($this->columns))
{
$arguments = func_get_args();
$position = sizeof($this->contents);
$this->contents[$position] = array();
foreach($this->columns as $column)
{
$this->contents[$position][$column] = array_shift($arguments);
}
}
}
function spill($name = 'bucket')
{
$data[$name] = $this->contents;
return $data;
}
}
?>