Forum Moderators: coopster

Message Too Old, No Replies

Show data by date

         

krowazpola

7:10 pm on Nov 15, 2011 (gmt 0)

10+ Year Member



Hi on server i have a txt file looking like this:

2011-11-14 15:39:51,Polnoc
2011-10-13 15:41:54,Polnocny-Wschod
2011-10-30 15:43:04,Wschod

And php code that count all occurences of same strings looking like that:

$getText = file_get_contents("Kier.txt", true);
$Poln = substr_count($getText ,"Polnoc");
$PolnW = substr_count($getText ,"Polnocny-Wschod");
$Wsch = substr_count($getText ,"Wschod");
$PoldW = substr_count($getText ,"Poludniowy-Wschod");
$Pold = substr_count($getText ,"Poludnie");
$PoldZ = substr_count($getText ,"Poludniowy-Zachod");
$Zach = substr_count($getText ,"Zachod");
$PolnZ = substr_count($getText ,"Polnocny-Zachod");
$getPoln = $Poln - $PolnW - $PolnZ ;
$getPold = $Pold;
$getZach = $Zach - $PoldZ - $PolnZ ;
$getWsch = $Wsch - $PoldW - $PolnW ;

echo "Polnoc =". $getPoln;
echo "<br>";
echo "Polnocny-wschod =". $PolnW;
echo "<br>";
echo "Wschod =". $getWsch ;
echo "<br>";
echo "Poludniwy-Wschod =". $PoldW ;
echo "<br>";
echo "Poludnie =". $getPold ;
echo "<br>";
echo "Poludniwy-Zachod =". $PoldZ ;
echo "<br>";
echo "Zachod =". $getZach ;
echo "<br>";
echo "Polnocny-Zachod =". $PolnZ ;


It works fine but counts all occurences in file. I would like to do 3 butons and show occurences from last month,lat week, last day.

How to do it? Do my code will be helpful with it?

eelixduppy

2:22 am on Nov 16, 2011 (gmt 0)



This becomes more difficult than what you are currently doing. You are going to have to parse the data from beginning to end, splitting each line on the comma, then matching the date as needed and storing the count in appropriate variables.

Look at date_parse [php.net] to get you started with the date field.

I would have a structure like the following:


$counts = array(
array(
"name" => "Polnoc",
"count" => array(
"lastmonth" => 123,
"lastweek" => 50,
"yesterday" => 16
),
array(
"name" => "Polnoc-Wschod",
"count" => array(
"lastmonth" => 123,
"lastweek" => 50,
"yesterday" => 16
),
etc.....
);



With each new "name" that isn't already in the array, append a new sub array (as above) to the $counts array for that name.

Since you are only interested in last month's data (as well as sub groups of that) I would skip any lines that do not belong to last month. Any time the date matches last month, add to the respective "lastmonth" field for that name; any time the date matches last week && last month, add to both "lastmonth" and "lastweek" counts, respectively; continue this for "yesterday" as well.

Once you get to the end of the file you can iterate over this array doing whatever you like with the data.

krowazpola

12:27 pm on Nov 16, 2011 (gmt 0)

10+ Year Member



O used this code and it shows me data only form last month,week,day.

$lines = file('Kier.txt');
$monthlist = "";
$weeklist = "";
$daylist = "";
foreach($lines as $line){
$r = explode(",",$line);
if(strtotime($r[0]) > strtotime('-1 month'))$monthlist .= "\n\t<li>{$r[1]}</li>";
if(strtotime($r[0]) > strtotime('-1 week'))$weeklist .= "\n\t<li>{$r[1]}</li>";
if(strtotime($r[0]) > strtotime('-1 day'))$daylist .= "\n\t<li>{$r[1]}</li>";
}
if($monthlist !="")$monthlist = "\n<ul>$monthlist\n</ul>";
if($weeklist !="")$weeklist = "\n<ul>$weeklist\n</ul>";
if($daylist !="")$daylist = "\n<ul>$daylist\n</ul>";
//....
?>

<h3>DAYLIST</h3>
<?php echo $daylist;?>
<h3>WEEKLIST</h3>
<?php echo $weeklist;?>
<h3>MONTHLIST</h3>
<?php echo $monthlist;?>


Is there any way to gat it workinkig with my code? I mean to do 3 buttons and when i clik on week list it will show me occurence of same data only i last week?

rocknbil

4:43 pm on Nov 16, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you considered dumping the data in a temporary mysql table and doing the sorting using select statements? That's what I'd do, I think I'd spend a lot less time on it that way.