Bootstrap panel sorting by dates with php

184 Views Asked by At

I'm building php mysql app for job interviews, and I want to have for admin filer with bootstrap panels which are sorted by date in the same color column.

$db = connectPDO();
   $query = ("SELECT razgovori.*, djelatnik.ime, djelatnik.prezime
        FROM razgovori INNER JOIN djelatnik 
        ON djelatnik.id = razgovori.insert_by 
        ORDER BY datum_raz ASC");

  $stmt = $db->prepare($query);
  $stmt->execute();
  $count = $stmt->rowCount();
  $row = $stmt->fetch(PDO::FETCH_ASSOC);
  $date = strtotime($row['datum_raz']);
  $twoDaysAgoStart =  strtotime('-2 day 00:00:00');
  $yesterdayEnd =  strtotime('yesterday 23:59:59');
  $todayStart = strtotime('today 00:00:00');    
  $todayEnd = strtotime('today 23:59:59');  
  $tomorrowStart =  strtotime('tomorrow 00:00:00'); 
  $twoDaysAheadEnd =  strtotime('+2 day 23:59:59');

   <div class="container">
  <div class="row">

    <?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)): ?>

     <?php $date = strtotime($row['datum_raz']); ?>   

    <?php if ($twoDaysAgoStart < $date && $yesterdayEnd > $date): ?> 

  <!--Panel info if past time Job interviews-->
  <div class="col-md-4 pull-left">
   <div class="panel panel-info">
    <div class="panel-heading"><h3 class="panel-title"><?php echo 
     datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
    <div class="panel-body">
      <div class="col-md-9">
        <h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php 
         echo $row['rime'].' '.$row['rprezime']; ?></a></h5>           
          <?php echo $row['kontakt_br']; ?>
      </div>
      <div class="col-md-3">
        <span class="label label-default pull-right"></h5><?php echo 
    $row['ime'].' '.$row['prezime']; ?></span>
      </div>
    </div>
  </div>      
</div>
<!--//Panel--> 
</tr>
<tr>
<!--Panel primary if Job interview is today-->
<?php elseif ($todayStart <= $date && $todayEnd >= $date): ?>  
 <div class="col-md-4">
  <div class="panel panel-primary">
    <div class="panel-heading"><h3 class="panel-title"><?php echo 
datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
    <div class="panel-body">
   <div class="col-md-9">
    <h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php echo 
$row['rime'].' '.$row['rprezime']; ?></a></h5>           
        <?php echo $row['kontakt_br']; ?>
      </div>
        <div class="col-md-3">
            <span class="label label-default pull-right"></h5><?php echo 
$row['ime'].' '.$row['prezime']; ?></span>
          </div>
    </div>
  </div>      
</div>
</tr>
<tr>
<!--Panel warning if Job interview is tomorrow-->
<?php  elseif ($tomorrowStart <= $date && $twoDaysAheadEnd >= $date): ?>   
 <div class="col-md-4 pull-right">
  <div class="panel panel-warning">
    <div class="panel-heading"><h3 class="panel-title"><?php echo 
        datumVrijemeSQLuHR($row['datum_raz']); ?></h3></div>
    <div class="panel-body">
      <div class="col-md-9">
      <h5><a href="editRazgovori.php?id=<?php echo $row['id']; ?>"><?php 
        echo $row['rime'].' '.$row['rprezime']; ?></a></h5>           
          <?php echo $row['kontakt_br']; ?>
        </div>
          <div class="col-md-3">
              <span class="label label-default pull-right"></h5><?php echo 
                      $row['ime'].' '.$row['prezime']; ?></span>
            </div>

        </div>
      </div>      
      </div>
     <?php endif; ?>


    <?php endwhile; ?> 

  </div><!--/row-->
</div><!--/container-->

I want to have sorted like thiswith Main Panal or without

I get this:enter image description here

Because stackoverflow red message constantly asking me for more details, i can't put my all php code

1

There are 1 best solutions below

2
Ronnie Oosting On

You can do this in your MySQL query.

Example:

SELECT * FROM t1
  ORDER BY key_part1, key_part2;

SELECT * FROM t1
  WHERE key_part1 = constant
  ORDER BY key_part2;

SELECT * FROM t1
  ORDER BY key_part1 DESC, key_part2 DESC;

SELECT * FROM t1
  WHERE key_part1 = 1
  ORDER BY key_part1 DESC, key_part2 DESC;

SELECT * FROM t1
  WHERE key_part1 > constant
  ORDER BY key_part1 ASC;

SELECT * FROM t1
  WHERE key_part1 < constant
  ORDER BY key_part1 DESC;

SELECT * FROM t1
  WHERE key_part1 = constant1 AND key_part2 > constant2
  ORDER BY key_part2;

More information can be found here: https://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html or https://www.w3schools.com/sql/sql_orderby.asp

If you want to do this inside PHP you can use sort:

<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
    echo "fruits[" . $key . "] = " . $val . "\n";
}

?>

The above example will output:

fruits[0] = apple fruits[1] = banana fruits[2] = lemon fruits[3] = orange

More information about PHP sort(): http://php.net/manual/en/function.sort.php