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: - $catquery = mysql_query("SELECT * FROM categories WHERE subcategory_id = '0' ORDER BY name ASC");
-
$catrows = mysql_num_rows($catquery);
-
-
-
$query = "SELECT COUNT(title) AS `total` FROM classifieds WHERE classified_id = 'category_id'";
-
$result = mysql_query($query);
-
-
-
while($row = mysql_fetch_assoc($result)){
-
$num_items_in_category = $row['total'];
-
}
-
echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>";
-
Thanks guys
9 1757
I can't tell if it's right or wrong without any sample data.
What data do you want me to provide?
I need to see a sampling of data in the tables.
Ok, here is database structure and its data. - CREATE TABLE IF NOT EXISTS `categories` (
-
`id` int(255) NOT NULL AUTO_INCREMENT,
-
`name` text COLLATE utf8_unicode_ci NOT NULL,
-
`subcategory_id` int(2) NOT NULL DEFAULT '0',
-
`parent_id` int(255) NOT NULL,
-
PRIMARY KEY (`id`)
-
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=27 ;
-
-
--
-
-- Dumping data for table `categories`
-
--
-
-
INSERT INTO `categories` (`id`, `name`, `subcategory_id`, `parent_id`) VALUES
-
(1, 'Announcements', 0, 0),
-
(2, 'Employment', 0, 0),
-
(3, 'Items For Sale', 0, 0),
-
(4, 'Services', 0, 0),
-
(5, 'Garage Sales', 0, 0),
-
(6, 'Automobiles', 0, 0),
-
(7, 'Announcement1', 1, 1),
-
(8, 'Announcement2', 1, 1),
-
-
--
-
-- Table structure for table `classifieds`
-
--
-
-
CREATE TABLE IF NOT EXISTS `classifieds` (
-
`classified_id` int(255) NOT NULL AUTO_INCREMENT,
-
`title` text COLLATE utf8_unicode_ci NOT NULL,
-
`description` text COLLATE utf8_unicode_ci NOT NULL,
-
`category_id` int(10) NOT NULL,
-
`name` text COLLATE utf8_unicode_ci NOT NULL,
-
`authorized` int(10) NOT NULL DEFAULT '0',
-
PRIMARY KEY (`adid`)
-
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=177 ;
-
-
--
-
-- Dumping data for table `classifieds`
-
--
-
-
INSERT INTO `classifieds` (`classified_id`, `title`, `description`, `category_id`, `name`, `authorized`) VALUES
-
(1, 'Test Classified', 'Here is the First Test classified listing.', 1, 1);
-
-
INSERT INTO `classifieds` (`classified_id`, `title`, `description`, `category_id`, `name`, `authorized`) VALUES
-
(2, 'GMC For Sell', 'Looks like new 1979 GMC.', 6, 1);
Thanks again
You should use a query that returns an aggregate query instead. Something like - SELECT groupingField, COUNT(*) AS countOfGroup
-
FROM someTable
-
GROUP BY groupingField
Thanks, I tried it this way: - $query = "SELECT category_id, COUNT(title) FROM classifieds GROUP BY category_id";
-
$num_items_in_category = $row['COUNT(title)'];
-
echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>";
-
According to data above, I'm getting (2) in each category.
So far I am getting there.
You don't seem to be looping your recordset at all so I'm not surprised you get the same number every time.
Oh sorry I just didn't put the whole code: -
$query = "SELECT category_id, COUNT(title) FROM classifieds GROUP BY category_id";
-
$result = mysql_query($query) or die(mysql_error());
-
while($row = mysql_fetch_array($result)){
-
$num_items_in_category = $row['COUNT(title)'];
-
}
-
echo "<><a href='category-".$row['id'].".php' >".$row['name'].$num_items_in_category."</a></li>";
You never print the results within the loop so you're only going to get the last value.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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
|
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
|
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...
|
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....
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |