user1621727
user1621727

Reputation:

PHP Mysql Order by Condition

I want to order my events well configured, what im facing is,

i order my events if they are open for selling, i show them first, after those i show the ones which are open for reservation and which cannot be sold, but when people make reservation, i open my events for them for 3-4 days where only the ones who made reservation can buy that event,

what i want is simple, i want to show the event that they have made reservation and the event is open for selling for the ones only who made reservation; first in order, after that, the ones which are open for all, and than the ones which cant be bought because they are only for reservation

for instance, A is an event open for all, B is open for reservation, C can be bought by the ones only who made a reservation,

what my query returns is : A - B - C

what i want is : C - A - B

i tried to change the query but i know that i made a mistake there

here is my code :

try
{
$event_query = "SELECT * FROM `events` ORDER BY `reservation` ASC, `eventdate` ASC";
$event_query_check = $db->prepare($event_query);
$event_query_check->execute();
$ac = $db->query("SELECT FOUND_ROWS()")->fetchColumn();
while($fetch = $event_query_check->fetch (PDO::FETCH_ASSOC) ){ 

// fetching the info exc in here


try {
$q = "SELECT * FROM `event_reservation` WHERE `attendee_id` = :attendee_id AND `event_id` = :event_id";
$check_query = $db->prepare($q);
$check_query->bindParam(':attendee_id', $userid, PDO::PARAM_INT);
$check_query->bindParam(':event_id', $event_id, PDO::PARAM_INT);
$check_query->execute();
$n2 = $db->query("SELECT FOUND_ROWS()")->fetchColumn();
}
catch(PDOException $e) {
$log->logError($e." - ".basename(__FILE__));
}

// not working part of the code

 if($n2){
$event_query = "SELECT * FROM `members` ORDER BY `reservation_open_to_sell` DESC, `reservation` ASC, `eventdate` ASC";
}

// ends here

} // end of while
} // end of try
catch(PDOException $e) {
$log->logError($e." - ".basename(__FILE__));
}

Thank you

Upvotes: 0

Views: 462

Answers (1)

Z .
Z .

Reputation: 12837

you can do something like this:

order by case when reservation='c' then 1 
              when reservation='a' then 2
              when reservation='b' then 3 end

Upvotes: 1

Related Questions