PHP into Flash

Hey guys. I got a script for a PHP counter but i want to display it in flash. so on the last line of my PHP script i added:


$variable="variable=$variable";
echo $variable

then in flash i am using


counterText=loadVariables("counter.php");

is that how i would go about doing it? I was talking to David and he helped me this far, but we both wanted to doubble check to make sure thats how you would do it.

Any help is appreciated-
-Mike

PS: Here is the counter script:


<?php
$count_my_page = ("hitcounter.txt");
$hits = file($count_my_page);
$hits[0]++;
$fp = fopen($count_my_page , "w");
fputs($fp , "$hits[0]");
fclose($fp);
echo $hits[0];
?>

This is before adding what i said above btw

if you follow what i posted here it should work :slight_smile:

http://www.kirupaforum.com/forums/showthread.php?s=&threadid=18524&highlight=counter

i knew i saw that thread some where/ when i searched it didnt find it. thaks!

Ahmed: i am using this code in the first frame of my movie:


count=loadVariables("counter.php", 0);

but the thing is that it wont show up in my text box of count. i can provide the FLA if thats what you need, but i am using the code above so i dont htinkits the PHP

use this actionscript:

myTextbox.variable = myCounter
lv = new LoadVars()
lv.onLoad = function(success) {
if(success) {
myCounter = this.myCounter
}
}
lv.load("counter.php");

… that should load the variable ‘myCounter’ and display it in the textbox :slight_smile:

ok ahmed i will try it, thanks! but just a question, shouldnt loadVariables have worked?

the way you were trying to do it, it won’t :slight_smile:

this could’ve worked:

myTextbox.variable = myCounter
loadVariables("counter.php", this)
// when loadVariables is done loading, the variable myCounter in the php script will be
// included in the flash movie, and then loaded into the textbox

:slight_smile:

ahmed, the first code didnt werk. i will try this one.

ahmed, unfortuanatly that doesnt work either. i honestly dont know why, i think i am doing everything right…but i just dont know…if you want here is the fla to the project.

www.mdipi.com/php/miniml.fla

where’s your script located?

ok… you need to have the field’s instance name set as ‘myTextbox’… both methods should work :slight_smile:

doh!
lol thanks a ton! lol how stupidof a mistake was it?

omgosh! still nothing! i odnt get it!

ok let’s see now…


<?

$filename = "TestCounter.txt";

$file = fopen( $filename,"r"); 
$OldCount = fread($file, 25); 
fclose( $file ); 

$OldCount = split ("=", $OldCount, 25);

$NewCount = $OldCount[1] + '1';

$New = "&count=$NewCount";

$file = fopen( $filename,"w+");
if (flock($file, 2)) { 
fwrite($file, $New, 100); }
fclose( $file ); 

print "&count=$NewCount";

?>

if (flock($file, 2)) { <— this line blocks the file or something! I saw it in a tute somewhere! Anyway it keeps the count from resetting when a bunch of ppl visit the site at the same time n’ all.

Are you doing a Live counter or just a counter?

You can see my live one here: <a href=“http://www.vmkdsn.com/sykopage/counter/TestCounter.htm”>Syko’s Live Counter</a>

<b>NB! Be sure to change the permissions of the txt file to 777</b>

Well syko its just that i want it too count when ppl visit lol. i am not to sure what you mean by live. though. but i will change the permission right now…(oops)
edit one:
i changed them and still nothing. i will try your code though
edit two:
tryed syko’s php and still nothing. i has to be with the flash. my code is in the first frame and is as follows:


myTextbox.variable = myCounter
loadVariables("counter.php", this)

check my .fla file here: http://www.vmkdsn.com/sykopage/counter/TestCounter.fla
I have the tetbox named the same as the variable in the txt file! I alwasy have a problem with that!

And by Live Counter I meant that the counter refreshes itself all the time so you don’t have to refrsh the page to refresh it!
[ go http://www.vmkdsn.com/sykopage/counter/TestCounter.htm, opne up a new window and go to the same URL and see it increase! ] ( it will refresh after a period of time)

syko, i tore apart some of the code you gave me and got it to display 1, but it never increments…like with this code in your sample fla


loadVariablesNum("TestCounter.txt?cnum="+random(99999), 0);

that loads the text file just fine, but the php is never executed. lol so where do i put the php in all of this?

i placed the php but still no werk! the fla is attached.

www.mdipi.com/TextCounter.txt
thats the txt file

and the PHP code is exactly as you posted.

wops! Sry for the delay!

I wish I could view that .fla but my hard-drive is all screwqed up right now.
the varname of the textbox is “count” right?

Your going trough the same thing as I did. At first it didn’t show me anything, then it was just “1” and then voila.
Try executing the php directly! ( www.something.sth/yourphp.php ) and see if it gives ya any errors.

And also if you have an action loading the textfile [size=1]loadvariablesnum(“txtfile.txt”);[/size] then delete it or put “//” symbols in front of it and see if it still shows the “1” there.

Not sure if your question is answered or not… but here is a code that I would use for a simple PHP hit counter…

here is a link to the working copy. Hit refresh a couple of times…

http://www.bluefinatlantic.com/egTest/count.php


<?
$file = "count.txt";  //define file

$fp = fopen($file, "a+"); //open file for read and writing

$data = fread($fp, 80000); //read file to retrieve data

ftruncate($fp, 0); //Cuts file length to zero - Erases previous value

$new = $data + 1; //Adds to counter

fwrite($fp, $new); //Writes the new number into the textfile

fclose($fp); //Closes the file

print "count=".$new; //Prints out the variable so Flash can read it.
?>

This only saves a single number to the text file, so there is no need to manipulate strings or anything…

Then your Flash code would be…


loadVariablesNum("http://www.somesite.com/phpFile.php", 0, "POST");

and on your main stage have a textbox that has the variable name of “count” (without the quotes).

if you want a sample file I can make one up…