简体   繁体   中英

problem with sorting an html php mysql table

I'm a learning programmer and I have an question.

I have an Html table on my website, and I fill it with my data from my MySql.

Now I want to sort my Table Datewise, so I want the nearest date at the top of the table.

How do I do that ( My Date format is [dd.mm] no years )

With the ORDER BY method the 01.11 is for example the first entry and not after the 23.10.

My Code:

I hope you could help me because I am not getting it.

    <!DOCTYPE html>
<html>
<head>
    <title>lll</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="style.css">

    <script type="text/javascript">

    function eintragen(){
        window.location="http://lll.bplaced.net/eintragen.php"
    }
    </script>
</head>
<body>

    <p class="headertext">Online lll</p>
    <hr><br>

    <?php

        $servername = "localhost";
        $username = "kllllf";
        $password = "lll";
        $dbname = "kll";
        $conn = mysqli_connect($servername, $username, $password, $dbname);
        if (mysqli_connect_errno()) {
            die("Connection failed: " . mysqli_connect_error());
        }



        $rows = 0;
        $sql = "
        SELECT
            *
        FROM
            lll
       (ORDER BY
            Ende)
        ";
        $query = mysqli_query($conn, $sql);
        $num = mysqli_num_rows($query);


        echo "<table name=\"table\"border=\"1\">";
                echo "<tr>";
        echo "<td align=\"center\" valign=\"middle\" width=\"150px\" height=\"50px\">Bis Wann</td>";
        echo "<td align=\"center\" valign=\"middle\" width=\"150px\">Fach</td>";
        echo "<td align=\"center\" valign=\"middle\" width=\"350px\">lll</td>";
        echo "</tr>";
                echo "<tr>";
        echo "<td align=\"center\" valign=\"middle\" height=\"50px\">× × ×</td>";
        echo "<td align=\"center\" valign=\"middle\">× × ×</td>";
        echo "<td align=\"center\" valign=\"middle\">× × ×</td>";
        echo "</tr>";

        while($row = mysqli_fetch_assoc($query)){

            $fach = $row['Fach'];
            $ha = $row['Hllln'];
            $ende = $row['Ende'];

            if($ende < date("d.m")) {
                       $sqls = "DELETE FROM hlllll WHERE Ende='$ende';";
            if(!mysqli_query($conn, $sqls)){
            echo "<p>Es gab keine älteren Hlllll</p>";
        }

            }

        echo "<tr>";
        echo "<td align=\"center\" valign=\"middle\" height=\"50px\">".$ende."</td>";
        echo "<td align=\"center\" valign=\"middle\">".$fach."</td>";
        echo "<td align=\"center\" valign=\"middle\">".$ha."</td>";
        echo "</tr>";
        }




        echo "</table>";

        echo "<br />";
        echo "<button type=\"button\" onclick=\"eintragen()\">Hlllllln eintragen?</button>";



?>





</body>

Convert your date column into datetime and use the below query.

$sql = " SELECT * FROM lll
         ORDER BY convert(datetime, Ende , 104) ASC ";

Here, 104 stands for dd.mm format

Method 2:

$sql = "SELECT * FROM lll 
        ORDER BY STR_TO_DATE(Ende ,"%d.%m") ASC";

由于您只有一天和一个月,因此可以尝试一下。

SELECT * FROM lll ORDER BY STR_TO_DATE(Ende,'%d.%m') DESC

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