1

我想使用 php(adodb) 和 smarty 在 jquery 函数中打印图像地址列表。

我需要这个:

 $.lightbox(["/media/photos/tmb/4.jpg", "/media/photos/tmb/3.jpg", "/media/photos/tmb/6.jpg"]);

我试试这个(只是简单的测试):

 {/literal} {if $photos} $.lightbox(["{"\", \""|implode:$photos}"]); {/if}{literal}

结果是:

 $.lightbox(["Array", "Array", "Array", "Array", "Array", "Array", "Array", "Array", "Array", "Array", "Array"]); 

实际上,我需要用图像地址替换数组。

图片地址(动态循环):

{$relative}/media/photos/tmb/{$photos[i].PID}.jpg

没有 jquery 功能,我用这种方法列出图像:

{if $photos}
{section name=i loop=$photos}
{$relative}/media/photos/tmb/{$photos[i].PID}.jpg
{/section}
{/if}

php代码:

    $sql            = "SELECT PID, caption FROM photos WHERE AID = " .$aid. " AND status = '1' LIMIT " .$limit;
    $rs             = $conn->execute($sql);
    $photos         = $rs->getrows();
    $smarty->assign('photos', $photos);

如何打印我需要的清单?我可以吗?

4

1 回答 1

1

最简单的做法是将数组分配给您的模板,然后使用json_encode(). 我不经常使用 Smarty,但我相信你可以这样做:

{$yourImageArray | json_encode}

编辑:现在您终于告诉我们您的数组包含什么:

数组 ( [0] => 数组 ( [0] => 13 [PID] => 13 [1] => [标题] => ) [1] => 数组 ( [0] => 14 [PID] => 14 [1] => [标题] => ) [2] => 数组 ( [0] => 15 [PID] => 15 [1] => [标题] => ) [3] => 数组 ( [ 0] => 30 [PID] => 30 [1] => 萨拉姆 [标题] => 萨拉姆) [4] => 数组 ([0] => 17 [PID] => 17 [1] => [标题] => ) [5] => 数组 ( [0] => 31 [PID] => 31 [1] => [标题] => ) [6] => 数组 ( [0] => 32 [PID] => 32 [1] => [标题] => ) [7] => 数组 ( [0] => 33 [PID] => 33 [1] => [标题] => ) [8] => 数组( [0] => 34 [PID] => 34 [1] => [标题] =>))

这是你需要做的。在您的控制器中:

$photoFileNames = array();
foreach ($photos as $photo) {
    $photoFileNames[] = $photo['PID'];
}

$smarty->assign('photoFileNamesJSON', json_encode($photoFileNames));

在您的模板中,您可以通过$photoFileNamesJSON. 下次您发布问题时,请从一开始就包含所有信息。如果您的问题具体,提供更具体的答案会容易得多。

于 2012-08-12T22:09:00.710 回答