0

我要做的是创建一个可重用的画廊功能,如果需要,可以更改缩略图的布局。

我的问题是我的带有花括号变量的 $thumbLayout 字符串在包含在这样的函数中时不起作用,它仅在替换变量之后写入字符串时才起作用。

非常感谢任何帮助提出解决方案或更好的方法。

$thumbLayout = "<div class=\"customgallery\"><img src=\"{$thumbImgPath}\" width=\{$width}\" height=\"{$height}\" alt=\"{$imgName}\" /></div>";

function createImageGallery($galleryName,$db,$thumbLayout=''){
        $imgno = 1;
        //Page numbers//
        if(isset($_GET['page']) && is_numeric($_GET['page'])) {
            $page = $_GET['page'];
        }elseif(isset($_POST['page']) && is_numeric($_POST['page'])) {
            $page = $_POST['page'];
        }else{
            $page = 1;
        }

        $rowGallery = $db->query_first("SELECT gallery_id,num_cols,images_per_page FROM tblgallery WHERE gallery_name='$galleryName' AND show_gallery='y' LIMIT 1");    
        if($rowGallery!=false){
            $recordsPerPage = $rowGallery['images_per_page'];
            $numCol = $rowGallery['num_cols'];
            $gallery = '<ul>';
            $gallery_id = $rowGallery['gallery_id'];

            $offset = ($page - 1) * $recordsPerPage;
            $query = "SELECT * FROM tblimages WHERE group_id='$gallery_id' AND imgpath_thumb<>'' AND show_img='y' ORDER BY img_order";
                $rs = $db->query("$query LIMIT $offset,$recordsPerPage");
                foreach($rs as $row){
                    $largeImgPath = '/'.$row['imgpath_large'];
                    $imgName = $row['img_name'];
                    $thumbImgPath = '/'.$row['imgpath_thumb'];
                    if(($imgno>1) && (!is_float(($imgno-1)/$numCol))){
                        $li_class = 'class="newline"';
                    }elseif(($imgno>1) && (!is_float(($imgno)/$numCol))){
                        $li_class = 'class="last"'; 
                    }else{
                        $li_class = ''; 
                    }
                    list($width, $height) = getimagesize($row['imgpath_thumb']);
                    if($thumbLayout==''){
                        $gallery .= '<li '.$li_class.'><a href="'.$largeImgPath.'" class="popup" title="'.$imgName.'"><div class="gallery_imgbox"><img src="'.$thumbImgPath.'" width="'.$width.'" height="'.$height.'" alt="'.$imgName.'" /></div><span>'.$imgno.$imgName.'</span></a></li>';
                    }else{
                        $gallery .= '<li '.$li_class.'>'.$thumbLayout.'</li>';
                    }
                    $imgno++;
                }
            $gallery .= '</ul>';
        }else{
            //Gallery specified does not exist//
            return 'Gallery does not exist';
        }
    return  $gallery;
}

我认为这确实与 $thumbLayout 的位置有关。当在替换变量下方的函数内部时,它可以工作,但是当从函数外部包含时,它们不会被替换。好的,这是一个非常简单的例子:

$test1 = sprintf("My name is %s.",$name);
$test2 = "My name is {$name}.";
$test3 = "My name is ${name}.";
$test4 = "My name is $name .";
$test5 = "My name is <?= name ?>.";
$test6 = "My name is {{name}} .";

function showtest($test){
$name = "Bob";
echo $test; 
}

showtest($test1); //Result: My name is .//
showtest($test2); //Result: My name is .//
showtest($test3); //Result: My name is .//
showtest($test4); //Result: My name is .//
showtest($test5); //Result: My name is .//
showtest($test6); //Result: My name is {{name}}.//

而这会起作用(但对我没有帮助)

function showtest2(){
$name = "Bob";
$test = "My name is {$name}.";  
echo $test; 
}

showtest2(); //Result: My name is Bob.//
4

3 回答 3

0

我猜你想要的东西就像你在模板文件中得到的一样,我们在其中找到通常看起来像的模板变量{{my_var}}<?= $my_var ?>

不幸的是,这里的花括号变量将在您将其分配=给 $thumbLayout 后立即被替换,而不是在您将其传递给函数之后。它不像模板那样工作。

