Problème de trie sur une colonne date
Bonjour,
voila j'ai un petit souci, j'ai un tableau qui me retourne des données de ma table, et j'ai récupérer un code pour trier chaque colones :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| <?php
// Connexion à la base de donnée
mysql_connect('localhost','root','');
mysql_select_db('annu_tel');
// Le nom de notre table
$tablename = 'info_personne';
// Tri sur colonne
$tri_autorises = array('nom','prenom','etablissement','login','LibFonction', 'LibService', 'typepersonnel', 'fincontrat');
$order_by = in_array($_GET['order'],$tri_autorises) ? $_GET['order'] : 'nom';
// Sens du tri
$order_dir = isset($_GET['inverse']) ? 'DESC' : 'ASC';
// Préparation de la requête
$sql = "
SELECT *
FROM {$tablename}
ORDER BY {$order_by} {$order_dir}
";
$result = mysql_query($sql);
// Notre fonction qui affiche les liens
function sort_link($text, $order=false)
{
global $order_by, $order_dir;
if(!$order)
$order = $text;
$link = '<a href="?order=' . $order;
if($order_by==$order && $order_dir=='ASC')
$link .= '&inverse=true';
$link .= '"';
if($order_by==$order && $order_dir=='ASC')
$link .= ' class="order_asc"';
elseif($order_by==$order && $order_dir=='DESC')
$link .= ' class="order_desc"';
$link .= '>' . $text . '</a>';
return $link;
}
// Affichage
?>
<style type="text/css">
a.order_asc,
a.order_desc:hover {
padding-right:15px;
background:transparent url(s_asc.png) right no-repeat;
}
a.order_desc,
a.order_asc:hover {
padding-right:15px;
background:transparent url(s_desc.png) right no-repeat;
}
</style>
<table>
<tr>
<th><?php echo sort_link('Nom', 'nom') ?></th>
<th><?php echo sort_link('Prenom', 'prenom') ?></th>
<th><?php echo sort_link('Site', 'etablissement') ?></th>
<th><?php echo sort_link('Login', 'login') ?></th>
<th><?php echo sort_link('Fonction', 'LibFonction') ?></th>
<th><?php echo sort_link('Service', 'LibService') ?></th>
<th><?php echo sort_link('Type personnel', 'typepersonnel') ?></th>
<th><?php echo sort_link('Date de fin de contrat', 'fincontrat') ?></th>
</tr>
<?php while( $row=mysql_fetch_assoc($result) ) : ?>
<tr>
<td><?php echo $row['nom'] ?></td>
<td><?php echo $row['prenoml'] ?></td>
<td><?php echo $row['etablisseement'] ?></td>
<td><?php echo $row['login'] ?></td>
<td><?php echo $row['LibFonction'] ?></td>
<td><?php echo $row['LibService'] ?></td>
<td><?php echo $row['typepersonnel'] ?></td>
<td><?php echo $row['fincontrat'] ?></td>
</tr>
<?php endwhile ?>
</table> |
Mon problème est le suivant : j'ai une colonne "Date de fin de contrat" qui en fait est de type "Varchar" , et donc par conséquent le trie se fait sur les jours car les dates sont écrites sous le format jj/mm/yyyy.
Alors j'ai voulu changer directement le type de la colonne dans phpMyAdmin, mais évidemment il ma remis toute mes dates a 0000-00-00.
Donc est-ce que vous savez si on peut convertir le type varchar vers date d'une colonne en gardant les valeurs (car il y a plus de 2000 personnes dans la table:lol:) ou alors si vous avez une astuce pour que j'arrive quand même a trier mes champs varchar mais dans le bon ordre:cry:
Voila j'espère avoir votre soutient:cry:
Merci beaucoup;)