问题
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".
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 } ?>
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?
I'm trying to query like this :
[Controller]
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');
}
[Volt]
{% for similar in tagged %}
{{tagged.btitle}}
{% endfor %}
but its not render as expected. How could i retrieve matching data?
回答1:
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);
回答2:
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
来源:https://stackoverflow.com/questions/36396598/how-to-retrieve-similar-tags-data-from-db-in-phalcon