473,379 Members | 1,530 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,379 software developers and data experts.

Sort categories in A-Z order

11
Hi,
I am having a problem in sorting category, here is what i am doing..

SELECT S.subcat_name, count(S.subcat_id) AS total FROM subcategory S LEFT OUTER JOIN files F ON F.subcat_id=S.subcat_id GROUP BY F.subcat_id ORDER BY S.subcat_name ASC

this SQL query will result like this

Arts (1) (this is empty but still showing 1)
Celebrity(5)

I want to display result like this http://tinyurl.com/yt5z78

Please help me to correct this query and also suggest me how to display results just like the below sample shows you..

A
ABC (1)
DEC (55)

B
XYZ (23)
JOO (0)

Thanks
Jan 16 '08 #1
7 2734
Markus
6,050 Expert 4TB
Please show the code you use to display the data.

Use CODE=PHP tags.
Jan 16 '08 #2
moazam
11
its very simple.. i just don't know to put category names under A-Z
[PHP]
$a = $mydb->query("SELECT S.subcat_name, count(S.subcat_id) AS total FROM subcategory S LEFT OUTER JOIN files F ON F.subcat_id=S.subcat_id GROUP BY F.subcat_id ORDER BY S.subcat_name ASC");

while($b = mysql_fetch_assoc($a)) {
echo $b['subcat_name'] . ' (' . $b['total'] . ')<br />';
}
[/PHP]
Jan 16 '08 #3
moazam
11
No one here to answer :(
Jan 16 '08 #4
moazam
11
here is the updated code.

[PHP]
include("connect.php");
$a = mysql_query("SELECT S.subcat_name, S.subcat_id, count(F.id) AS total FROM files F RIGHT JOIN subcategory S USING(subcat_id) WHERE S.cat_id=3 GROUP BY S.subcat_id");
if($a) {
if(mysql_num_rows($a)== 0 ) {
echo 'No Result';
}
while($b = mysql_fetch_assoc($a)) {
echo "$b[subcat_id] - ". $b['subcat_name'] . "($b[total])<br >";
}
}
else {
echo 'Error: '.mysql_error();
}
[/PHP]

Output
======
Angelina Jolie(0)
Salma Hayek(0)
Jessica Simpson(0)
Jessica Alba(0)
Berry Moore(1)

What i need is..to display this like

A S
Angelina Jolie Salma Hayek

B
Berry Moore

J
Jessica Alba
Jessica Simpson

any idea?
Jan 17 '08 #5
code green
1,726 Expert 1GB
You need to do a bit more with this line [PHP]while($b = mysql_fetch_assoc($a)) {
echo "$b[subcat_id] - ". $b['subcat_name'] . "($b[total])<br >";[/PHP] A simple HTML table will give you more control over the format
Jan 17 '08 #6
dlite922
1,584 Expert 1GB
here is the updated code.

[PHP]
include("connect.php");
$a = mysql_query("SELECT S.subcat_name, S.subcat_id, count(F.id) AS total FROM files F RIGHT JOIN subcategory S USING(subcat_id) WHERE S.cat_id=3 GROUP BY S.subcat_id");
if($a) {
if(mysql_num_rows($a)== 0 ) {
echo 'No Result';
}
while($b = mysql_fetch_assoc($a)) {
echo "$b[subcat_id] - ". $b['subcat_name'] . "($b[total])<br >";
}
}
else {
echo 'Error: '.mysql_error();
}
[/PHP]

Output
======
Angelina Jolie(0)
Salma Hayek(0)
Jessica Simpson(0)
Jessica Alba(0)
Berry Moore(1)

What i need is..to display this like

A S
Angelina Jolie Salma Hayek

B
Berry Moore

J
Jessica Alba
Jessica Simpson

any idea?

This is easy... here's what you do. First get an array of the letters A - Z.

You know your data is sorted in Ascending order, so you aschend your alphabet with it.

You compare your first index in the array with the first letter or $b['subcat_name'], if its a match, out put the Letter A , and SET A FLAG THAT A MATCH HAS BEEN FOUND.

this flag will be set to false so that you don't out put the A for each result under A.

When you reach a subcat_name that doesn't not match A, you increase your index until you get a match, say we skipped B and we matched the subcat_name on C. Then reset the flag back to True and output that Index, in our example C.

and you'll get something like this with the correct HTML:

A
Andersion
Azul

C
Cat
Corndog

G
Gearge

H
Have Fun!!
Jan 18 '08 #7
dlite922
1,584 Expert 1GB
let me know if you need help with the coding, but code what you can understand of my logic and i'll correct it for you.
Jan 18 '08 #8

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

Similar topics

6
by: Brian | last post by:
Greetings, I'm working on a project that involved that has need of a categorization system. Logically speaking, the system will have elements, these elements will belong to at least a single...
2
by: jeef | last post by:
Im setting up a database driving store running off of a sql database and asp. I want to have categories with subcategories. That way a customer can borrow down through my site to buy a product....
6
by: Becker | last post by:
Very simple, I have a datagrid on a windows form. I load some rows in it. I click a column header and it sorts ascending. I click it again it sorts descending. This is great, but what I want to...
1
by: J | last post by:
Hi, all After user sorted a column in datagrid, or I should say after user click a column title to sort it, does this raise any event? if yes, how can I catch this event? becuase I need to do...
1
by: Jim | last post by:
Hello, I am creating a windows form application using vb.net 2003 which displays data in a datagrid, and then, if the user wants, I produce a nicely formatted crystal report of the data. The...
2
by: shuckjunkmail | last post by:
It was suggested that I re-submit this question as a new post rather than adding onto an old and unanswered post. The basic problem has to do with the .NET datagrid and sorting. I am having...
1
by: tothlaci | last post by:
Hi, I have an asp site where I sell some products. Product categories are stored in different tables. There are different asp product pages, which collects data from these tables. I have also...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
2
by: Randy | last post by:
I have two listboxes on a form. The first box displays categories while the second box displays the items belonging to the category selected in the first box. Thus, the second box is essentially...
1
by: Brit | last post by:
I have an ASP file that retrieves names from an Access database for 4 different categories of membership, which the visitor to the page selects (corporate, institutional, regular, or student). The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.