nethken nethken - 1 year ago 111
PHP Question

PHP - Sort by month but the data type is varchar?

Hello i want to fetched all the data order in month but my problem is the data type of month to my database is varchar can someone give me ideas how to do it?

here is the structure of my sql
enter image description here

here is the output of the query to the events

enter image description here

and here is the code of my events

<div class="col-xs-6 col-md-3">
<div class="title2">
<h2><i class="fa fa-calendar"></i> Upcoming Events</h2>
<div class="fix sidebar floatright">
<div class="fix single_sidebar">
<div class="events">

$sql ="SELECT * FROM events ORDER BY month";
$result = mysqli_query($con, $sql);

while($row = mysqli_fetch_array($result)){

$month = $row['month'];
$day = $row['day'];
$year = $row['year'];
$etitle = $row['event_title'];
$stime = $row['start_time'];
$etime = $row['end_time'];
$evenue = $row['event_venue'];
<div class="fix single_events">
<div class="date">
<span class="month"><?php echo $month; ?></span>
<h1 class="day"><?php echo $day; ?></h1>
<h2><?php echo $etitle; ?></h2>
<p><?php echo $stime; ?> - <?php echo $etime; ?></p>
<p><?php echo $evenue; ?></p>


Answer Source

The problem is that month is a string, not a numeric value.

Without knowing the values, it is hard to suggest a particular solution. But here is one idea:

order by field(month, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download