简体   繁体   中英

MySQL GROUP by or using PHP?

I have an issue that seems to be quiet easy but I just would like to ask how you would solve it:

In a MySQL-table there is the following structure:

provider     artist      
a            1
a            2
a            3
b            4

Now it is necessary to echo a list in HTML like:

provider a
1
2
3

provider b
4

I stuck at the point where I would like to group the results and echo them out with for-each and while loop.

The main idea is quiet simple like:

<?php $query = mysqli->query("SELECT * FROM `table` GROUP by provider");

foreach GROUP {?>

  echo some styling HTML-code for each group of headline;

  <?php while ($data= $query->fetch_assoc()){?>

  echo some styling HTML-code for each list-item;

<?php};?>

Thanks in advance.

UPDATE:

Thanks for answering.

The solution fro RiggsFolly seems to work fine. There is just a small problem with the HTML. There is a surrounding div-tag that wont be closed when adding the HTML code in that line:

 echo 'provider '. $data->provider;

The problem is that the while loop needs to be in the div. The closing div-tag is missing for every

if ( $current_provider != $data->provider ) {

Here is the original HTML-code:

    <?php
        $service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");
        $current_provider = NULL;

        while ($data = $service->fetch_object()) {
            if ( $current_provider != $data->provider ) {// new provider?>
    <div class="service">
    <p class="lower">
     <?php echo $data->provider;?>
    </p>
    <?php
        $current_provider = $data->provider;
    }?>
    <a href='?artist=<?php echo $data->artist;?>'><?php echo "/".$data->artist;?</a><br/>   
    <?php };?>
</div><!--service -->

The list-items will be shown nicely. But when looking into the Source code you can see that the closing div-tag is missing. Thanks

Kind regards.

It would seem simpler to not use a GROUP BY especially as it will not provide you with the data that you want. So instead just select them all and sort them by provider and maybe artist as a sub sort like so

<?php 
    $result = $mysqli->query("SELECT * FROM `table` ORDER BY provider, artist");

    $current_provider = NULL;

    while ($data = $result->fetch_object()){
        if ( $current_provider != $data->provider ) {
            // new provider
            echo 'provider '. $data->provider;
            $current_provider = $data->provider;
        }
        echo $data->artist;
    }
?>

AFTER UPDATE:

<?php
    $service = $db->query("SELECT * FROM `system` ORDER BY provider, artist");

    $current_provider = NULL;

    while ($data = $service->fetch_object()) {

        if ( $current_provider != $data->provider ) {
            if ( $current_provider !== NULL ) {
                echo '</div>';
            }
            echo '<div class="service">';
            echo '<p class="lower">' . $data->provider . '</p>';
            $current_provider = $data->provider;
        }
        echo '<a href="?artist=' . $data->artist '">' .
             $data->artist . '</a><br/>';   
    }
    echo '</div>';

How about that.

<?php $query = mysqli->query("SELECT * FROM `table`");
    $current_group = array();
   while ($data= $query->fetch_assoc()){
      if(in_array($data['provider'],$current_group)){
         echo  "<h1>New Group" . $data['provider'] ."</h1>";
         $current_group[] = $data['provider']
      }
     echo $data['artist'] . "<br/>";
   }

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