我有一个包含img
和desc
字段的PHP图库。
img
字段存储图像的网址。
desc
字段提供了图像的简短说明。
这是我从数据库中获取数据的功能。
public function getGallery()
{
$images = array();
foreach ($this->model->image->slice(0,36) as $img)
{
//if we've got images web url we'll use
//it to hotlink image
if ($img->web)
{
$images[] = $img->web;
$desc[] = $img->desc;
}
//otherwise we will use image that we downloaded locally.
elseif ($img->local)
{
$images[] = $img->local;
$desc[] = $img->desc;
}
}
$count = count($images);
//repeat images if we have less then 6
if ($count < 36 && ! empty($images))
{
$duplicates = array_fill(0, 36 - $count, head($images));
$images = array_merge($images, $duplicates);
}
return (isset($images, $desc) ? $images : null);
}
这是我的Blade View
<div id="links">
@foreach(array_slice($data->getGallery(), 0, 36) as $k => $img)
<a href="{{ asset(Helpers::original($img)) }}" class="col-sm-2 col-xs-6 image-col" data-gallery>
<img src="{{{ Helpers::thumb($img) }}}" data-num="{{ $k }}" data-original="{{ Helpers::original(asset($img)) }}" alt="{{ 'Still of ' . $data->getTitle() }}" class="img-responsive pull-left thumb lightbox">
</a>
<figcaption title="{{{ $desc }}}" ></figcaption>
@endforeach
我一直在接受 未定义的变量:desc
答案 0 :(得分:0)
如果您真的希望按照自己的方式保留事物,可以将功能更改为:
public function getGallery(&$desc)
{
....
}
您的观点
@foreach(array_slice($data->getGallery($desc), 0, 36) as $k => $img)
...
@endforeach
但你必须确保在getGallery()函数中总是得到它们的值。
可能您可以在结构中更改一些内容,以使Controller将此数据传递给您的视图:
<?php
class MyController extends Controller {
public function show()
{
$gallery = ...; // retrieve your gallery model or repository here
$data = $this->repository->model->getGallery(); /// run your getGallery method
return View::make('gallery')->with('gallery', $gallery)->with('data', $data);
}
}
你的画廊必须返回:
public function getGallery()
{
...
return array(
'images' => isset($images, $desc) ? $images : null,
'desc' => $desc
);
}
您的观点必须改为:
@foreach(array_slice($data['images'], 0, 36) as $k => $img)
<a href="{{ asset(Helpers::original($img)) }}" class="col-sm-2 col-xs-6 image-col" data-gallery>
<img src="{{{ Helpers::thumb($img) }}}" data-num="{{ $k }}" data-original="{{ Helpers::original(asset($img)) }}" alt="{{ 'Still of ' . $gallery->getTitle() }}" class="img-responsive pull-left thumb lightbox">
</a>
<figcaption title="{{{ $data['desc'][$k] }}}" ></figcaption>
@endforeach