Forum Moderators: coopster

Message Too Old, No Replies

count entries

         

smallcompany

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

LifeinAsia

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

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



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

Peter_S

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

5+ Year Member Top Contributors Of The Month



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

smallcompany

12:23 am on May 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.

smallcompany

1:01 am on May 6, 2017 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

Peter_S

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

5+ Year Member Top Contributors Of The Month



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

smallcompany

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

Peter_S

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

5+ Year Member Top Contributors Of The Month



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

without the "s" at the end of "rows".

smallcompany

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

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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

Thank you very much!