如何从Phalcon中的db检索类似的标签数据? [英] How to retrieve similar tags data from db in phalcon?

查看:81
本文介绍了如何从Phalcon中的db检索类似的标签数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Phalcon的新手.我决定将Phalcon php视为Codeigniter的替代php框架.我正在实施带有标签的博客.我首先将标签值插入数据库的单列中.我遵循以下标签插件示例: https://github.com/mfiels/tagsly/blob/master/index.html 它将多个值插入到单个列中,例如"php,jquery,asp,html,css".

I'm New in Phalcon. I decided to look at Phalcon php as an alternate php framework to Codeigniter. I'm Implementing a blog with tags. where first of im inserting tags value into a single column into the db. I follow tags plugin example from: https://github.com/mfiels/tagsly/blob/master/index.html its inserted multiple value into a single column like "php,jquery,asp,html,css".

现在,我像这样从db到volt检索值:

Now i just retrive the value from db to volt like this way:

[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检索与每个标签相关的数据?

Now its link like :"localhost/demo/blog/tag/php" or "localhost/demo/blog/tag/jquery" My Question is how could i retrieve each tags related data from 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 %}

,但未按预期呈现.我如何检索匹配的数据?

but its not render as expected. How could i retrieve matching data?

推荐答案

您可以遍历所有当前标记,并将它们逐一添加到查询中. 在循环期间,您还将创建绑定元素数组.

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

这篇关于如何从Phalcon中的db检索类似的标签数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