Welcome Guest, Not a member yet? Register   Sign In
how to pass a multidimensional array to a view
#1

[eluser]atno[/eluser]
Hi,
Im trying to return a multidimensional array to a view but i dont know how to do it, i would appreciate any help

Controller
Code:
function index()
{
    function getFileList($dir, $recurse=false) {
        # array to hold return value
        $retval = array();

        # add trailing slash if missing
        if(substr($dir, -1) != "/") $dir .= "/";

        # open pointer to directory and read list of files
        $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
        while(false !== ($entry = $d->read())) {
                # skip hidden files
                if($entry[0] == ".") continue;
                if(is_dir("$dir$entry")) {
                        $retval[] = array(
                        "path" => "$dir",
                        "name" => "$entry",
                        "type" => filetype("$dir$entry"),
                        "size" => 0,
                        "lastmod" => filemtime("$dir$entry")
                        );
                        if($recurse && is_readable("$dir$entry/")) {
                                $retval = array_merge($retval, getFileList("$dir$entry/", true));
                        }

                } elseif(is_readable("$dir$entry")) {
                        $retval[] = array(
                        "path" => "$dir",
                        "name" => "$entry",
                        "type" => mime_content_type("$dir$entry"),
                        "size" => filesize("$dir$entry"),
                        "lastmod" => filemtime("$dir$entry")
                        );
                }
        }
        $d->close();
        return $retval;
    }
    $data = getFileList('/home/atno/Movies',false);
    $this->load->view('dirlist_view',$data);
}

View
Code:
<table>
  <thead>
    <tr>
      <th>Directory</th>
      <th>Name</th>
      <th>Type</th>
      <th>Size</th>
      <th>Last Mod.</th>
    </tr>
  </thead>
  <tbody>
    &lt;?php foreach ($retval as $file):?&gt;
    <tr>
    <td>&lt;?= $file['path'].$file['name']?&gt;</td>
    <td>&lt;?= $file['name']?&gt;</td>
    <td>&lt;?= $file['type']?&gt;</td>
    <td>&lt;?= $file['size']?&gt;</td>
    <td>&lt;?= date("r", $file['lastmod']) ?&gt; </td>
    </tr>
    &lt;?php endforeach; ?&gt;
     </tbody>
</table>

Thanks,
atno
#2

[eluser]therealmaloy[/eluser]
atno

$data["retval"] = getFileList('/home/atno/Movies',false);

just try this code, replace the one you had... this ensures that you have a variable that represents your array of files that your view can manipulate.

let us know if this works Smile

note: i would suggest to make your getFileList() function outside your index and just make it private inside your class, i guess it's cleaner and you can reuse it anytime you want inside the class.
#3

[eluser]atno[/eluser]
[quote author="therealmaloy" date="1231101929"]atno

$data["retval"] = getFileList('/home/atno/Movies',false);

just try this code, replace the one you had... this ensures that you have a variable that represents your array of files that your view can manipulate.

let us know if this works Smile

note: i would suggest to make your getFileList() function outside your index and just make it private inside your class, i guess it's cleaner and you can reuse it anytime you want inside the class.[/quote]

Hi therealmaloy,

Thanks it's working as it should now.
how can i make getFileList() private inside the class and how would i call it to run?

Thanks,
Atno
#4

[eluser]therealmaloy[/eluser]
Atno

class SampleClassname {

//some constructs and/or whatever

function index(){
...
$data['retval'] = $this->getFileList(...); //note on using it inside the class use $this
}

private function getFileList($dir, $recurse=false) {
...
}

} //end of class

you just precede your function with private modifier keyword, you can also further read about other access modifiers such as protected, by default if nothing precedes your function it is considered public...

it's nice to know some stuff about OOP knowing that CI is a good implementation of such.

you can check and look at CI helpers and plugins to slowly create your own set of important functions, such as this.
#5

[eluser]atno[/eluser]
Thanks a lot Smile




Theme © iAndrew 2016 - Forum software by © MyBB