PDA

View Full Version : help with an array in a template


Mickie D
04-26-2015, 06:03 PM
hi there,

I am having trouble with the outputting of an array

Basically I am trying to show the contents of a directory on a custom page. I have created the template and also registered the variable but it will only show me the first row and not the rest.



$dir = "/uploaddirectory/";
$exclude = array( ".","..","error_log","_notes" );
if (is_dir($dir)) {
$files = scandir($dir);
foreach($files as $file){
if(!in_array($file,$exclude)){
$filerows = '<li>' . $file . '</li>';

}
}
}

// ###### Register Variables #######


vB_Template::preRegister('mytemplate',array('filer ows' => $filerows));



There are 4 files in that folder and all that it will display for me is the first one?

Thank you so much for any help you can give me

MarkFL
04-26-2015, 06:10 PM
Try this:

$dir = "/uploaddirectory/";
$exclude = array( ".","..","error_log","_notes" );
$filerows = '';
if (is_dir($dir)) {
$files = scandir($dir);
foreach($files as $file){
if(!in_array($file,$exclude)){
$filerows .= '<li>' . $file . '</li>';

}
}
}

// ###### Register Variables #######


vB_Template::preRegister('mytemplate',array('filer ows' => $filerows));

Mickie D
04-26-2015, 06:23 PM
That worked great.

Thank you.

I mistyped something the first time.

Cheers
Sat

kh99
04-26-2015, 07:02 PM
If you were seeing one before, adding that '.' before the = shouldn't have made it so you don't see anything. Are you sure you didn't make any other changes? If you just delete the added '.' does the one show up again?

Mickie D
04-26-2015, 07:59 PM
Hi KH99 it was my mistake i miss types the template at the bottom! (i edited that post 2 minutes before you posted)

vB_Template::preRegister('mytemplate',array('filer ows' => $filerows));

ALSO :)

just for my sanity it is the DOT EQUALS that explodes it into recursive rows?

And why did you empty the filesrow'' on the third row.

Thanks very much for your help

MarkFL
04-26-2015, 09:51 PM
Hi KH99 it was my mistake i miss types the template at the bottom! (i edited that post 2 minutes before you posted)

vB_Template::preRegister('mytemplate',array('filer ows' => $filerows));

ALSO :)

just for my sanity it is the DOT EQUALS that explodes it into recursive rows?

And why did you empty the filesrow'' on the third row.

Thanks very much for your help

This:

$variable1 .= $variable2;

is shorthand for:

$variable1 = $variable1 . $variable2;

So this way we are concatenating strings rather than resetting it each time with just "=".