Reputation: 35
I read lots of posts about tags but none seem to do what I am doing. I have this post uploader where you have 6 tags from which you can choose as many as you want by clicking the check boxes beside each category and that POSTs the value to the php.
All that's fine, I just don't get how to store the tags in the database. If somebody uploads something that has to do with 4 categories I will have 4 tag variables which I will insert into somewhere. What's the best method to store the tags so I can sort the posts by tags into different pages later?
EDIT:
The HTML of the category checkboxes is something like this:
<ul class="selectionList">
<li>
<div class="markerDiv" id="maker_school">
<input class="marker_ckeckbox" name="markerType" value="Naruto" type="checkbox" />
<label class="marker_label">Naruto</label>
</div>
</li>
<li>
<div class="markerDiv" id="maker_school">
<input class="marker_ckeckbox" name="markerType" value="OnePiece" type="checkbox" />
<label class="marker_label">One Piece</label>
</div>
</li>
<li>
<div class="markerDiv" id="maker_school">
<input class="marker_ckeckbox" name="markerType" value="DragonBall" type="checkbox" />
<label class="marker_label">Dragon Ball</label>
</div>
</li>
<li>
<div class="markerDiv" id="maker_school">
<input class="marker_ckeckbox" name="markerType" value="Facts" type="checkbox" />
<label class="marker_label">Facts</label>
</div>
</li>
<li>
<div class="markerDiv" id="maker_school">
<input class="marker_ckeckbox" name="markerType" value="Quotes" type="checkbox" />
<label class="marker_label">Quotes</label>
</div>
</li>
<li>
<div class="markerDiv" id="maker_school">
<input class="marker_ckeckbox" name="markerType" value="Other" type="checkbox" />
<label class="marker_label">Other</label>
</div>
</li>
</ul>
I was thinking of grabbing $tag1,2...6 from POST['tag1']['name'] etc. then check if any of them are different to nothing else echo no category selected. Inside the if statement though I was thinking of inserting the t.id of the tag whose name is $tag1,2,3...6 and the postid of the post that is being uploaded into post_tags?
I know how to write it in php, but I just wanna know if the thought process is correct.
Upvotes: 0
Views: 38
Reputation: 204746
That is called a n to m relation. You can map it like this
posts table
-----------
id
title
...
tags table
----------
id
name
...
post_tags table
---------------
post_id
tag_id
To get all tags of a specific post you can do
select t.name
from tags t
join post_tags pt on pt.tag_id = t.id
join posts p on pt.post_id = p.id
where p.title = 'my little post'
Upvotes: 1