/    Sign up×
Community /Pin to ProfileBookmark

ORDER BY php array in MySQL

I query my database by date and order it by time. The issue is that the times are stored as strings not integers in the database. So they do not come out in the correct order. I am trying to use a PHP array to order them correctly, but it is giving me errors. I found this script online and others have said it worked for them. It is not working for me, maybe someone can tell me what I’m doing wrong. Here is the script:

[code=php]

$array = array (‘9:00 AM’, ’10:00 AM’, ’10:30 AM’, ’11:00 AM’, ’11:30 AM’, ’12:00 PM’, ’12:30 PM’, ‘1:00 PM’, ‘1:30 PM’, ‘2:00 PM’, ‘2:30 PM’, ‘3:00 PM’);
$list = implode(‘,’, $array);
$orderbylist = ‘ORDER BY ‘; // initialize
foreach ($array as $time) {
$orderbylist .= “time=$time ASC,”;
}
$orderbylist = rtrim($orderbylist, ‘, ‘);

$query = “SELECT CONCAT( last_name, ‘, ‘, first_name) AS name, date AS date, time AS t, phone_number AS pn, email AS e, comments AS c, user_id AS id FROM client_info WHERE (date = ‘$d’) AND time IN ($list) $orderbylist”;
[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@GUIRAug 17.2009 — Try with,

$array = array ([b][SIZE="4"]"[/SIZE][/b]'9:00 AM'[b][SIZE="4"]"[/SIZE][/b], "'10:00 AM'," "'10:30 AM'",...., "'3:00 PM'");
Copy linkTweet thisAlerts:
@teddybearauthorAug 17.2009 — Try with,

$array = array ([b][SIZE="4"]"[/SIZE][/b]'9:00 AM'[b][SIZE="4"]"[/SIZE][/b], "'10:00 AM'," "'10:30 AM'",...., "'3:00 PM'");[/QUOTE]


I feel like crying, hugging you, kissing you, etc. I've been trying to figure this out for 2 weeks. Your solution solved the problem. Thankyou So Much!
Copy linkTweet thisAlerts:
@GUIRAug 18.2009 — ?
×

Success!

Help @teddybear spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...