0

I have a problem with my sorting and I don't understand what is the problem. I have a table

id  id_teacher     subject  class   hour           day
1   2 [->]           Math      X C     8         Monday
2   2 [->]           Math      X C     12       Wednesday
3   2 [->]           Math      X C     9         Tuesday
4   2 [->]           Math      VI B    10       Monday
5   2 [->]           Math      X C     11       Monday
6   2 [->]           Math      X C     10       Tuesday
7   5 [->]           Chimie   X C     9         Monday
8   5 [->]           Chimie   X C     12       Monday
9   2 [->]           Sport     X C      7         Monday

And I have a function that prints me 'hour' and 'subject'. The function is this:

function OreMonday($item){
        $sth = $this->dbh->prepare("SELECT class FROM elevi WHERE id_elev = :id_elev;");
        $sth->bindParam(":id_elev", $item);   
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        $sth1 = $this->dbh->prepare("SELECT hour, subject, day FROM hourr WHERE class = :class ORDER BY hour DESC;");
        $sth1->bindParam(":class", $result['class']);   
        $sth1->execute();
        while ($result1 = $sth1->fetch(PDO::FETCH_ASSOC)) {
            if ($result1['day'] == 'Monday') {
                echo $result1['hour'];
                echo $result1['subject']."<br>";
            }
        }    
    }

}

$item is $_SESSION['id'] and $result['class'] is X C from another statement

The result is this:

Monday
9Chimie
8Math
7Sport
12Chimie
11Math

And I want:

Monday
7Sport
8Math
9Chimie
11Math
12Chimie
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Comy
  • 83
  • 2
  • 9

1 Answers1

4
  1. in the db, the hour column should be a numeric column, not a varchar or string column*
  2. you should order ASC, not DESC

*if (and only if) for whatever reason you are not able to change the column type: check mysql sort string number

Community
  • 1
  • 1
Fortega
  • 19,463
  • 14
  • 75
  • 113