Subject Layout

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wondernate
Forum Newbie
Posts: 1
Joined: Mon Aug 03, 2009 11:28 pm

Subject Layout

Post by wondernate »

I have a note organizer and I want to make it sortable by subjects. Instead of creating multiple columns in the database ie. subject 1, subject 2, subject_3, etc. I wanted to have one subjects column and have it take multiple entries in the same column. How would I then create a page that lists all the categories? I was just thinking commas after each subject in the table.

Any ideas?

Thanks,
N8
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: Subject Layout

Post by pcoder »

You can use an array and the sort function in PHP.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Subject Layout

Post by peterjwest »

So what you're suggesting is instead of database columns:
subject1 = 'animals'
subject2 = 'cats'
subject3 = 'cuteness'

Having:
subject = 'animals, cats, cuteness';

When PHP gets the subject from the database you can use:
$subjectArray = explode(', ',$subject);

To get an array where
$subjectArray [0] = subject1;
$subjectArray [1] = subject2;
$subjectArray [2] = subject3;

However if you're working on a large project (if you'll need to use the subjects in many ways) you may wish to consider making a separate table for each subject.

Subject table:
note = [note primary key]
subject = 'animals'
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

Re: Subject Layout

Post by peterjwest »

Yes, sorry. I mean that you should make one table called Subject which stores an entry for each subject.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Subject Layout

Post by Ollie Saunders »

+1 separate subject table idea.
Post Reply