繁体   English   中英

聪明的功能分配PHP

[英]smarty function assign php

我在smarty中有一个函数,用于检查称为“ mikaphotos”的行是否为空。 如果该行为空,则可以,不进行任何操作;如果该行不为空,则使用$ path变量中的值。 现在我的助手是:

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

而我的getTheImages函数是:

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
 }
}

现在放在我的file.tpl中。.我放了:{$ mikaPhotos},它不起作用。 我认为我的代码中存在一些问题,但是我不知道该怎么做。 有什么帮助吗?

功能assign在智者受让人(!)的返回值getTheImages功能mikaPhotos变量。 因此,您的getTheImages函数应该返回一个字符串而不是回显。 例如,类似:

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
}    

好的,谢谢大家,我得到了正确的代码:

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;
                                    } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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