Jump to content

[SOLVED] Count matches in loop


Schlo_50

Recommended Posts

Hello,

 

I am trying to query data stored in a flatfile and have run into a little problem.

 

I want to count the amount of lines entered into the flat file during January 2007. I have established the variables and now can't seem to count() them. The output is just a long list of '1's.

 

Could someone show me how to count the entries correctly please?

 

function osevenJan() {
    
$posted = "posted";
$file = file("file.DAT");

foreach($file as $Key => $Val){

    $Data[$Key] = explode("|", $Val);
if($posted == trim($Data[$Key][3])){

	$shorthand = $Data[$Key][2];
	$date = explode("/" , $shorthand);

	$month = $shorthand[3].$shorthand[4];
	$year = $shorthand[6].$shorthand[7];

if($year == "07" && $month == "01") {

		print count($shorthand);

}
}
}
}

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/133668-solved-count-matches-in-loop/
Share on other sites

Wicked  :D

 

Thanks for the quick response. Is there anyway that I can format the output to print only the total value instead of every value up to the last?

 

E.g At the moment I get: 1 2 3 4 5 6 7 8 9 10 11 12 etc etc

 

function osevenJan() {
    $total = 0;
$posted = "posted";
$file = file("file.DAT");

foreach($file as $Key => $Val){

    $Data[$Key] = explode("|", $Val);
if($posted == trim($Data[$Key][3])){

	$shorthand = $Data[$Key][2];
	$date = explode("/" , $shorthand);

	$month = $shorthand[3].$shorthand[4];
	$year = $shorthand[6].$shorthand[7];

if($year == "08" && $month == "02") {

		$total++;
		print $total;
		print "<br />";

}
}
}
}

 

Thanks

Like this?

 

<?php
function osevenJan() {
    $total = 0;
   $posted = "posted";
   $file = file("file.DAT");
   
   foreach($file as $Key => $Val){
      
    $Data[$Key] = explode("|", $Val);
   if($posted == trim($Data[$Key][3])){
   
      $shorthand = $Data[$Key][2];
      $date = explode("/" , $shorthand);
   
      $month = $shorthand[3].$shorthand[4];
      $year = $shorthand[6].$shorthand[7];
   
   if($year == "08" && $month == "02") {
         
         $total++;
         print "<br />";
         
   }
   }
   }
   print $total;
   }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.