我想做的是创建一个可重用的图库函数,如有必要,可以更改缩略图的布局。
我的问题是我的$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.//
试试这个:
$thumbLayout = "<div class='"customgallery'"><img src='"${thumbImgPath}'" width='"${width}'" height='"${height}'" alt='"${imgName}'" /></div>";
如果左大括号 { 在 $ 符号之后,即 ${varname},则大括号将被 PHP 替换。
添加:
应该将变量$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);
?>
根本不需要大括号。
$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 查询的结果,这也将起作用。 这是我使用的方法,因为我觉得可读性的微小损失和通过错误排序变量引入错误的更高风险是通过轻松插入函数、数组和变量的结果来弥补的。 通过仔细的格式设置和实践,错误不太常见。
我猜你想要一些类似于你在模板文件中得到的东西,在那里我们找到通常看起来像{{my_var}}
或<?= $my_var ?>
的模板变量
不幸的是,在这里,一旦您将=
花括号变量分配给$thumbLayout,就会被替换,而不是在将其传递到函数之后。它不像模板那样工作。