Jump to content

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

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.