2013-03-19 101 views
0

我有一个小任务,其中有一个mysql表“blog”。它包含一列“ID_CAT”。 ID_CAT的每个字段包含单个文章的不同类别的不同值,如“22,44,33,55”。 我想根据类别选择过滤博客的帖子。我通过ID_CAT选择成URL和包含页面GET梅索德那样根据PHP的类别筛选帖子

<a class="rotated_link" href="?cat='.$categorie->getIDCategorie().'">'.$categorie->getNomCategorie().'</a> 

随后包括在网页那样

$id_categorie = $_GET["cat"]; 
if (isset($id_categorie)) { 

    foreach(Article::getAllArticlebycategorie($id_categorie) as $all){ 
       $article= new Article($all->ID_BLOG); 
       $img=Image::getImageByArticle($article->getIDarticle()); 
       $tblCat=explode(',',$article->getIDCategorie()); 
        echo '<li class="li_blog_post">'; 
          echo'<img class="img_post_mini" src="img/file/'.$img.'" style="width:100%; height:150px; border:1PX solid #9D9D9D;" />'; 
          echo'<span class="post_title0">'.$article->getTitlearticle().'</span>'; 
          echo'<span class="tag_post"><img src="img/tag.png" style="width:16px; height:16px;"/>'; 
        foreach($tblCat as $catt){ 
         $categoriea = new Categorie($catt); 

        echo '<a class="tag_lable" href="">'.$categoriea->getNomCategorie().'</a> </span>'; 
        } 
        echo'<p class="post_prev">'.substr($article->getArticle(), 0, 410).'&nbsp;...</p>'; 
        echo'<span class="date">le&nbsp;'.$article->getDatearticle().'</span> <span class="view_more"><a class="test" href="?article='.$article->getIDarticle().'">voire les détails</a></span>'; 
        echo '</li>'; 
       } 

} 

的probleme是当我例如ID_CAT = 4,则功能选择getAllArticlebycategorie只有在数字4是ID_CAT(4,33,50) - > selected(3,4,10) - >未选中列中的第一个值时才会返回帖子。 功能:

public static function getAllArticlebycategorie($id_categorie){ 
    global $db; 
    $req = $db->prepare('SELECT * FROM blog WHERE ID_CAT='.$id_categorie); 
    $req->execute(); 
    return $req->fetchAll(PDO::FETCH_OBJ); 
} 

回答

1

使用FIND_IN_SET()因为你正在使用PDO利用参数化查询。

因此改变

$req = $db->prepare('SELECT * FROM blog WHERE ID_CAT='.$id_categorie); 
$req->execute(); 

$req = $db->prepare("SELECT * FROM blog WHERE FIND_IN_SET('$id_categorie', ID_CAT)"); 
$req->execute(array(':id_categorie' => $id_categorie)); 

SQLFiddle您与FIND_IN_SET()

+0

它的工作就像一个魅力发挥,感谢SOOOO了! – 2013-03-19 11:48:57