FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Creating/Using Pre-Set Categories with PHP (and MySQLi)





hardingrj
Hi all....I am both new to Frihost and the wonderful world of PHP and MySqli.

I have been designing a blog/forum style site and have come stuck with listing posts by category.

What i am trying to do is add a category ID to each post a user makes (i.e. Category1, Category2, Category3 and so on.)

From this have a link on home page Category1, Category2 etc etc and when a user selects this link all posts with relevant CatID are then displayed.

Where i am stuck is do i need to create a categories table and link this to my users_posts table or add a CATID into users-posts table? How do I create the CATID for each category and then call each seperate category.

I really am stuck on this part and would appreciate any help available

Thanks in advance
jajarvin
Maybe this list posts in certain category on homepage will help you.
Marcuzzo
this is a pure (My)SQL task. if you've got your database properly normalized you should be able to do this.


you basically add a category table with an id as pk and a description.
in the posts table you simply add the category id as a fk and you are good to go

then you run a select all on the category table and join the posts table on the category ID and you join the users table on the userID

something like:
Code:
select * from categories as c
join posts as p on c.catid=p.categoryid
join users as u on c.userid=u.id


this will pretty much give you the data needed to display the posts.



edit: if you've got a table users and another table posts and then another table users_posts then your database is not properly normalized.

I would usually do something like this:

Code:
POSTS
--------
post_id ( PK ) Autoincrement not null
user_id ( PK-> FK ref to users table ) not null
date....
post content...
etc...
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.