How to retrieve similar tags data from db in phalcon?

怎甘沉沦 提交于 2019-12-04 21:27:19

You can loop through all your current tags and add them to your query one by one. During the loop you are also creating your bind elements array.

[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); 

My expectation is when a user visit the detail view of a post i want to show that post related/similar another posts on that page. And similarity mached by its tags, Now i figure out by this way:

[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); 

And so on...

[View]

{% for similar in datas %}
{{link_to('blog/showfull/'~similar.id,similar.btitle,'class':'cats')}}  
{% endfor %}

Now its working as expected But is there another easy small simple way to do this? Please! Thnx Timothy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!