[help] Manipulating mySQL results

I am trying to figure out something, but I don't know what to search for to looks for solutions. Firstly, I have a SQL database, surprised? However, I want to arrange the results in a format that I want to. I have three main categories and a half dozen to dozen sub categories that the results must be put into.

I don't want to run the SQL Query 20 times, with certainty that it will keep expanding. The order of these categories are in an arbitrary order, at least to the SQL server, so I can't simply use ORDER BY. Without getting too involved, the order is like:

Group Blah

Cat kkej

Cat spa

Cat psn

Cat scr

Group Ni

Cat wad

Cat ape

Group Heya

Cat poe

Cat cvwu

Each record does contain varchar fields on what Group and Cat they belong in. Is there any way that I can easily get this done? Or is there going to be a lot of PHP code involved?

I was thinking there could be a way to reQuery the results, like using a SQL Query, but instead of going into a table, to go through the returned array. But it doesn't like that. Can anyone point me in the right direction? Thank you.

#347988

Each record does contain varchar fields on what Group and Cat they belong in.
First, get a decent database design. Use foreign keys instead of varchar field for groups.

You should have a group - table holding all the groups ( pk ID, Name). Also, have a category table holding all categories: (pk ID, unique [Name, fk group] ) whereas each category is part of a group.

Have a table "record" that has (pk ID, fk category) whereas each record is related to a category.

#347993

Should use this:

$db = @MYSQL_CONNECT($db_server, $db_user, $db_password);

mysql_select_db("your_db", $db);

$result = mysql_query("SELECT id, name FROM your_table WHERE category_name1 = "if_you_want_to_filter_your_results" ORDER BY category_name1");

while($row = mysql_fetch_array($result)) {

echo $row['category_name1'] . "

";

echo $row['category_name2'] . "

";

echo $row['category_name4'];

}

arne

#348167