Sorting values based on MySQL recordset

I don't know if this is possible, but I would like to use a single query to accomplish a couple of tasks.

What I have is a single query that has a list of FAQ questions that include an id, question, answer, and category (int) that is inner joined with another table to provide the category name.  Here is the query.

$query_rsFAQ = "SELECT FAQ.faqID, FAQ.faqName, FAQ.faqCat, FAQ.faqText, Categories.CatID, Categories.CatName, Categories.CatDescription FROM FAQ Inner Join Categories ON FAQ.faqCat = Categories.CatID ORDER BY Categories.CatName ASC";

What I want to do is create a menu like REGISTRATION / PLACING LISTING / MEMBERSHIP / ETC. based on the values from the query.

Now, after the menu, I want to just run the results of the query, separating the categories and provide a heading for each section.
For Example
Membership
     Question 1: Answer
     Question 2: Answer
Registration:
     Question 1: Answer
     Question 2:  Answer

Etc.

As I am kind of a newbie at arrays, I don't know how to pull out the unique values from an array column in order to make the headings/menu, without repeating the category over and over with a do/while loop.

I know how to do it by going back and hitting the database for the values, but thought it would be much faster to do it with a single query and manipulate the array instead.
dragboatrandyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dirknibleckCommented:
I think you'll want a second query for determining the headings:

ie:  $heading_query = "SELECT DISTINCT base.CatName FROM (" . $query_rsFAQ . ") as base"

0
dirknibleckCommented:
I would run it like:

$query_rsFAQ = "SELECT FAQ.faqID, FAQ.faqName, FAQ.faqCat, FAQ.faqText, Categories.CatID, Categories.CatName, Categories.CatDescription FROM FAQ Inner Join Categories ON FAQ.faqCat = Categories.CatID ORDER BY Categories.CatName ASC";

$heading_query = "SELECT DISTINCT base.CatName FROM (" . $query_rsFAQ . ") as base";
$heading_result = @mysql_query($result);

while ($heading_row = @mysql_fetch_array($heading_result, MYSQL_NUM)){
     echo $row[0];
     
     $query_rsFAQ = "SELECT FAQ.faqID, FAQ.faqName, FAQ.faqCat, FAQ.faqText, Categories.CatID, Categories.CatName, Categories.CatDescription FROM FAQ Inner Join Categories ON FAQ.faqCat = Categories.CatID WHERE Categories.CatName ='" . $row[0] . "' ORDER BY Categories.CatName ASC";

     $result_rsFAQ = @mysql_query($query_rsFAQ);
     while($rsFAQ_row = @mysql_fetch_array($result_rsFAQ, MYSQL_ASSOC)){
          echo //your columns
     }
}
0
carchitectCommented:
Select data by a single query and sort it in DB only, no need to do in array. Loop through the data and as soon as category changes, print accordingly.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

dragboatrandyAuthor Commented:
Thanks for the help, could you give me an example?
0
carchitectCommented:
$query_rsFAQ = "SELECT FAQ.faqID, FAQ.faqName, FAQ.faqCat, FAQ.faqText, Categories.CatID, Categories.CatName, Categories.CatDescription FROM FAQ Inner Join Categories ON FAQ.faqCat = Categories.CatID ORDER BY Categories.CatName ASC";
$res=mysql_query($query_rsFAQ);

$category_id = 0;

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

if($category_id != $row['Categories.CatID'])
{
$category_id = $row['Categories.CatID'];
echo "<br>";
echo Categories.CatName;
echo "<br>";
}
echo "{FAQ.faqID}. {FAQ.faqName} {FAQ.faqText}";
echo "<br>";

}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dragboatrandyAuthor Commented:
I did change a couple of things, but the basic structure worked like a champ!  Thanks - you're the best!

Here is what the final code looked like:

$category_id = 0;
while($row=mysql_fetch_array($res)) {
      if($category_id != $row['CatID'])
            {
            $category_id = $row['CatID'];
                  echo "<br>";
                  echo $row['CatName'];
                  echo "<br>";
            }
      echo $row['faqID']. $row['faqName']."-".$row['faqText'];
      echo "<br>";
}
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.