2014-03-28 22 views
0

好吧,所以这就是我想要实现的!试图创建只有一个foreach元素的灯箱gallery

我有一个PHP脚本,我从API/JSON设置中将信息导入到foreach()语句中。

这是我的一些代码:

$ch = curl_init(); 
$category=$_GET['category']; 
$url="http://www.someurl.com/api/listings?token=f716909f5d644fe3702be5c7895aa34e&group_id=10046&species=".$category; 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json', 
'X-some-API-Key: f716909f5d644fe3702be5c7895aa34e', 
)); 
$json = json_decode(curl_exec($ch), true); 

// Functions relating to the Echo Code 

foreach($json['listings'] as $listing) 
{ 
$short_personality=substr($listing['personality'],0,500); 
$peturl="http://www.someurl.com/?variable=".$listing['id']; 
$medium_photo=$listing['photos'][0]['large_340']; 
$gender_class=strtolower($listing['gender']); 
$breed_class=strtolower($listing['species']); 
$name=($listing['name']); 

然后,我创建的图像与下面的代码显示:

// Echo the output 

echo'<div class="animal"> 
     <div class="animal-image">'; 
     if ($listing['photos'] > 1) { 
    echo '<a class="size-thumbnail thickbox" rel="gallery" href="'.$medium_photo.'"> 
       <img src="'.$medium_photo.'" class="image-with-border" alt=""> 
       <div class="border" style="width: 340px; height: 340px;"> 
        <div class="open"></div> 
       </div> 
      </a>'; 
     } 
else { 
    echo '<a class="size-thumbnail thickbox" href="'.$medium_photo.'"> 
       <img src="'.$medium_photo.'" class="image-with-border" alt=""> 
       <div class="border" style="width: 340px; height: 340px;"> 
        <div class="open"></div> 
       </div> 
      </a>'; 
} 

因此,例如,在API中25拉所有项目都格式化为各自的部分,每部分都有自己的初始照片。当我点击那张照片时,灯箱会加载。

但是,它加载了API调用中的所有图像的灯箱,所以我得到了在灯箱中可用的144个图像。

有没有办法限制每个正在检索的项目在灯箱中的照片?

任何帮助,将不胜感激。

+0

通过说*它加载在API调用中的所有图像的灯箱,所以我得到144图像可用在灯箱*你的意思当你用鼠标旋转轮子或按下箭头键时,你正在查看下一个,就像所有的图像都在一个画廊中一样? – Sharky

+0

正确sharky! –

+0

插件的名称是什么? ThickBox的? – Sharky

回答

0

这个答案是基于我的假设您使用thickbox plugin(如果多数民众赞成的情况下,请更新您的问题的细节:d)如果你想每一个图像是在一个画廊“独”,而不是再

每一个环节都必须有不同的rel="name-of-individual-image-gallery-whatever"

所以你可以做这样的事情

// Functions relating to the Echo Code 

// add a counter 
$inc=0; 

foreach($json['listings'] as $listing) 
{ 
    $short_personality=substr($listing['personality'],0,500); 
    $peturl="http://www.someurl.com/?variable=".$listing['id']; 
    $medium_photo=$listing['photos'][0]['large_340']; 
    $gender_class=strtolower($listing['gender']); 
    $breed_class=strtolower($listing['species']); 
    $name=($listing['name']); 
    // add this 
    $unique_gallery_name="individual-gallery-".$inc; 
    $inc++; 
    // the rest of your code... 
    // . 
    // . 
    // . 

,然后添加rel属性的出把代码

<a class="size-thumbnail thickbox" rel="'.$unique_gallery_name.'"

等各个环节都会有一个相对这样rel="individual-gallery-0",下一环节rel="individual-gallery-1",下一个环节rel="individual-gallery-2" ...等等。结果将是每个图像在“独特”画廊,“独自”

+0

谢谢Sharky!那就是诀窍。现在我只需要遍历每个列表的照片。谢谢你的帮助! –

+0

@BlackDog乐于帮助:D – Sharky

相关问题