Category, Subcategory? - !

Pages: 1, 2
free web hosting

Latest Entry: (Post #14) by alex1985 on Aug 10 2008, 03:28 AM. (Line Breaks Removed)
Thanks. I was waiting for this answer for several months!
Express your Opinion! Contribute Knowledge.

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Category, Subcategory? - !

alex1985
Listen, I would like to create some menu which list categories describes the news that category contains as well as sub-category below it. So, the questions is how I can count the number of news or items in the category or sub-category?

This is example:

Books (100)

-Finance (25)
-Biology (75)

Any ideas?

Reply

Galahad
I suppose you are using MySQL to store data, so you could simply use COUNT(*) in SQL query, to count categories and subcategories...

Something like
CODE
SELECT COUNT(*) FROM `news` WHERE `category`=1;


And that would return only one row and one field, containing number of news in the table, that correspond to the query you used...

Reply

alex1985
QUOTE
And that would return only one row and one field, containing number of news in the table, that correspond to the query you used...


Just, show me the sample code if you can.

Reply

alex1985
You, guys, to give up against my questions?!

Reply

galexcd
I think the best way of doing this is to have two tables. One called Category the other called subcategory.
Under the category table you would have ID and NAME.
Under the subcategory table you would have ID, SUPER, and NAME.

The super value under the subcategory would point to the ID of its supercategory.

Reply

alex1985
I'm a novice, just when you finish write your code for it as well.

Reply

galexcd
You haven't even said what you want to do with the database yet, how are we supposed to know what kind of code to give you when you haven't even fully explained what you want? Do you want to know just how to set up the database? Well you can do that much easier with phpmyadmin, but i suppose I could whip out something for you really fast.


CODE
CREATE TABLE  "category"(`ID` INT(3) NOT NULL AUTO_INCREMENT,`NAME` VARCHAR(35) NOT NULL, PRIMARY KEY (`ID`))
CREATE TABLE  "subcategory"(`ID` INT(3) NOT NULL AUTO_INCREMENT,`SUPER` INT(3) NOT NULL,`NAME` VARCHAR(35) NOT NULL, PRIMARY KEY (`ID`))

Reply

Framp
To write the code with these tables you simply have to retrieve data with a couple of mysql queries.

With a query select all the categories and then loop through their values.
In the loop you've to print the menu category and then to query all the subcategories with the SUPER id corrispondent to the category id. With this second loop you can simply print all the subcatergories

The code isn't so difficult smile.gif try to write it yourself.

Reply

alex1985
The code itself just creates database values, I need the one that counts, the code that links those two databases.

Reply

alex1985
Anymore replies, still waiting?!

Reply

Latest Entries

alex1985
Thanks. I was waiting for this answer for several months!

Reply

salamangkero
QUOTE(alex1985 @ Jul 31 2008, 06:03 AM) *
Just show me the instance of it!


Ok, assuming that your news articles are represented as filenames, suppose that you have the following table:

CODE
CREATE TABLE news (
`ID` INT(3) NOT NULL AUTO_INCREMENT,
`FILENAME` VARCHAR(128) NOT NULL,
`CATEGORY` VARCHAR(32) NOT NULL DEFAULT 'misc',
`SUBCATEGORY` VARCHAR(32) NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY INDEX_CATEGORY (CATEGORY),
KEY INDEX_SUBCATEGORY (SUBCATEGORY)
);


This will give you the following table:

CODE
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| ID | int(3) | | PRI | NULL | auto_increment |
| FILENAME | varchar(128) | | | | |
| CATEGORY | varchar(32) | | MUL | misc | |
| SUBCATEGORY | varchar(32) | | MUL | | |
+-------------+--------------+------+-----+---------+----------------+


Now, let's insert some dummy records:
CODE
INSERT INTO news (FILENAME, CATEGORY, SUBCATEGORY) VALUES
("hp01.php", "Books", "Harry Potter"),
("hp02.php", "Books", "Harry Potter"),
("hp03.php", "Books", "Harry Potter"),
("kl01.php", "Books", "tlhIngan Hol"),
("nz01.php", "Politics", "Adolf Hitler"),
("nz02.php", "Politics", "Adolf Hitler"),
("as01.php", "Politics", "Arnold Schwarzenegger"),
("as02.php", "Politics", "Arnold Schwarzenegger"),
("es01.php", "Genius", "Albert Einstein"),
("ph01.php", "Genius", "Paris Hilton");


As you can see, we have three categories: Books (4),Politics (4) and Genius (4)
Books has two subcategories: Harry Potter (3) and tlhIngan Hol (1)
Politics has two subcategories: Adolf Hitler (2) and Arnold Schwarzenegger (2)
Genius, too, has two subcategories: Albert Einstein (1) and Paris Hilton (1)

Now, to get how many news articles you have under the category Books, use the following statement:
CODE
mysql> SELECT count(1) FROM news WHERE CATEGORY = "Books";
+----------+
| count(1) |
+----------+
| 4 |
+----------+


On the other hand, to see how many articles you have under the subcategory Paris Hilton, use the following statement:
CODE
SELECT count(1) FROM news WHERE SUBCATEGORY = "Paris Hilton";
+----------+
| count(1) |
+----------+
| 1 |
+----------+


Well, there you go. If you need any more information, it certainly wouldn't hurt to ask in a polite manner, be patient and non-aggressive. It definitely pays to be nice to people you're asking help from. Most of all, a lot of us developers here took the time to learn PHP and MySQL, which is why we know these things. You certainly cannot learn simply by bullying other people to give you the information that you want, when you can just as easily search online resources.

Ah, but I'm going off-topic. In any case, have a nice day happy.gif

Reply

alex1985
Just show me the instance of it!

Reply

magiccode9
You may try this

Create a table like this

CODE
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| cat_id    | int(11)     | NO   | PRI |         | auto_increment |
| parent_id | int(11)     | YES  |     | 0       |                |
| name      | varchar(50) | NO   |     |         |                |
+-----------+-------------+------+-----+---------+----------------+



then, use this select query

CODE
SELECT parent_id, COUNT(parent_id) FROM testcat group by parent_id




Eric,

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Pages: 1, 2
Similar Topics

Keywords : category subcategory

  1. Evilboard (forum Software) - Multiple Categorys - Don't Work :( - I am creating a forum and i can't fix more then 1 category. (6)



Looking for category, subcategory,

Searching Video's for category, subcategory,
advertisement



Category, Subcategory? - !



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE