简体   繁体   中英

smarty function assign php

i have a function in smarty that checks if the row called "mikaphotos" is empty. If the row is empty, it's ok and doesnt happen anything, and if it's not empty..it takes the value in $path variable. Now my assin is:

$smarty->assign("mikaPhotos", getTheImages("SOMEPATH",400), true);

and my getTheImages function is:

getTheImages($path,$width){
 $dirname = $path; //the path
 $images = glob($dirname."*.png");//get the png images from directory
 foreach($images as $image) {
 echo '<img src="'.$dirname.'/'.$image.'" width="'.$width.'" /><br />';//echo all the images in SOMEPATH
 }
}

now in my file.tpl .. i put:{$mikaPhotos} and it's not working. I think there is some problem in my code, but i do not know how to do that. any help please?

Function assign in smarty assigns( ! ) value returned by getTheImages function to mikaPhotos variable. So, your getTheImages function should return a string instead of echoing. For example, something like:

getTheImages($path,$width){
    $result = '';
    $dirname = $path; //the path
    $images = glob($dirname."*.png");//get the png images from directory
    foreach($images as $image) {
        $result .= '<img src="'.$dirname.'/'.$image.'" width="'.$width.'" /><br />';
    }

    return $result;    // return html for all your images as a string
}    

ok thanks guys' i got the right code:

function getTheImages($path,$width){
                                        $result = '';
                                        /*$dirname = "../uploads/reko/".$path.""; //the path
                                        $images = glob($dirname."*.png");//get the png images from directory
                                        foreach($images as $image){
                                            $result .= '<img src="uploads/reko/'.$path.'/'.$image.'" width="'.$width.'" /><br />';
                                        }*/
                                        $handle = opendir("uploads/reko/".$path."");
        while($file = readdir($handle)){
            if($file !== '.' && $file !== '..'){
                $result .= '<img src="uploads/reko/'.$path.'/'.$file.'" width="'.$width.'" /><br />';
            }
        }
                                        return $result;
                                    } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM