Jump to content

Recommended Posts

Hey guys, i have this script where my .txt file loads events and displays the ones relevant to the month they belongs in.

 

My .txt file stores data in this format. --> ('Date|'Month'|'Message') Practical E.G 1st|Jan|Test1

If i have multiple events happening in Jan i want to order the events so that they run in the order of 1st, 2nd etc through to 31st when all events in 'Jan' are called. At the moment if i enter an event for the 2nd of Jan, then the 10th of Jan and finally the 7th of Jan they will be displayed in that order and not date order!

 

Please can someone help me? Maybe i just need a small piece of code to order the called results?

I hope you can understand what i am trying to do, thanks uin advance!

 

<?php
//Use the following function to retrive data for each month.
//Look at the data structure.

function getevents($eventmonth){

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

foreach($file as $key => $val){ //loop through array

  $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];

  if($month == $eventmonth){ //will only list month you specify in function.

   $out = "$day - $event<br>\n";
   print $out;

  }

}

}

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

Link to comment
https://forums.phpfreaks.com/topic/54859-solved-i-need-to-order-my-txt-file/
Share on other sites

Rather than echo'ing out the events when you find them. Save them to a variable which is an array call this variable, $events and then change this portion of code:

if($month == $eventmonth){ //will only list month you specify in function.

   $out = "$day - $event<br>\n";
   print $out;

  }

To this:

//will only list month you specify in function.
        if($month == $eventmonth)
        {
           // save each event for the month to the an array called events
           // we'll use the day as the key for the variable
           // the key will be used to sort the events in ascending order
            $events[$day] = $event;
        }

 

Now after the foreach loop have this code:

    // order the keys in ascending order, remember the day is the key for the $events variable
    ksort($events);

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

 

Put it altogether an you get this:

<?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;
        }
    }

    // order by day
    ksort($events);

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

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

?>

 

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.