I have PHP code to select categories from tbl_categorie, Now I have a multiple select in a form where an article can have more than one categorie. While inserting the values in database, I want to store multiple categories values in one column/attribute. Where ID_CAT attribute would only store categories ids of the tbl_categorie separated by comma(,). I have two tables in one DB,
tbl_blog:
ID_BLOG ID_CAT TITLE ARTICL DATE
1 1,3 title1 article1 2013-03-04
2 4,10 title2 article2 2013-03-04
3 3,6 title3 article3 2013-03-04
tbl_categorie:
ID_CAT NOM_CAT
1 HTML
2 CSS
3 DESIGN
4 PHP
5 ..
I have problem in first place to add tow ID_CAT for one article although I used the implode() statment but she works when i change the type of ID_CAT from int to varchar , and this is the process to add article :
Article::creatArticle(0,$_POST['title'],implode(', ', $_POST['id_categorie']),$_POST['article']);
and this is the function to add article from class_article :
/**
* function créeArticle
*/
public static function creatArticle($id_article,$title,$id_categorie,$article)
{
global $db;
$req = $db->prepare("INSERT INTO blog (ID_BLOG,TITLE,ID_CAT, ARTICLE,DATE) VALUES ('',:title,:id_categorie,:article,'".date('Y-m-d')."')");
$ok = $req->execute(Array('title' =>$title,'id_categorie' => $id_categorie,'article' => $article));
return $db->lastInsertId();
//$erreur = $req->errorInfo();
}
now i have probleme to fetch all categories for which in each article shall have , and this is how i fetch in my back-office the table of articles using the magic method get() which she returne only the first value in th column :
<?php
foreach(Article::getAllArticle()as $blog ){
$article= new Article($blog->ID_BLOG);
$categorie = new Categorie($article->getIDCategorie());
echo'
<tr>';
echo '<td><input type="checkbox" /></td>';
echo '<td>'.$article->getTitlearticle().'</td>';
echo '<td>'.$categorie->getNomCategorie().'</td>';
echo '<td>'.$article->getDatearticle().'</td>';
echo '<td>35</td>';
echo ' <td class="actions">';
echo '<a href="javascript:editArticle('.$article->getIDarticle().');" title="Edit this content"><img src="img/icons/actions/edit.png" alt="" /></a>';
echo ' <a href="javascript:deleteArticle('.$article->getIDarticle().');" title="Delete this content"><img src="img/icons/actions/delete.png" alt="" /></a></td>';
echo '</tr>';
} ..
i know that i have to use the explode() statment or making a loop but i can't figure it out how :( ,and i have a doubt about the type of ID_CAT should be varchar make the problem ? thanx !