Log in

View Full Version : Specifiy what tags show what?



lankinator
07-13-2007, 09:58 PM
how do you specify if a tag is equal to say 5 and shows a piece of text, but still show that text until the tag is equal to say 10?

Twey
07-13-2007, 10:33 PM
How can a "tag" be equal to 5?

alexjewell
07-14-2007, 02:33 AM
I think you mean, instead of tag, if a variable is equal to 5?
You might want to try:



for($i = 5; $i <= 10; $i++) { echo 'The variable is between 5 and 10!'; }


But, to be more specific to your situation in the code, can you provide more details?

Twey
07-14-2007, 02:38 AM
What's with that do?

alexjewell
07-14-2007, 02:40 AM
Haha, sorry. I don't know what I was thinking.

Changed.

lankinator
07-14-2007, 07:12 AM
But, to be more specific to your situation in the code, can you provide more details?

ok, say:
$code == "5";

now, when the $code is equal to 5, i want it to print a piece of text, and i don't want that text to disappear until $code is equal to 10?

djr33
07-14-2007, 07:30 AM
What's making it change?

More info.... a lot more info.

The word until seems to barely even apply to PHP. Though it's linear and it you could say "until" it reaches a certain point, that's practically unnoticable, certainly with the text changing. So... do you mean javascript?

lankinator
07-14-2007, 08:53 AM
i have this code: everytime someone visit the page it alters the counter number. now, when the $code is equal to 5, i want it to print a piece of text, and i don't want that text to disappear until $code is equal to 10?



//set the count file
$counterfile = "hits/" .$me. "_count.dat";

//check the file is readable
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");

//grab the value and increase by 1
$count = (int) fread($fp, 20);
fclose($fp);
$count++;

//show the result, write the new value and close the file
echo "document.write('$count');";
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
fclose($fp);

Twey
07-14-2007, 11:28 AM
if($code > 4 && $code < 10)
print "a piece of text";

mwinter
07-16-2007, 05:19 PM
//set the count file
$counterfile = "hits/" .$me. "_count.dat";

//check the file is readable
if(!($fp = fopen($counterfile,"r"))) die ("cannot open counter file");

//grab the value and increase by 1
$count = (int) fread($fp, 20);
fclose($fp);
$count++;

//show the result, write the new value and close the file
echo "document.write('$count');";
$fp = fopen($counterfile, "w");
fwrite($fp , $count);
fclose($fp);


This is liable to fail when deployed: the file isn't locked, so data can between reading the existing value and writing the new one. The whole process should be atomic (which isn't necessarily easy in PHP):



$counterFilename = "hits/{$me}_count.dat";
$counterFormat = '%u';

if (($counterFile = fopen($counterFilename, 'r+'))) {
if (flock($counterFile, LOCK_EX)) {
// In PHP 4:
// if (!fscanf($counterFile, $counterFormat, &$counter)) {
if (!fscanf($counterFile, $counterFormat, $counter)) {
$counter = 0;
}
// I have no idea why you'd want the indirection of using client-side scripting to write
// the value into the document. Seems pointless to me.
echo ++$counter;
rewind($counterFile);
fprintf($counterFile, $counterFormat, $counter);
flock($counterFile, LOCK_UN);
}
fclose($counterFile);
} else {
// I would suggest something more robust than just quitting here. In fact, once the file was
// created (which you'd have to do manually, at the moment), you'd have to do something very
// wrong to have this opening operation fail.
}

I can't test concurrency properly here as file locks are made at the process level on Windows, and Apache only runs one process under NT-based OSs. However, it should be safer, at least.