Jump to content

sorting flat file data by date and displaying in table


eche

Recommended Posts

Hi,

 

I'm probably missing something really obvious here but can you please help. What I have is a comma separated flat file which contains:

date,task name,information,url link,contact name,contact email,contact phone #
10/10/10,example,this is an example,http://www.example.com,John Doe,jon@doe.com,12345678

and so on. Data is not sorted.

(Have to use a flat file as no access to DB)

 

What I am attempting to do is extract the data from the file, sort by date (where date is dd/mm/yy) and display in a table. What I then hope to do later is show only selected date ranges (e.g by month or a year view from today's date + 12 months)

 

Here is what I've got right now which at the moment is extracting the date and showing things in a table the way I need them:

 

<table cellpadding="5">
<tr class="trheading">
<td>Date</td>
<td>Task</td>
<td>Contact</td>
</tr>

<?php
$filedata = file("data.txt"); 
$data_arr = array();

foreach($filedata as $line) {  

$explode = explode(",",$line);
$data_arr['date'][] = trim($explode[0]);
$data_arr['trimester'][] = trim($explode[1]);
$data_arr['task'][] = trim($explode[2]);
$data_arr['information'][] = trim($explode[3]);
$data_arr['link'][] = trim($explode[4]);
$data_arr['contact'][] = trim($explode[5]);
$data_arr['email'][] = trim($explode[6]);
$data_arr['phone'][] = trim($explode[7]);
}


foreach($data_arr['date'] as $key=>$value){

	if ($key % 2) {	
echo '<tr class="tralt">';
        } else {
echo '<tr>';
}

    echo "<td>".$data_arr['date'][$key]."</td>\n";
    echo "<td><strong>".$data_arr['task'][$key]."</strong><br />\n";
    echo $data_arr['information'][$key]." <a href=\"" .$data_arr['link'][$key]. "\">" .$data_arr['link'][$key]. "</a></td>\n";
    echo "<td>".$data_arr['contact'][$key]." " .$data_arr['phone'][$key]. "<br /><a href=\"".$data_arr['email'][$key]. "\">".$data_arr['email'][$key]."</a></td>\n";
    echo "</tr>\n";

}


?>
</table>

 

I tried using usort function which was comparing dates however this messed up the data and I ended up with the wrong dates being applied to the wrong data. Sort function works only by sorting the first numbers (in this case day) in ascending order which doesn't help because months and years are out of whack.

 

Must be missing something - have I over complicated my use of arrays - what am I missing here?  :-\

Link to comment
Share on other sites

$filedata = "data.txt";

$openedfile = fopen($filedata);
if($openedfile)
{
while(!feof($openedfile))
{
$line = trim(fgets($openedfile));
if(!empty($line))
{
list($date, $task_name, $information, $url ....etc with each ","  seperated info);

if($date == '2010/01/01')
{
echo "<table>";
//what ever data you wish to display here
echo "</table>";
}
}
}
fclose($openedfile);

or you could also define a $begin_date and $end_date and test to see if the desired date is between those

two dates and then display the data accordingly.

 

Also- instead of tesing '2010/01/10' you could define the $desired_date and plug that in like so;

if($date == $desired_date)

 

 

Hope that helps...

Link to comment
Share on other sites

If you store your date as yyyymmdd you can just natsort($array) after file() reads it in.

 

Thanks, that was probably the easiest way to do it and it worked. I changed dates to that format and I modified my code to be:

 

<table cellpadding="5">
<tr class="trheading">
<td>Date</td>
<td>Task</td>
<td>Contact</td>
</tr>

<?php
$filedata = file("data.txt"); 
$data_arr = array();

natsort($filedata);

foreach($filedata as $line) {  

   $explode = explode(",",$line);
   $data_arr['date'][] = trim($explode[0]);
   $data_arr['trimester'][] = trim($explode[1]);
   $data_arr['task'][] = trim($explode[2]);
   $data_arr['information'][] = trim($explode[3]);
   $data_arr['link'][] = trim($explode[4]);
   $data_arr['contact'][] = trim($explode[5]);
   $data_arr['email'][] = trim($explode[6]);
   $data_arr['phone'][] = trim($explode[7]);
}


foreach($data_arr['date'] as $key=>$value){
   
    if ($key % 2) {   
   echo '<tr class="tralt">';
        } else {
   echo '<tr>';
   }
   
    echo "<td>".$data_arr['date'][$key]."</td>\n";
    echo "<td><strong>".$data_arr['task'][$key]."</strong><br />\n";
    echo $data_arr['information'][$key]." <a href=\"" .$data_arr['link'][$key]. "\">" .$data_arr['link'][$key]. "</a></td>\n";
    echo "<td>".$data_arr['contact'][$key]." " .$data_arr['phone'][$key]. "<br /><a href=\"".$data_arr['email'][$key]. "\">".$data_arr['email'][$key]."</a></td>\n";
    echo "</tr>\n";

}


?>
</table>

 

I was then able to only display entries I wanted by adding:

if (($value < $start_date) || ($value > $end_date )) {}
else {

 

between:

foreach($data_arr['date'] as $key=>$value){

and:

if ($key % 2) {   

 

where $start_date and $end_date are dates set in the same format 'yyyymmdd'

 

Thanks for your suggestions too meltingpoint  :)

Link to comment
Share on other sites

Just also,

 

I wanted to sort by task title with date remaining as the key. I had put an if statement where I put the date range code as above however this produced a table with the shading of every second row incorrectly (where some rows were shaded 3/4 in a row, then not shaded for several rows etc.)

 

Then I realised I probably should have put the if statement before it got to this step where the data is actually put into the array. So I put it in after this line:

 

$explode = explode(",",$line);

 

I put:

if ($explode[1] == $search_task) {
..explode data from lines into the array
}

 

where $search_task was the task name I was looking for. So that section of code now looks like:

 

foreach($filedata as $line) {  

$explode = explode(",",$line);
if ($explode[1] == $search_task) {
$data_arr['date'][] = trim($explode[0]);
$data_arr['task'][] = trim($explode[1]);
.... etc
}

 

Short of using a database, this is what I think is as good as it gets. Thanks  :D

 

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.