Q:

php how to list with sub heading

New to php environment, what I am trying to do is create a list using php and MySQL database. see below
-----my output requirements info ------
main page heading
subheading1 (content of brandtype=1)
1111
2222
3333
4444 ( I want these as a hyperlink to open in the same webpage)
subheading2 (brandtype=2)
5555
6666
7777
8888 ( I want these as a hyperlink to open in the same webpage)
my code below
    

// Connect to database server
include 'xxxxxyyyyzzzz.php';
$conn = mysql_connect($db_host,$db_username,$db_password);
mysql_connect($db_host,$db_username,$db_password) or die (mysql_error ());

mysql_select_db($db_database,$conn) or die(mysql_error()); // Select database
echo "<h1>main webpage heading</h1>"; 
echo "<h3>my subheading</h3>"; 
// SQL query

$strSQL = "SELECT * FROM table name WHERE brand type='1','Software'  ORDER BY serviceName ASC";

// Execute the query (the recordset $rs contains the result)

$rs = mysql_query($strSQL);

// Loop the recordset $rs

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

$strName = $row['serviceName'];
$strLink = "<a href = 'person.php?id = " . $row['ID'] . "'>" . $strName . "</a>";

// List link

echo "<li>" . $strLink . "</li>";

//  include 'emaild.php';  
 }

// Close the database connection

mysql_close();
?>

</ul>
 </body>
 </html>
  end of my code

I am getting a listing ok, but have to put in subheading manually (e.g. echo "main title page names"; echo "subheading title";  depending on the brand type ?

how can tweak my code so that where my brand type = 1 it prints a heading brand type1 and then prints out the items for that brand 1 underneath it, if there are no items , then it does not print subheading (brand type=1) and goes to the next to print subheading for brandtype=2 and lists the content underneath it.
I can make list items underneath each subheading as links to a webpage but it does not work, how can I make these list items open on the same webpage with more details of product and I would use back button to go previous list webpage...thanks in advance..singhy

A:

Are the brand names in a different table where it can be read? If it's not you can do it manually still, but will much more ease (See below). If so, please let us know where that is located.
SendBrandListings(1, "Brand 1 Header");
SendBrandListings(2, "Brand 2 Header");

function SendBrandListings($brandId, $heading){
    echo "<h3>$heading</h3>"; 
    $strSQL = "SELECT * FROM table name WHERE brand type='$brandId','Software'  ORDER BY serviceName ASC";
    $rs = mysql_query($strSQL);
    while($row = mysql_fetch_array($rs)) {
        $strName = $row['serviceName'];
        $strLink = "<a href = 'person.php?id = " . $row['ID'] . "'>" . $strName . "</a>";
        echo "<li>" . $strLink . "</li>";
    }
}

UPDATE
If your brand name is in the same table, let's say the field is brandName - you would modify you current loop to this (simplified for demo purposes). 
First, you'll want to ensure that your results are sorted by Brand FIRST, then by service name. This is because we will be looping through the services in the order they are received in. This will arrange items of a like brand together. So make sure your SQL statement's sorting argument looks like this: 
ORDER BY brandName, serviceName ASC
Simply keep track of the last Brand Name you displayed, and if it changes, display the new one.
$c = 0; //Variable to keep count of categories
$lastBrand = '';    //Declare a variable to track the last displayed Brand Name    
while($row = mysql_fetch_array($rs)) {

    //If the brand name has changed, display it and update the tracking variable
    if ($lastBrand != $row['brandName']){
        $lastBrand = $row['brandName'];

        //If this isn't the first category, end the previous list.
        if ($c>0) echo "</ul>";

        echo '<h3>'.$row['brandName'].'</h3><ul>';
        $c++;
    }

    $strName = $row['serviceName'];
    $strLink = "<a href = 'person.php?id = " . $row['ID'] . "'>" . $strName . "</a>";

    echo "<li>" . $strLink . "</li>";
}

