Welcome Guest, Not a member yet? Register   Sign In
passing array to views
#1

[eluser]emsurban[/eluser]
can any help me figure out the problem in this code. What I would like to do is pass a file list to views.

controller code:
Code:
$path = base_url().'/images/';
    $data['images'] = get_filenames($path,TRUE);
  
    $this->load->view('gallery_view',$data);

views code:

Code:
<?php foreach($images as $image):?>

<?php echo $image; ?>

<?php endforeach;?>


I got this error
Quote:Message: Invalid argument supplied for foreach()

Can any help me, i'm a newbie to CI. Thanks in advance.
#2

[eluser]Dam1an[/eluser]
get_filenames returns a boolean (FALSE) if the opendir command fails, and a boolean is not valid for a foreach loop
To see if this is the case, to a var_dump/print_r on what it returns
#3

[eluser]BnoL[/eluser]
The problem is $data['images'] is not an array so that you can not use foreach()
Try:
Code:
$data['images'][] = get_filenames($path,TRUE);
#4

[eluser]emsurban[/eluser]
thanks.. for the quick reply.
#5

[eluser]vitoco[/eluser]
[quote author="Dam1an" date="1246157794"]get_filenames returns a boolean (FALSE) if the opendir command fails, and a boolean is not valid for a foreach loop
To see if this is the case, to a var_dump/print_r on what it returns[/quote]

true , so you could do this in the view to see if the function returns a FALSE or an array
Code:
<?
    if( is_array( $images ) )
    {
        //<pre> tag it's only to make the array structure more readeable
        echo '<pre>'.print_r( $images , true ) .'</pre>';
    }
    else
    {
        echo 'NO IMAGES ( FALSE ) ';
    }
?&gt;

Saludos.
#6

[eluser]SardiorDragon[/eluser]
[quote author="vitoco" date="1246253645"][quote author="Dam1an" date="1246157794"]get_filenames returns a boolean (FALSE) if the opendir command fails, and a boolean is not valid for a foreach loop
To see if this is the case, to a var_dump/print_r on what it returns[/quote]

true , so you could do this in the view to see if the function returns a FALSE or an array
Code:
&lt;?
    if( is_array( $images ) )
    {
        //<pre> tag it's only to make the array structure more readeable
        echo '<pre>'.print_r( $images , true ) .'</pre>';
    }
    else
    {
        echo 'NO IMAGES ( FALSE ) ';
    }
?&gt;

Saludos.[/quote]

This can be done in the view but as a personal preference I would rather check the array in the controller and if it is FALSE then pass an empty array to the view. This way the foreach will still find an array but will not actually do anything because there is no data to handle.
#7

[eluser]vitoco[/eluser]
[quote author="SardiorDragon" date="1246297704"]This can be done in the view but as a personal preference I would rather check the array in the controller and if it is FALSE then pass an empty array to the view. This way the foreach will still find an array but will not actually do anything because there is no data to handle.[/quote]

true, if you pass an empty array, the code will look like this.

Code:
&lt;?
    // IN THE CONTROLLER
    $path   = base_url().'/images/';
    $images = get_filenames($path,TRUE) ;
    $data['images'] = ( is_array( $images ) ) ? $images : array() ;

    // IN THE VIEW
    if( count( $images ) > 0 )
    {
        //<pre> tag it's only to make the array structure more readeable
        echo '<pre>'.print_r( $images , true ) .'</pre>';
    }
    else
    {
        echo 'NO IMAGES ( EMPTY ARRAY ) ';
    }
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB