473,394 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Count Number Of Classifieds In Each Category

5
Hi there, I have a table called categories that contains: id, name, subcategory_id, parent_id.
And another table classifieds that contains: classified_id, title, category_id.
I am trying to pull out number of item in each category. For example:
Accessories(15)
Auto(11)
Tutorials(25)
I gave it a try as follow, but all I got was zeros(0) even thought I added some items in categories:

Expand|Select|Wrap|Line Numbers
  1. $catquery  = mysql_query("SELECT * FROM categories WHERE subcategory_id = '0' ORDER BY name ASC");
  2.     $catrows = mysql_num_rows($catquery);
  3.  
  4.  
  5.       $query = "SELECT COUNT(title) AS `total` FROM classifieds WHERE classified_id = 'category_id'";
  6. $result = mysql_query($query);
  7.  
  8.  
  9. while($row = mysql_fetch_assoc($result)){
  10. $num_items_in_category = $row['total']; 
  11. }
  12. echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>";
  13.  
Thanks guys
May 10 '12 #1
9 1757
Rabbit
12,516 Expert Mod 8TB
I can't tell if it's right or wrong without any sample data.
May 10 '12 #2
Rocks
5
What data do you want me to provide?
May 10 '12 #3
Rabbit
12,516 Expert Mod 8TB
I need to see a sampling of data in the tables.
May 10 '12 #4
Rocks
5
Ok, here is database structure and its data.
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE IF NOT EXISTS `categories` (
  2.   `id` int(255) NOT NULL AUTO_INCREMENT,
  3.   `name` text COLLATE utf8_unicode_ci NOT NULL,
  4.   `subcategory_id` int(2) NOT NULL DEFAULT '0',
  5.   `parent_id` int(255) NOT NULL,
  6.   PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=27 ;
  8.  
  9. --
  10. -- Dumping data for table `categories`
  11. --
  12.  
  13. INSERT INTO `categories` (`id`, `name`, `subcategory_id`, `parent_id`) VALUES
  14. (1, 'Announcements', 0, 0),
  15. (2, 'Employment', 0, 0),
  16. (3, 'Items For Sale', 0, 0),
  17. (4, 'Services', 0, 0),
  18. (5, 'Garage Sales', 0, 0),
  19. (6, 'Automobiles', 0, 0),
  20. (7, 'Announcement1', 1, 1),
  21. (8, 'Announcement2', 1, 1),
  22.  
  23. --
  24. -- Table structure for table `classifieds`
  25. --
  26.  
  27. CREATE TABLE IF NOT EXISTS `classifieds` (
  28.   `classified_id` int(255) NOT NULL AUTO_INCREMENT,
  29.   `title` text COLLATE utf8_unicode_ci NOT NULL,
  30.   `description` text COLLATE utf8_unicode_ci NOT NULL,
  31.   `category_id` int(10) NOT NULL,
  32.   `name` text COLLATE utf8_unicode_ci NOT NULL,
  33.   `authorized` int(10) NOT NULL DEFAULT '0',
  34.   PRIMARY KEY (`adid`)
  35. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=177 ;
  36.  
  37. --
  38. -- Dumping data for table `classifieds`
  39. --
  40.  
  41. INSERT INTO `classifieds` (`classified_id`, `title`, `description`, `category_id`, `name`, `authorized`) VALUES
  42. (1, 'Test Classified', 'Here is the First Test classified listing.', 1, 1);
  43.  
  44. INSERT INTO `classifieds` (`classified_id`, `title`, `description`, `category_id`, `name`, `authorized`) VALUES
  45. (2, 'GMC For Sell', 'Looks like new 1979 GMC.', 6, 1);
Thanks again
May 10 '12 #5
Rabbit
12,516 Expert Mod 8TB
You should use a query that returns an aggregate query instead. Something like
Expand|Select|Wrap|Line Numbers
  1. SELECT groupingField, COUNT(*) AS countOfGroup
  2. FROM someTable
  3. GROUP BY groupingField
May 11 '12 #6
Rocks
5
Thanks, I tried it this way:
Expand|Select|Wrap|Line Numbers
  1.  $query = "SELECT category_id, COUNT(title) FROM classifieds GROUP BY category_id"; 
  2. $num_items_in_category = $row['COUNT(title)'];
  3. echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>";
  4.  
According to data above, I'm getting (2) in each category.
So far I am getting there.
May 11 '12 #7
Rabbit
12,516 Expert Mod 8TB
You don't seem to be looping your recordset at all so I'm not surprised you get the same number every time.
May 11 '12 #8
Rocks
5
Oh sorry I just didn't put the whole code:
Expand|Select|Wrap|Line Numbers
  1.  $query = "SELECT category_id, COUNT(title) FROM classifieds GROUP BY category_id"; 
  2. $result = mysql_query($query) or die(mysql_error());
  3. while($row = mysql_fetch_array($result)){
  4. $num_items_in_category = $row['COUNT(title)'];
  5. }
  6. echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>";
May 11 '12 #9
Rabbit
12,516 Expert Mod 8TB
You never print the results within the loop so you're only going to get the last value.
May 11 '12 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rich Protzel | last post by:
Hello, So my table contains say 100,000 records, and I need to group the categories in fld1 by the highest count of subcategories. Say fld1 contains categories A, B, C, D, E. All of these...
0
by: DataFreakFromUtah | last post by:
Hello! No question here, just a procedure for the archive. Search critera: count records imported count data imported count number of rows imported count number of records imported record import...
7
by: Barkster | last post by:
I'm trying to list my categories and how many items are in each category. I'm not much on sub queries. How can I combine these two items. I'd like to display the category and the corresponding...
7
by: FrankEBailey | last post by:
I'm not sure if this is a completely dumb question, but please humor me :) I have a table of records, called Records, each of which has a Category_ID that places it in a specific category; the...
11
by: Mack | last post by:
Hi all, I want to write a program to count number of bits set in a number. The condition is we should not loop through each bit to find whether its set or not. Thanks in advance, -Mukesh
10
by: coaassign | last post by:
Hi all I need to get a count the no of instructions executed in a for loop in exactly one second... Can any one please give me the code for this... THANKS IN ADVANCE
2
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following code to get the recordset of an external database. Dim dbPatients As Database Dim rsCountPatients As Recordset ' to count number of...
4
by: sahil | last post by:
Hello frends i am learning c language, I want to make a program which count occurence of each element in an array .I write following code for it but ity is not giving me desired result.pls help me....
1
by: jlt206 | last post by:
This code <?php include("counter.php")?> on the webpage produces the count number. (function code below) I want to place the current number into a variable $MemberNo or into a FormField to be sent...
0
by: luciacs | last post by:
Hi, I'm a completely novice in HTML and I'm trying to create a portfolio. I have a pull down menu and each section has more categories, and I want to click to each category, and see a picture on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.