Welcome to WebmasterWorld Guest from 3.227.3.146

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

count entries

     
8:58 pm on May 5, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


Hi,

I have a script that writes a variable $v1 into a flat txt file. First, it checks if the variable is presented. If it is, it skips the action, if not, it writes it into the txt file.
The script:
$lines = array_map('trim',file("/path/write.txt"));
$fp = fopen("/path/write.txt", "a");
if(in_array($v1,$lines))
{
fclose($fp);
} else {
fwrite($fp, "$v1\n");
fclose($fp);
}


The txt file could look like this:

variable1
variable3
variable7
etc

Now I would like to count each hit, so it looks like this:

variable1, 45
variable3, 78
variable7, 9
etc

where the numbers would represent how many times a certain variable has been noted.

Edit: deleted second code example that actually did not matter in this case.

Thanks
9:27 pm on May 5, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
posts:5852
votes: 200


Any particular reason you're using a flat file instead of a database?
9:42 pm on May 5, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


Do you absolutely need the text file to have this format ?

Otherwise here is a simple example (I didn't test it)


$list_var = @unserialize ( @file_get_contents ( $file_name ) ) ;

if ( $list_var === false )
{
$list_var = array ( ) ;
}

if ( isset ( $list_var [ $v1 ] ) )
{
$list_var [ $v1 ] ++ ;
}
else
{
$list_var [ $v1 ] = 1 ;
}

file_put_contents ( $file_name , serialize ( $list_var ) , LOCK_EX ) ;
12:23 am on May 6, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


Thanks.

Would $file_name in this case be "/path/write.txt" ?

Any particular reason you're using a flat file instead of a database?

It's an old code I used to track some variables that had not many entries, so checking/purging a flat text file was just easy.
Now what I'm doing is some sort of testing where I would turn this writing part off, once the testing is finished.
Plus, less code for a txt file.
1:01 am on May 6, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


Ok, I got that working, and thank you as it seems it does what I wanted it for. It looks like this:

a:6:{s:29:"/page1.html";i:2;s:20:"/page3.html";i:1;s:0:"";i:1;s:27:"/page2.html";i:1;s:34:"/page4";i:1;s:29:"/page5";i:2;}

Now, I guess "i:2" means it got counted 2 times so far. But what would "a" and "s" stand for?

Also, how do I enter a line break after each entry, between "i" and "s" ?

Thank you
9:59 am on May 6, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


In my example , you can't modify the format, because it's PHP's own representation of an array. ( "a:6" means it's an array made of "6" rows. "s:29" means the following field is a string which has 29 characters ). That's why I asked if you needed a particular format in your text file.

if you really need a human readable file, you might try this (not tested) :


$content = @file_get_contents ( $file_name ) ;
$list_var = array ( ) ;

if ( $content !== false )
{
$rows = explode ( "\n" , $content );
foreach ( $rows as $row )
{
$fields = explode ( "," , $rows ) ;
$field_name = trim ( $fields [ 0 ] ) ;
if ( $field_name != '' )
{
$counter = trim ( isset ( $fields [ 1 ] ) ? $fields [ 1 ] : 0 ) ;
$list_var [ $field_name ] = $counter ;
}
}
}

if ( isset ( $list_var [ $v1 ] ) )
{
$list_var [ $v1 ] ++ ;
}
else
{
$list_var [ $v1 ] = 1;
}

$content = '' ;

foreach ( $list_var as $field_name => $counter )
{
$content .= $field_name . ',' . $counter . "\n" ;
}

file_put_contents ( $file_name , $content , LOCK_EX ) ;
8:31 pm on May 6, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


Thanks.

This produces a warning:

explode() expects parameter 2 to be string, array given in /path/script.php on line 10

Line 10 is this:

$fields = explode ( "," , $rows ) ;

At the end, it does write the variable into the text file, but then keeps overwriting it with the new entry. Therefore, there's only one variable showing at any time.

Thanks
8:37 pm on May 6, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Apr 20, 2017
posts:334
votes: 73


$fields = explode ( "," , $row ) ;

without the "s" at the end of "rows".
8:56 pm on May 6, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2241
votes: 8


First testing shows positive results. I'll just wait to see more from natural traffic, but it looks right.

Thank you very much!