Jump to content

Save text file with number format


Recommended Posts

Hi All, I am not quite sure if this is possible - but here goes. I have a webpage hit counter that uses PHP and saves the counter totals in .txt files. I have gotten the code to print out the comma (thousand separator) fine on the page, however, I would like to have that comma in the text file also. I display the counters on separate pages using <?php include("file.txt");?> and the counter shows up as "12345" when I'd like it to read the text as "12,345". Basically, I just want that comma written to the actual text file if possible so that when it's included on a web page, it displays the thousands separator. Any help would be great. Thank You!

 

Here is a portion of my code:

/* Turn error notices off */
error_reporting(E_ALL ^ E_NOTICE);

/* Get page and log file names */
$page = input($_GET['page']) or die('ERROR: Missing page ID');
$logfile = 'logs/' . $page . '.txt';

/* Does the log exist? */
if (file_exists($logfile))
{
/* Get current count */
$count = intval(trim(file_get_contents($logfile))) or $count = 0;
$cname = 'tcount_unique_'.$page;

if ($count_unique==0 || !isset($_COOKIE[$cname]))
    {
	/* Increase the count by 1 */
	$count = $count + 1;
	$fp = @fopen($logfile,'w+') or die('ERROR: Can\'t write to the log file ('.$logfile.'), please make sure this file exists and is CHMOD to 666 (rw-rw-rw-)!');
	flock($fp, LOCK_EX);
	fputs($fp, $count);
	flock($fp, LOCK_UN);
	fclose($fp);

	/* Print the Cookie and P3P compact privacy policy */
	header('P3P: CP="NOI NID"');
	setcookie($cname, 1, time()+60*60*$unique_hours);
}

/* Is zero-padding enabled? */
    if ($min_digits > 0)
    {
    $count = sprintf('%0'.$min_digits.'s',$count);
    }

    /* Print out Javascript code and exit */
    echo "document.write('".number_format($count)."');";
    exit();
}
else
{
    die('ERROR: Invalid log file!');
}

Link to comment
Share on other sites

What exactly is the problem?

 

I would like the thousands separator to be in the actual .txt file, so that if I display the counter on another page, it shows the comma. I cannot seem to get it to write the thousands separator the actual text file.

Link to comment
Share on other sites

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.