我是Phalcon的新人。我决定将Phalcon php作为Codeigniter的替代php框架。我正在实施一个带有标签的博客。第一个im插入标签的值是单个列到db中。我关注的标签插件示例来自:https://github.com/mfiels/tagsly/blob/master/index.html 它将多个值插入到单个列中,如“php,jquery,asp,html,css”。
现在我只需将db的值从这样的方式转换为伏特:
[controller]
$bloger = $this->modelsManager->executeQuery("SELECT * FROM Blogs ORDER BY Blogs.datetime DESC");
$this->view->setVar('blogs', $bloger);
[volt]
<?php
$blogTags = array();
$blogTags = $bloger->tags;
$tags = explode(',',$blogTags);
foreach($tags as $taged){ ?>
<a class="tags" href="blog/tag/<?php echo($taged); ?>">
<?php echo($taged); ?> <span>[ 0 ]</span></a>
<?php } ?>
现在它的链接如:"localhost/demo/blog/tag/php"
或"localhost/demo/blog/tag/jquery"
我的问题是如何从db中检索每个标签相关数据?
我正在尝试这样查询:
[控制器]
public function tagAction($taged)
{
$tags = Blogs::findBytags($taged);
$tagData = array();
$tagData = explode(',', $tags->tags);
$similar = Blogs::find(["tags LIKE :title:","bind"=> ["title"=>'%'.$tagData.'%'],"order" => "datetime DESC limit 5"]);
$this->view->setVar('tagged', $similar);
$this->view->pick('blog/tagline');
}
[伏特]
{% for similar in tagged %}
{{tagged.btitle}}
{% endfor %}
但它没有按预期呈现。我怎样才能检索匹配的数据?
答案 0 :(得分:2)
您可以遍历所有当前标记,然后逐个添加到查询中。 在循环期间,您还要创建绑定元素数组。
[controller]
...
$currenttags = explode(',', $blog->tags);
$query = Blogs::query();
$bindParameters = [];
for($i = 0; $i < count($currenttags); $i++) {
$query->orWhere('tags LIKE :tag' . $i . ':');
$bindParameters['tag' . $i] = '%' . $currenttags[$i] . '%';
}
$query->bind($bindParameters);
$similar = $query->execute();
$this->view->setVar('datas', $similar);
答案 1 :(得分:0)
我的期望是当用户访问帖子的详细视图时,我想在该页面上显示该帖子相关/类似的其他帖子。由它的标签组成的相似性,现在我想通过这种方式:
[controller]
public function showfullAction($id)
{
$blog = Blogs::findFirstByid($id);
$this->view->setVar('detail', $blog);
$currenttags = explode(',',$blog->tags);
I want to make Loop Throw....
$dataCount = count($currenttags);
$tags1 = $currenttags[0];
$tags1 = $currenttags[0];
$tags2 = $currenttags[1];
$tags3 = $currenttags[2];
$tags4 = $currenttags[3];
$tags5 = $currenttags[4];
$tags6 = $currenttags[5];
$tags7 = $currenttags[6];
$tags8 = $currenttags[7];
$tags9 = $currenttags[8];
if($dataCount == '1')
{
$similar = $this->modelsManager->executeQuery("SELECT Blogs.* FROM Blogs WHERE Blogs.tags LIKE '%$tags1%'");
}
elseif($dataCount == '2')
{
$similar = $this->modelsManager->executeQuery("SELECT Blogs.* FROM Blogs WHERE Blogs.tags LIKE '%$tags1%' or Blogs.tags LIKE '%$tags2%'");
}
elseif($dataCount == '3')
{
$similar = $this->modelsManager->executeQuery("SELECT Blogs.* FROM Blogs WHERE Blogs.tags LIKE '%$tags1%' or Blogs.tags LIKE '%$tags2%' or Blogs.tags LIKE '%$tags3%'");
}
$this->view->setVar('datas', $similar);
等等......
[View]
{% for similar in datas %}
{{link_to('blog/showfull/'~similar.id,similar.btitle,'class':'cats')}}
{% endfor %}
现在按预期工作但是还有另一个简单的小方法吗?请! Thnx Timothy