Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a project right now and I'm facing an issue that is about to freak me out.

Basically I had to display information of some courses, in order to do so, I create a custom field called "CourseInfo", that array includes, date, place, and a link to sign in.

So the user create that custom field as long as he needs, cause courses can be infinites, however I need to order the output based on the course date and not based on the last custom field created.

Since the course date includes month and the year, is there a change to order it basd on the date?

I would appreciate any help!

Thanks!

UPDATE** ------------------------------------------

Basically I'm using this code, maybe it will help

 <?php $limit = 1; $courses = array_reverse($courses); foreach ($courses as $course) {
                            $course = nl2br($course);
                            list($date,$city,$link) = explode('<br />',$course);

 echo "<ul>";
 echo "<li>$date</li>";
 echo "<li>$city</li>";
 echo "<li><a href='$link'>Inschrijven</a></li>";
 echo "</ul>";

 }  ?>

And the page is http://www.otoacademie.nl/dev/rijinstructeur-auto-b/

Notice there is not order.

UPDATE===================================================== update dates format

enter image description here

share|improve this question
    
I'm a little confused. Can you give us an example of the data contained in $courses? –  v0idless Nov 1 '11 at 22:19
    
sure, I'm updating the post with a ss of the custom fields =) –  andresmijares25 Nov 1 '11 at 22:23
    
can you do a echo "<pre>" . print_r($courses, true) . "</pre>" and post that output? I'd like to see how the data is actually represented in the array. –  v0idless Nov 1 '11 at 22:33
    
I just set it in otoacademie.nl/dev/rijinstructeur-auto-b Just click "Alle opleidingsdata en locaties zien" and you will see the output –  andresmijares25 Nov 1 '11 at 23:00
add comment

1 Answer

up vote 0 down vote accepted
  1. Multiline is not an array (rant)
  2. You need to use standard dates for this to work DD/MM/YYYY (first row)
  3. Use the PHP function usort() to sort the fields
  4. Use strtotime() to convert that date to a timestamp and date_i18n() to output it

If you need an actual example, change the date formats to what I've shown you above, update the screenshot and I'll give you the code. First line must be DD/MM/YYYY.

THE CODE

// Demo set (replace with you custom fields)
$courses = array(
    '27/10/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
    '19/10/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
    '17/01/2011<br />'. // Date
        'Amsterdam<br />'. // City
            'http://www.google.com/', // Link
);
// Convert the courses to objects and translate the dates
$sorted_courses = array(); // Holds objects for sorting
foreach($courses as $course){
    $course = nl2br($course);
    // Replace <br /> with \r\n
    $course = preg_replace('~<br[\s]*/[\s]*>~i', PHP_EOL, $course);
    // Split by \r\n and trim each entry
    list($date, $city, $link) = array_map('trim', preg_split('~[\r\n]+~', $course));
    // Validate the date format [ :) ]
    if(!preg_match('~^([0-9]{2})/([0-9]{2})/([0-9]{4})$~', $date, $slices)) continue;
    // Extract date elenents
    list($match, $day, $month, $year) = $slices;
    // And now create the object
    $sorted_courses[] = (object)array(
        'time'  => $time = mktime(1, 1, 1, $month, $day, $year),
        'date'  => $date = date_i18n('d F Y', $time),
        'city'  => $city,
        'link'  => $link,
    );
}
// Sort elements ascending (using callback)
usort($sorted_courses, function($item1, $item2){
    // Compare the timestamps and sort ascending
    return $item1->time > $item2->time;
});
// Got through the sorted courses and display them
foreach($sorted_courses as $sorted_course){
    echo "<ul>";
        echo "<li>{$sorted_course->date}</li>";
        echo "<li>{$sorted_course->city}</li>";
        echo "<li><a href='{$sorted_course->link}'>Inschrijven</a></li>";
    echo "</ul>";
}
share|improve this answer
    
just update it :) hope u can help me! thanks a ton! –  andresmijares25 Nov 2 '11 at 16:54
    
Check it out :) Have fun! –  EarnestoDev Nov 2 '11 at 17:18
1  
thanks a ton! u rock! –  andresmijares25 Nov 2 '11 at 17:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.