Вопрос

I'm having trouble trying to search an article by his tag.

In my table in the mysql database, the field tags is like:

tags: "tag1,tag3,tag92,tagasd"

so if i search tag1 it should show the article with that tag. But it show me this. enter image description here

I don't know also why it gives me an error in the "data" field because i use it for everything and no error seen.

However i use this query in mysql:

'SELECT * FROM news WHERE tags LIKE "%'.$tag.'%" ORDER BY id DESC'

but i've also used

'SELECT * FROM news WHERE tags REGEXP "[[:<:]]'.$tag.'[[:>:]]" ORDER BY id DESC'

If the tag i'm searching for, is in more than one post, the result search is correct, but if is in only one post, it looks like i've posted over (the picture) This is the link where you can try (and view): If you click on the bolder links, you will see correct articles, if you click in the "normale-size" links, you will see something like the picture above.

http://www.qsec.it/Blog/Tags

EDIT: NEWS CLASS function SearchWithTag(..)

    public function SearchWithTag($tag)
{
    $db = new Database();
    $db->connect();
    $db->sql('SELECT * FROM news WHERE tags REGEXP "[[:<:]]'.$tag.'[[:>:]]" ORDER BY id DESC');
    $res = $db->getResult();
    return $res;
}

TAGS-SEARCH FILE

                                <?php
                        $news = new News();
                    $newsArray = $news->SearchWithTag($categoria);
                    foreach($newsArray as $arr)
                    {
                ?>
                    <article itemscope itemtype="http://schema.org/Article">
                        <div class="article-body">
                            <strong class="contactpage" itemprop="name"><?php echo utf8_encode($arr['titolo']); ?></strong>                         
                            <div class="article-data">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo date('d M Y - H:i', $arr['data']); ?>
                            <br>
                            </div>
                            <div class="article-tag">
                                    <?php
                                        $tag = explode(",",$arr['tags']); 
                                        for ($i=0; $i<count($tag);$i++)
                                        {
                                            $cat = str_replace(" ", "", $tag[$i]);
                                            echo "<a href=\"/Blog/Tags/".$cat."\">".$cat."</a>";
                                            echo " <span class=\"separator\">|</span>\n";
                                        }
                                    ?>
                            </div>
                            </em></strong>
                            <div class="read-more">
                            <br>
                                <b><i class="pull-left"><a href="/Blog/Articoli/<?php echo $arr['titolo_url']; ?>.html">Vai all'articolo..</a></i></b>
                                <i class="pull-right" itemprop="name"><?php echo $arr['autore']; ?></i>
                            </div>
                        </div>
                        &nbsp;
                    </article>

                <br>

                <?php
                    }
                                 ?>
Это было полезно?

Решение

You can change the structure of your db.

You have the table of posts

||ID_post || post_name||

||000001|| Foo||
||000002|| Bar ||

You have the table of tags

|| tag_name ||

|| IT ||
|| Security ||

You have the table of "links"

||id_post|| tag_name ||

||000001 || IT ||
||000001 || Security ||

in the table links you can see that the post with id 000001 (the post titled FOO) is tagged with IT and Security. So, if you want to retrieve all posts ID tagged with "IT", you select all the rows with tag_name == security.

Другие советы

Since you're exploding your tags by commas right off the back I'm going to assume you forgot to escape your commas in your database or did escape them and are forgetting to replace them after your query.

$tags = preg_replace('/\s*,\s*/', ',', $tagsrow);
$tags = explode(",", $tags);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top