Category ans Datetime Field

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
noeluylee
Forum Newbie
Posts: 7
Joined: Mon Sep 27, 2010 2:43 am

Category ans Datetime Field

Post by noeluylee »

Hello,

I am trying create a news article, but I have few category in it. Can you help me to create an "add article form" which has dropdown list of category that I can choose, so when I select "Announcement" it will save the data on my mysql database under announcement table. sample html form below.

<form id="form1" name="form1" method="post" action="">
<label>
Category :
<select name="category" id="category">
<option>--- choose category ---</option>
<option>News</option>
<option>Announcement</option>
<option>Sports</option>
</select>
</label>
<br />
Date: <br />
Title:
<label>
<input type="text" name="title" id="title" />
</label>
<br />
Body:
<label>
<textarea name="body" id="body" cols="45" rows="5"></textarea>
</label>
<br />
<label>
<input type="submit" name="Submit" id="Submit" value="Submit" />
</label>
<br />
<br />
</form>

Can you tell me how to create the php code which support the above html.

Also, I would like to have a date and time on my article, but I want it automatically get the time and date from the computer of the client, so the user will not put it manually. can you point me to the correct code?


Thanks and hope you can help me! :D
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Category ans Datetime Field

Post by requinix »

You should not have a separate table for each article. One table for everything and, if there's only one category per article, a field that relates the article to a separate category table.

For the date, look into JavaScript's Date object.
noeluylee
Forum Newbie
Posts: 7
Joined: Mon Sep 27, 2010 2:43 am

Re: Category ans Datetime Field

Post by noeluylee »

Hello,

This is my news table look like
id int(10) unsigned NOT NULL auto_increment,
postdate timestamp(14),
title varchar(50) NOT NULL,
newstext text NOT NULL,
PRIMARY KEY (id),
KEY postdate (postdate)

so, if I want to add Announcement, I have to include that the news table? News and Announcement will be a separate category. Can you show me a bit of php script with case statement for this matter? let say on the form, when I choose Announcement on category, how php will handle it?

Thanks a lot! :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Category ans Datetime Field

Post by requinix »

Here are a few bits and pieces.

Code: Select all

article (
    id          INT UNSIGNED NOT NULL auto_increment,
    postdate    TIMESTAMP,
    title       VARCHAR(50)  NOT NULL,
    articletext TEXT         NOT NULL,
    category    INT UNSIGNED NOT NULL,
    PRIMARY KEY (id),
    INDEX (postdate)
)

id | postdate            | title | articletext | category
---+---------------------+-------+-------------+---------
1  | 2010-01-01 12:34:56 | One   | ...         | 1
2  | 2010-01-02 12:34:56 | Two   | ...         | 1
3  | 2010-01-03 12:34:56 | Three | ...         | 2

Code: Select all

category (
	id           INT UNSIGNED NOT NULL auto_increment,
	categoryname VARCHAR(50),
	PRIMARY KEY (id),
	UNIQUE (name)
)

id | categoryname
---+-------------
1  | Announcement
2  | News
3  | Sports

Code: Select all

SELECT article.title, article.postdate, category.categoryname
FROM article
JOIN category ON article.category = category.id

title | postdate            | categoryname
------+---------------------+-------------
One   | 2010-01-01 12:34:56 | Announcement
Two   | 2010-01-02 12:34:56 | Announcement
Three | 2010-01-03 12:34:56 | News

Code: Select all

<select name="category">
    <option value="1">Announcement</option>
    <option value="2">News</option>
    <option value="3">Sports</option>
</select>
noeluylee
Forum Newbie
Posts: 7
Joined: Mon Sep 27, 2010 2:43 am

Re: Category ans Datetime Field

Post by noeluylee »

So, what I need a "Article table" and "Category table"? for all my articles let say in News Section, Sports, Announcement etc, will be under Article table?

where will I put this?
SELECT article.title, article.postdate, category.categoryname
FROM article
JOIN category ON article.category = category.id
Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Category ans Datetime Field

Post by requinix »

That query merely demonstrates how to get the category name (because only the category number is in the article table).
noeluylee
Forum Newbie
Posts: 7
Joined: Mon Sep 27, 2010 2:43 am

Re: Category ans Datetime Field

Post by noeluylee »

So that query will be put on my php code right? :) I would also like to put a comment on all or every article on my site. how do I do that? what I currently have is the table itself
CREATE TABLE news_comments (
id int(10) unsigned NOT NULL auto_increment,
news_id int(10) unsigned DEFAULT '0' NOT NULL,
name varchar(40) NOT NULL,
comment text NOT NULL,
PRIMARY KEY (id),
KEY news_id (news_id)
);
To get a better view, do you have or may know where I can download the entire "article with photo and category, and can handle comments" php script with mysql tables? I am trying to learn on how to create such sites.

Thanks a lot! :)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Category ans Datetime Field

Post by requinix »

noeluylee wrote:I would also like to put a comment on all or every article on my site. how do I do that? what I currently have is the table itself
That table looks fine. Just generalize the names, like "comments" instead of "news_comments" and "id" instead of "news_id".
noeluylee wrote:To get a better view, do you have or may know where I can download the entire "article with photo and category, and can handle comments" php script with mysql tables?
If you want to skip the coding part then forget this stuff with databases. Get a popular blog platform like Drupal, get a couple addons/extensions/whatevers to add the functionality you need, and you're done. Meanwhile you can look at its code and see how it works.
Post Reply