I am trying to group my data with country and year together and display it in a table but it is currently only grouped by year and not country. As below -
USA 2020 | Reise | Land | Reisedatnum | Dauer | Reise | | ------- | ------ | ------------- | ------- | ------- | |12324 | Ak | 23-10-22 | AF |intensive|
USA 2020 |Reise |Land |Reisedatnum |Dauer |Reise| |-|-|-|-|-| |12323 |AG |3-10-22 |AF| intensive|
But it should be shown in one table only as Country and year are the same, currently is showing in a different table.  I also grouped by keyregion
In database
keyregion = countryname
Year =  $count=$row->jahr;
MySQL Query
    function starttable($year,$country)
    {
 ;
    
        ?>
    <table class="termine" >
        <caption class="date"><b><?php   echo $country;?> <?php echo $year; ?> </b></caption>
        <thead>
        <tr>
            <th scope="col" class="reisenr">Reise Nr.</th>
            <th scope="col" class="land">Land</th>
            <th scope="col" class="datum">Reisedatum</th>
            <th scope="col" class="dauer">Dauer</th>
            <th scope="col" class="reise">Reise</th>
            <th scope="col" class="preis">Reisepreis</th>
        </tr>
        </thead>
        <tbody>
        <?
    }
    function closetable()
    {
        echo "
        </tbody>
    </table>
";
    }
    function ausgabe_einfach($zeile)
    {
        global $status;
        $anfang = htmlentities($zeile->beginn_f,ENT_COMPAT,'UTF-8',0);
        $ende   = htmlentities($zeile->ende_f  ,ENT_COMPAT,'UTF-8',0);
        $commen = ($zeile->comment_de) ? "<div class=\"comment\">". nl2br(htmlentities($zeile->comment_de,ENT_COMPAT,'UTF-8',0)) ."</div>" : "";
        ?>
        <tr>
            <td><?php echo $zeile->reisenr ?></td>
            <td><?php echo $zeile->keycountry .'<a href="http://'. $zeile->Shortlink .'">'.'<br>' .  $zeile->Shortlink . '</a>'?></td>
            <td class="commdate"><?php echo $anfang ?> – <?php echo $ende ?></td>
            <td><?php echo $zeile->tage+1 ?> T</td>
            <td><?php echo $zeile->tourname ?></td>
            <td><?php echo ($zeile->price) ? $zeile->price." Euro" : "-" ?> </td>
        </tr>
        <?
    }
    // Datenbankverbindung
    $connection = @mysqli_connect("localhost","username","password","DB");
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    if ($connection)
    {
        mysqli_set_charset($connection, "UTF8");
        mysqli_query($connection, "SET lc_time_names = 'de_DE'");
        $keywords = "Afrika";
        $zeig = 1;
        if ($keysuffix == "")
            $where = "WHERE keyregion LIKE \"".$keywords."\" AND zeig='$zeig'" ;
        else
            $where = "WHERE ( keyregion LIKE \"".$keywords."\" OR keycountry LIKE \"".$keywords.$keysuffix."\" )";
        $abfrage =  "SELECT reisenr,beginn,ende,airline,status,price,keywords,keyregion,Shortlink,keycountry,tourname, YEAR(beginn) AS jahr, MONTH(beginn) AS month, DATE_FORMAT(beginn,\"%a, %e. %b\") AS beginn_f,DATE_FORMAT(ende,\"%a, %e. %b %Y\") AS ende_f,comment_de, DATEDIFF(ende,beginn) as tage FROM $tabelle $where ORDER BY jahr, keyregion";
        $ergebnis = mysqli_query($connection, $abfrage);
        $opentab = false;        // Tabelle offen
        $p_month=0;
// Aktueller Jahresdurchlauf
        while($row = mysqli_fetch_object($ergebnis))
        {
            if ($row->month != $p_month)
            {
                $p_month=$row->month;
                $datee= htmlentities($row->beginn,ENT_COMPAT,'UTF-8',0);
                $count=$row->jahr;
                $country= $row->keyregion;
                $bigin =htmlentities($row->beginn,ENT_COMPAT,'UTF-8',0);
                if ($opentab)
                    closetable();
                starttable($count,$country);
                $opentab = true;
            }
            ausgabe_einfach($row);
        }
        if ($opentab)
            closetable();
    }
    else
    { ?>
        Daten können nicht geladen werden.
        <?
    } 
