Jump to content

[SOLVED] Help with counter/resetter snippet...


TheLoveableMonty

Recommended Posts

I'm pretty new to PHP and was wondering if someone could have a glance at this snippet.

 

## Opens file containing current value

$curloc = "curbil.dat";
$curbil = file($curloc);

## Opens file containing maximum value

$totloc = "totbil.dat";
$totbil = file($totloc);

if ($curbil[0] == $totbil[0]){

$fp = fopen($curloc, "w");
$curbil = "1";
fwrite($fp, $curbil);

} else {

$curbil = $curbil[0] + 1;
$fp = fopen($curloc, "w");
fwrite($fp, $curbil);

}

 

The script opens two files - one with a "current" value and one with a "max" value. The aim is to have the current increase by one every time the page loads, up until the current equals the max value, in which the current value's reset to "1".

 

I printed both values and refreshed the page to test it, but I encountered two problems. Firstly, the max value always prints "array", and when the current number's reset to "1" it also prints array. I've attempted to fix it by declaring the variables as the first subjects of the arrays the files are added to when they're loaded. I can't help but think I've done something wrong though.

 

The snippet, by the way, is to help modify a banner script. Most banner scripts operate using random values - I want this one to work on incriments so each banner will be displayed one after the other.

 

Can anyone help?

<?php
## Opens file containing current value

$curloc = "curbil.dat";
$curbil = file_get_contents($curloc);
list($curbil, $total) = explode(":", $curbil);

$fp = fopen($curloc, "w");
if ($curbil == $totbil){
    $curbil = "1:$totbil";
} else {
    $curbil = ($curbil+1) . ":$totbil";
}

fwrite($fp, $curbil);
flcose($fp);
?>

 

Modified your code, why store it in 2 files vs just 1 file with a splitting parameter (ie the colon). You may have to modify the current file to do that before you invoke this script.

 

Also removed alot of duplicated code and compacted the code.

 

See if that works out for you.

http://www.tenthousandbillboards.com/demo.php

 

The script's not splitting the two values via the character used. I've modified the data file to include both values and the splitting paramater, but all it does is print the current value and parameter and has done nothing with the total value. I'd imagine, because the current total value is 20, that the splitting piece hasn't worked because the value's already exceeded 20.

 

I didn't realise how much I repeated myself until I saw your narrowed script though. I hate trying new things.

 

EDIT: It's not printing the splitting parameter, directly, rather the new one used in the rewrite. Silly me.

 

EDIT 2: I've got it now. Just trying to ensure the value resets to "1" if it slips over.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.