Jump to content

Ordering Lists


Schlo_50

Recommended Posts

I have developed a script (with help) that tries to order events stored in my .txt file by date.

The .txt file looks like this: 1st|Jan|Meeting at House| 15th|Feb|Meeting at your House etc

 

The script orders the dates from lowest to highest but does not order dates before the 10th. Apparently 1st - 9th comes after 31st..

Here is a picture to explain:

 

untitledai4.jpg

 

This is the script:

 

<?php

function getevents($eventmonth)
{
    $file = file("data.txt"); //puts each line of the file into an array

    //loop through array
    foreach($file as $key => $val)
    {
        $data[$key] = explode("|", $val); //breaks up each line into seperate arrays, values seperated by |

        $day = $data[$key][0];
        $month = $data[$key][1];
        $event = $data[$key][2];

        //will only list month you specify in function.
        if($month == $eventmonth)
        {
            $events[$day] = $event;
        }
    }

    
    ksort($events);

    foreach($events as $day => $event)
    {
        echo "$day - $event<br />\n";
    }
}

//Using the function
getevents("Jan");

?>

 

Can someone revise the code so that the order goes for example: 1st-> 3rd-> 8th-> 11th-> 24th

and not: 11th-> 24th-> 1st-> 3rd

 

Thanks in advance guys!!

Link to comment
Share on other sites

The real problem lies with your data.  Sorting occurs by ASCII value of each character in a string so logically 9th would come after 31st (because 9 comes after 3).  If instead your dates were simply 01, 02, ... 31 then you could sort properly - and then if you want to display the day numbers in a more convenient manner.

Link to comment
Share on other sites

Hi Schlo_50 I helped you last time and provided you the code for this. Could you post an example of how data.txt is formatted.

 

The way I have it coded is when the days are pulled form the file I save the day inro the array index and then assign the event to that index. ksort is then used to sort the array indexes into numerical order, eg  1, 8, 11, 21. This how I get the events to show up in order of day. The code should be doing the trick.

 

EDIT:

I see what is wrong now. Before (when I coded this script for you) you didn't have the date suffix in data.txt, eg: st, nd, rd etc. That is what is throwing the list out of order. If you remove the suffix you'll see it'll display in the correct order.

Link to comment
Share on other sites

Ok Schlo_50 try this:

<?php

// get the suffix for the date
function get_suffix($day)
{
    // create a unix timestamp of the day
    $time = mktime(0, 0, 0, 0, $day);

    // using the unix timestamp get the date
    // suffix using PHP's built in date function
    $suffix = date('S', $time);

    // return the day with suffix
    // eg 1st, 3rd, 20th
    return $day . $suffix;
}

function getevents($event_month)
{
    $file = file("data.txt"); //puts each line of the file into an array

    //loop through array
    foreach($file as $key => $val)
    {
        $data[$key] = explode("|", $val); //breaks up each line into seperate arrays, values seperated by |

        $day   = $data[$key][0];
        $month = $data[$key][1];
        $event = $data[$key][2];

        //will only list month you specify in function.
        if($month == $event_month)
        {
            $events[$day] = $event;
        }
    }

    ksort($events);

    foreach($events as $day => $event)
    {
        echo get_suffix($day) . " - $event<br />\n";
    }
}

//Using the function
getevents("Jan");

?>

This time save the data.txt in this formatt:

3|Jan|What to do..
10|Jan|Birthday
...

The code will now take care of getting the correct suffix for the day and your list will be in the correct numerical order.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.