Schlo_50 Posted November 21, 2008 Share Posted November 21, 2008 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 More sharing options...
Zhadus Posted November 21, 2008 Share Posted November 21, 2008 Count is to count the amount of values in an array. Just set $amt = 0; to start with at the beginning of the function. During your if statement, where you print it, do $amt++; and then print $amt. Link to comment https://forums.phpfreaks.com/topic/133668-solved-count-matches-in-loop/#findComment-695519 Share on other sites More sharing options...
Schlo_50 Posted November 21, 2008 Author Share Posted November 21, 2008 Wicked 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 Link to comment https://forums.phpfreaks.com/topic/133668-solved-count-matches-in-loop/#findComment-695528 Share on other sites More sharing options...
flyhoney Posted November 21, 2008 Share Posted November 21, 2008 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; } ?> Link to comment https://forums.phpfreaks.com/topic/133668-solved-count-matches-in-loop/#findComment-695532 Share on other sites More sharing options...
Schlo_50 Posted November 21, 2008 Author Share Posted November 21, 2008 Sweet, Thanking you greatly! Link to comment https://forums.phpfreaks.com/topic/133668-solved-count-matches-in-loop/#findComment-695537 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.