于 2013-10-06T08:05:01.297 回答
0

尝试这个:

$thumbLayout = "<div class=\"customgallery\"><img src=\"${thumbImgPath}\" width=\"${width}\" height=\"${height}\" alt=\"${imgName}\" /></div>";

如果左大括号 { 在 $ 符号之后,则大括号将被 PHP 替换,即 ${varname}。

添加:

应该将变量 $thumbLayout 放在子例程中:

您的功能必须是这样的:

<?php
function createImageGallery($galleryName,$db){
    $imgno = 1;
    //Page numbers//
    if(isset($_GET['page']) && is_numeric($_GET['page'])) {
        $page = $_GET['page'];
    }elseif(isset($_POST['page']) && is_numeric($_POST['page'])) {
        $page = $_POST['page'];
    }else{
        $page = 1;
    }

    $rowGallery = $db->query("SELECT gallery_id,num_cols,images_per_page FROM tblgallery WHERE gallery_name='$galleryName' AND show_gallery='y' LIMIT 1");    
    if($rowGallery!=false){
        $recordsPerPage = $rowGallery['images_per_page'];
        $numCol = $rowGallery['num_cols'];
        $gallery = '<ul>';
        $gallery_id = $rowGallery['gallery_id'];

        $offset = ($page - 1) * $recordsPerPage;
        $query = "SELECT * FROM tblimages WHERE group_id='$gallery_id' AND imgpath_thumb<>'' AND show_img='y' ORDER BY img_order";
            $rs = $db->query("$query LIMIT $offset,$recordsPerPage");
            foreach($rs as $row){
                $largeImgPath = '/'.$row['imgpath_large'];
                $imgName = $row['img_name'];
                $thumbImgPath = '/'.$row['imgpath_thumb'];
                if(($imgno>1) && (!is_float(($imgno-1)/$numCol))){
                    $li_class = 'class="newline"';
                }elseif(($imgno>1) && (!is_float(($imgno)/$numCol))){
                    $li_class = 'class="last"'; 
                }else{
                    $li_class = ''; 
                }
                list($width, $height) = getimagesize($row['imgpath_thumb']);

$thumbLayout = "<div class=\"customgallery\"><img src=\"${thumbImgPath}\" width=\"${width}\" height=\"${height}\" alt=\"${imgName}\" /></div>";

                if($thumbLayout==''){
                    $gallery .= '<li '.$li_class.'><a href="'.$largeImgPath.'" class="popup" title="'.$imgName.'"><div class="gallery_imgbox"><img src="'.$thumbImgPath.'" width="'.$width.'" height="'.$height.'" alt="'.$imgName.'" /></div><span>'.$imgno.$imgName.'</span></a></li>';
                }else{
                    $gallery .= '<li '.$li_class.'>'.$thumbLayout.'</li>';
                }
                $imgno++;
            }
        $gallery .= '</ul>';
    }else{
        //Gallery specified does not exist//
        return 'Gallery does not exist';
    }
return  $gallery;
}

//
// then in the main body:
//
// ....
//

$host = "localhost";
$database = "picassoreserve";
$user = "johndoe2";
$pass = "mypassword";

$db = new PDO("mysql:host=$host;dbname=$database", $user, $pass);
//
// or 
// $db = @new mysqli($host, $user, $pass, $database);
//

$galleryName = "picasso";

echo createImageGallery($galleryName, $db);

?>
于 2013-10-06T07:23:40.523 回答
0

根本不需要花括号。

$thumbLayout = "<div class=\"customgallery\"><img src=\"$thumbImgPath\" width=\"$width\" height=\"$height\" alt=\"$imgName\" /></div>";

会运作良好。您可能更喜欢使用:

$thumbLayout = sprintf("<div class=\"customgallery\"><img src=\"%s\" width=\"%s\" height=\"%s\" alt=\"s\" /></div>",$thumbImgPath,$width,$height,$imgName);

如果您将变量更改为数组,这也将起作用,例如 mysql 查询的结果。这是我使用的方法,因为我感觉可读性的小损失和通过错误排序变量而引入错误的更高风险是通过轻松插入函数、数组和变量的结果的能力来弥补的。通过仔细的格式化和练习,错误不太常见。

于 2013-10-06T07:33:24.010 回答