Welcome Guest, Not a member yet? Register   Sign In
Simple way to deal with an array of arrays... a bucket !
#1

[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>&lt;?=$person['name']?&gt;</p>
<p>&lt;?=$person['age']?&gt;</p>
<p>&lt;?=$person['gender']?&gt;</p>
&lt;?php endforeach; ?&gt;

Here is the bucket.php library file...
Code:
&lt;?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;
    }
    
}

?&gt;
#2

[eluser]Unknown[/eluser]
Awesome. Thanks for this.
Works like a charm.
#3

[eluser]xwero[/eluser]
Code:
$data['people'] = array(
                     array('name'=>'Jeff','age'=>23,'gender'=>'Male'),
                     array('name'=>'Andrew','age'=>47,'gender'=>'Male'),
                     array('name'=>'Stacy','age'=>34,'gender'=>'Female'),
                  );
// VERSUS
$this->load->library('bucket', array('name', 'age', 'gender'));

$this->bucket->fill('Jeff', '23', 'Male');
$this->bucket->fill('Andrew', '47', 'Male');
$this->bucket->fill('Stacy', '34', 'Female');

$data = $this->bucket->spill('people');
I'm not sure which form is easier but i don't like that the spill method creates a container array for you. What if the controller has more data to pass to the view, then it has to come after the bucket is spilled.

Another thing is that it doesn't allow you to create multiple buckets.
#4

[eluser]Colin Williams[/eluser]
Must say this is the oddest thing I've seen in awhile Smile There's really nothing hard about working with multidimensional arrays. I assume is was a good exercise for you to get a grip with them though.

And the last really odd thing I saw here was a "CRUD library" that did nothing more than wrap database class calls in calls with different names. This bucket thing isn't nearly that bad. Don't worry.




Theme © iAndrew 2016 - Forum software by © MyBB