-1

Hi I have data in my MYSQL which I am trying to sort by Months and Year together and display it in new HTML table every time sort by Year and months.

trying to do like this -

January 2021
Reise Nr.   Reisedatum  Dauer   Flug    Reis
2122330 Di, 1. jan – Do, 24. jan 2022   23 T    AF  Mali Intensiv

February 2021
Reise Nr.   Reisedatum  Dauer   Flug    Reis
2122330 Di, 1. Feb – Do, 24. Feb 2022   23 T    AF  Mali Intensiv

(Months wise data)

But HTML table is sorted by year only not months. see below the output.

enter image description here

But I am trying to make it like this January 2021 data is different table and OKT = October 2021 data in new table like below image enter image description here

Here is my PHP CODE -

    <?php
 
    $tabelle="Termine_afrika";

    $keysuffix="";

    $status = array('freie&nbsp;Plätze','ausgebucht','nicht mehr buchbar','Restplätze','auf Anfrage','entfällt');

    function starttable($year,$month)
    {
        ?>
    <table class="termine" >

        <caption><?php echo date("F",strtotime($month)); ?> <?php echo $year ?></caption>
        <thead>
        <tr>
            <th scope="col" class="reisenr">Reise Nr.</th>
            <th scope="col" class="datum">Reisedatum</th>
            <th scope="col" class="dauer">Dauer</th>
            <th scope="col" class="flug">Flug</th>
            <th scope="col" class="reise">Reise</th>
            <th scope="col" class="preis">Reisepreis</th>
            <th scope="col" class="status">Status</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 $anfang ?> &ndash; <?php echo $ende . $commen ?></td>
            <td><?php echo $zeile->tage ?>&nbsp;T</td>
            <td><?php echo $zeile->airline ?></td>
            <td><?php echo $zeile->tourname ?></td>
            <td><?php echo ($zeile->price) ? $zeile->price."&nbsp;Euro" : "-" ?> </td>
            <td><?php echo $status[$zeile->status] ?></td>
        </tr>
        <?
    }

 
    $connection = @mysqli_connect("localhost","user","pass","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 = "mali";
        $zeig = 1;
        if ($keysuffix == "")
            $where = "WHERE keycountry LIKE \"".$keywords."\" AND zeig='$zeig'" ;
        else
            $where = "WHERE ( keycountry LIKE \"".$keywords."\" OR keycountry LIKE \"".$keywords.$keysuffix."\" )";

        $abfrage = "SELECT reisenr,beginn,ende,airline,status,price,keywords,keycountry,tourname, YEAR(beginn) AS jahr, DATE_FORMAT(beginn,\"%a, %e.&nbsp;%b\") AS beginn_f,DATE_FORMAT(ende,\"%a, %e.&nbsp;%b %Y\") AS ende_f,comment_de, DATEDIFF(ende,beginn) as tage FROM $tabelle $where ORDER BY jahr, beginn";


        $ergebnis = mysqli_query($connection, $abfrage);

        $opentab = false;        // Tabelle offen
        $count=0;                // Aktueller Jahresdurchlauf

        while($row = mysqli_fetch_object($ergebnis))
        {
            if (intval($row->jahr) > $count)
            {
                $count=$row->jahr;
                $bigin = $row->beginn;
                if ($opentab)
                    closetable();
                starttable($count,$bigin);
                $opentab = true;
            }

            ausgabe_einfach($row);
        }

        if ($opentab)
            closetable();
    }
    else
    { ?>
        Daten können nicht geladen werden.
        <?
    }

?>
 
  • 1
    `if (intval($row->jahr) > $count)` is the code which checks for change of table, this doesn't have the month in it. – Nigel Ren May 03 '21 at 17:13
  • @NigelRen thank you so much but how can I put months in it I am clueless could you please show me or help –  May 03 '21 at 17:17
  • not help I need - if heading is jan 2021 only Jan -feb data is there If Feb 2021 then feb or feb-March data is there –  May 03 '21 at 18:38
  • do you have problem with sort or line break? – Artier May 03 '21 at 19:21
  • order by MONTH(beginn) for sorting issue – Artier May 03 '21 at 19:21
  • I have issue with line break need month by month data not year by year –  May 03 '21 at 19:22
  • GROUP BY YEAR(beginn), MONTH(beginn) – Artier May 03 '21 at 19:25
  • I want to show month by month data not like in 2022 all months data , I need to show January 2022 only jan data, Feb 2022 only Feb data [Image](https://imgur.com/a/mngirrk) check image what is current output and what I need actually –  May 03 '21 at 19:30
  • What is its logic `if (intval($row->jahr) > $count)` like mean `2020>0` when next year `2020>2021` new table. – Artier May 03 '21 at 20:03
  • add one more column `MOTH(beginn) AS month`, in query . `$pre_m=0; if($row->month != $pre_m){ $pre_m=$row->month}` – Artier May 03 '21 at 20:06
  • yes logic is if (intval($row->jahr) > $count) new table creates –  May 03 '21 at 20:08
  • @Artier sorry to say but method not working –  May 03 '21 at 20:12
  • 1
    Check my answer hope it work i update it – Artier May 03 '21 at 20:12
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman May 03 '21 at 20:15

1 Answers1

0

do comparison between months not years and get month from DB also break line and add new line only when month change

Update query

$abfrage = "SELECT reisenr,beginn,ende,airline,status,price,keywords,keycountry,tourname, YEAR(beginn) AS jahr, MONTH(beginn) AS month, DATE_FORMAT(beginn,\"%a, %e.&nbsp;%b\") AS beginn_f,DATE_FORMAT(ende,\"%a, %e.&nbsp;%b %Y\") AS ende_f,comment_de, DATEDIFF(ende,beginn) as tage FROM $tabelle $where ORDER BY jahr, MONTH(beginn)";

update code

$p_month=0;                
// Aktueller Jahresdurchlauf

        while($row = mysqli_fetch_object($ergebnis))
        {
            if ($row->month != $p_month)
            {
                $p_month=$row->month;
                $count=$row->jahr;
                $bigin = $row->beginn;
                if ($opentab)
                    closetable();
                starttable($count,$bigin);
                $opentab = true;
            }

            ausgabe_einfach($row);
        }
Artier
  • 1,648
  • 2
  • 8
  • 22
  • dear Please read my Explanation I have updated my Question, I am trying to do display data month wise and year wise like jan 2021 only jan data , march 2021 only March data like this –  May 03 '21 at 18:43
  • 1
    try now, I have update it. if not work let me know – Artier May 03 '21 at 18:44
  • Hi @Artier what if I need to sort by Country name and group by country name too ? what should I do in that condition ? –  May 06 '21 at 11:26