I have converted the article database to an article database.
So I have a list of articles on a web page. This works just fine. Now I would like to add the categories for each article. But to really take this further what I would like the final result page to be is a list of articles sorted by category and each category(s) listed with each article.
I believe I need some type of join for this. So I have listed my last attempt at the bottom. I know I am lost in the forest, so can anyone help me get back on the path.
This is from the book. So I have the following 3 tables;
TABLE article
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
articletext TEXT,
articledate DATE NOT NULL,
authorid INT
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
TABLE category (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255)
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
(index)TABLE articlecategory (
articleid INT NOT NULL,
categoryid INT NOT NULL,
PRIMARY KEY (articleid, categoryid)
) DEFAULT CHARACTER SET utf8 ENGINE=InnoDB;
So I am trying a query like this.
$sql = ‘SELECT categoryid,articleid, category.id, name FROM articlecategory
INNER JOIN category
ON
categoryid = category.id’;
$result = $pdo->query($sql);
foreach ($result as $row)
{
$cat = array(
‘id’ => $row[‘id’],
‘categoryid’ => $row[‘categoryid’],
‘catname’ =>$row[‘name’],
‘articleid’ =>$row[‘articleid’]);
}
On the display page I have a foreach for the article and inside of this a foreach for $cat.
I was closer at one time than I am now.
Thank you
Darrel