Hi i have a code that shows me some repitnig data from .txt file. Code looks like:

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

and data in text file looks like:

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

And now its shows me all the occurences in txt file. I want to show do that it would show me only data form last monht,week,day but dont know how to start, do any of you have some good simple idea??

Hi Drugs,

first suggestion: use explode(): http://de2.php.net/manual/en/function.explode.php. This is in my opinion one of the most powerful functions in php. At least I use it every day. It helps you tu truncate the data.

If you only want to show specified dates, you can simply generate a timestamp with mktime http://de2.php.net/manual/en/function.mktime.php and do a simple eqal or greater condition to find out if the data is too old.

Hope that helps for the beginning, Simon

Yes i wont to do some buttons and show occurences from last month,last week and from day.

So do i must change the whole code or just add to it another function??

Member Avatar

diafol

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

Off top of head, not tested (\n and \t - just me being finicky with html formatting - not req'd).

WoW thanks kind sir its working.

Can i use it with my code?? I mean to get some buttons and when i clik on Daylist i will show me how many times it occured in Day.

Member Avatar

diafol

Try this:

function getList($param){
	$lines = file('Kier.txt');
	$list = "";
	foreach($lines as $line){
		$r = explode(",",$line);
		if(strtotime($r[0]) > strtotime("-1 $param"))$list .= "\n\t<li>{$r[1]}</li>";
	}
	if($list !="")$list = "\n<ul>$list\n</ul>";
	return "<h3>" . ucwords($param) . " List</h3>\n" . $list;
}

$output = "";
if(isset($_GET['list']) && in_array($_GET['list'],array("month","week","day"))){
	$output = getList($_GET['list']);		
}
?>

<!-- DTD and head and top of page stuff here -->

<form id="getlist" name="getlist">
    <button name="list" value="month">Month List</button>
    <button name="list" value="week">Week List</button>
    <button name="list" value="day">Day List</button>
</form>

<?php
	echo $output;
?>

<!-- bottom of page and close body/html tags here -->