我有两个简单的表来存储项目的标签:
标签:statement = "SELECT * FROM table"
if filterList:
statement += " WHERE column IN ({0})".format(
', '.join(['%s'] * len(filterList)))
cursor.execute(statement, filterList)
id | tag
是名称,因此每一行都是唯一的。
showcase_tags:tag
。
我根据项目的ID获取标签,如下所示:
id | tag_id | showcase_id
showcase类中的函数:
$tags = $showcase->getTags($id)
但 public function getTags ($id) {
$stmt = $this->db->getPdo()->prepare(
"SELECT tag_id FROM showcase_tags WHERE showcase_id = :id
");
$stmt->bindParam(":id", $id);
$stmt->execute();
$tagIds = $stmt->fetchAll();
return $this->getTagNames($tagIds);
}
private function getTagNames ($tagIds) {
$tagNames = [];
foreach ($tagIds as $id){
$stmt = $this->db->getPdo()->prepare(
"SELECT tag FROM tags WHERE id = :id
");
$stmt->bindParam(":id", $id);
$stmt->execute();
$name = $stmt->fetch()['tag'];
array_push($tagNames, $name);
}
return $tagNames;
}
数组$tagNames
只包含$tags = $showcase->getTags($id)
而不是标记的名称,但项目具有正确的标记数。