简体   繁体   中英

select order by - heading of items in group

I have a table "items" with musical pieces, headers like "title" "composer" "description".. various pieces per composer.. trying to get a PHP script to SELECT * from items, order by "composer" but generate a header in the while for the composer, then output all rows with their work, and so on for the next.. ie:

ITEMS TABLE

-------------------------------
David Drury   | piece 1  | info
-------------------------------
David Drury   | piece 2  | info
--------------------------------
Alfonzo Smith | piece 1  | info
--------------------------------
David Drury   | piece 3  | info

Output

David Drury

  • piece 1 | info
  • piece 2 | info
  • piece 3 | info

Alfonzo Smith

  • piece 1 | info

Can I do this from one table? or might I be able to create a table for composers with a join that can see the composer, output the header, then outport the pieces from the join in the items table?

Got it.. Subquery. For anyone whom this might help, this is what I've used:

$sqlCommand = "SELECT DISTINCT Composer FROM items"; 
$query = mysqli_query($db_conx, $sqlCommand) or die (mysqli_error()); 

while ($row = mysqli_fetch_array($query)) { 
$Composer = $row["Composer"];
echo '<h2>'.$row["Composer"].'</h2>';
$sqlCommand2 = "SELECT * FROM items WHERE Composer = '$Composer'"; 
$query2 = mysqli_query($db_conx, $sqlCommand2) or die (mysqli_error()); 
while ($row2 = mysqli_fetch_array($query2)) { 
    $idPart = $row2["idPart"];
    $Composer = $row2["Composer"];
    $Title = $row2["Title"];
    $Opus = $row2["Opus"];
    $Instrumentation = $row2["Instrumentation"];
    $Type = $row2["Type"];
    $ISMN = $row2["ISMN"];
    $RRP = $row2["RRP"];
    $information = $row2["information"];



    echo '<h5>'.$Title.' - '.$information.'</h5>';
}
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM