嗨,我有一个数据库,但似乎无法想出一种技术来搜索我的项目上的图像,我的数据库结构如下所示。
图像表
image_id (PK) |类别 |拇指|预览 |作者
图像标签表
img_id(FK)|标签名称
整个想法是,当用户填写文本框并按 Enter 键时,我的 PHP 脚本使用爆炸函数将请求的值按空格转换为数组,然后在搜索时循环遍历每个找到的值在数据库(图像标签表)上查找具有类似于请求的值的标签名称,并加入图像表,其中 (FK) 等于 (PK)。
我实际上正在寻找一种更有效、更可行的方法来完成我的工作
这是我的代码(php/query)。
$keywords = Input::get("keywords");
$keywordsExploded = explode(" ", $keywords);
for ($i=0; $i < count($keywordsExploded); $i++) {
$searchKey = $keywordsExploded[$i];
$search = DB::table('image_tags')
->leftJoin('images', 'images.image_id', '=', 'img_id')
->where('tag_name','like','%'.$searchKey.'%')
->get();
$imageResult[] = $search;
}
$imageResult = array_map('unserialize', array_unique(array_map('serialize', $imageResult)));
这是我转储变量时的输出。
Array ( [0] => Array ( [0] => stdClass Object ( [img_id] => 8045eb05de1f6b72824f2bfd2fc972c1 [tag_name] => Fine girls [image_id] => 8045eb05de1f6b72824f2bfd2fc972c1 [category] => Family [license] => Limited Utility (LU) [author] => Daniel Barde [thumb] => assets/downloads/img/thumb/2413759e3bdebf51b8bf215bc82018d4_thumb.jpg [prv] => assets/downloads/img/prv/00eead497c552ab309ab66294cf5c758_P.jpg [date_uploaded] => 01 - 23 - 2014 [time_uploaded] => 1390517578 [search_id] => 1390513134 [usage_policy] => ) [1] => stdClass Object ( [img_id] => c57d2e6f501670487fb5a5e2a016b5c1 [tag_name] => Fine girls [image_id] => c57d2e6f501670487fb5a5e2a016b5c1 [category] => Fashion [license] => Exclusive Utility (E [author] => Daniel Barde [thumb] => assets/downloads/img/thumb/c31598650ec51e6dd5725b2711920e91_thumb.jpg [prv] => assets/downloads/img/prv/fa1872139b4f53a840e94d7ceca1911e_P.jpg [date_uploaded] => 01 - 25 - 2014 [time_uploaded] => 1390657236 [search_id] => 1390652792 [usage_policy] => ) ) [2] => Array ( [0] => stdClass Object ( [img_id] => dfc7ec5aae7ce3b4397c57d4167b6417 [tag_name] => legs [image_id] => dfc7ec5aae7ce3b4397c57d4167b6417 [category] => Lifestyle [license] => Broad Utility (BU) [author] => Lifestyle [thumb] => assets/downloads/img/thumb/97f4517f6d5bdad880151d940da638d6_thumb.jpg [prv] => assets/downloads/img/prv/fb7402b93ce27fe6e93e5578571e02fe_P.jpg [date_uploaded] => 01 - 25 - 2014 [time_uploaded] => 1390656768 [search_id] => 1390652324 [usage_policy] => ) ) )
This is the code in my view.
@if(!empty($imgs))
@for ($i=0; $i < count($imgs) ; $i++)
<div class="span3 transition metal">
<figure class="top-img">
<img src="{{$backend_url}}{{$imgs[0][$i]->thumb}}" alt="{{$imgs[0][$i]->category}}" id='img{{$imgs[0][$i]->image_id}}'>
<figcaption class="img-meta">
<div class="row-fluid">
<div class="span8">
<a href="{{$base_url}}image/{{$imgs[0][$i]->search_id}}" class="img-id">#{{$imgs[0][$i]->search_id}}</a>
<a href="#" class="img-tags">{{$imgs[0][$i]->category}}</a>
</div>
<div class="span4">
<a href="#" class="btn download-btn"><i class="icon-download"></i></a>
</div>
</div>
</figcaption> <!-- end img-meta -->
<div class="roll-over" data-source='{{$imgs[0][$i]->image_id}}'>
<a href="#" class="btn"><span class="bucket"><i class='icon-shopping-cart'></i></span></a>
</div> <!-- end roll-over -->
</figure> <!-- end top-img -->
</div><!-- end span3 -->
@endfor
@endif
最佳答案
这是我要做的:
@if(!empty($imgs))
@foreach($imgs as $tag)
@foreach($tag as $image)
<div class="span3 transition metal">
<figure class="top-img">
<img src="{{$backend_url}}{{$image->thumb}}" alt="{{$image->category}}" id='img{{$image->image_id}}'>
<figcaption class="img-meta">
<div class="row-fluid">
<div class="span8">
<a href="{{$base_url}}image/{{$image->search_id}}" class="img-id">#{{$image->search_id}}</a>
<a href="#" class="img-tags">{{$image->category}}</a>
</div>
<div class="span4">
<a href="#" class="btn download-btn"><i class="icon-download"></i></a>
</div>
</div>
</figcaption> <!-- end img-meta -->
<div class="roll-over" data-source='{{$image->image_id}}'>
<a href="#" class="btn"><span class="bucket"><i class='icon-shopping-cart'></i></span></a>
</div> <!-- end roll-over -->
</figure> <!-- end top-img -->
</div><!-- end span3 -->
@endforeach
@endforeach
@endif
如果这有效,则意味着您跳过了两个循环之一。请注意我如何将您的 for
更改为 foreach
语句;我并不将自己定位为经验丰富的编码员,但我喜欢让变量更具可读性,而不是编写 $imgs[$i][$j]->something
关于php - 使用标签执行图像搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22475087/