|
-
help refining php to txt to flash
Hi all
I've just spent a couple of days looking for tutorials but i've found I'm really quite PHP illiterate so any help here would be awesome.
I have created a flash game that will later be used for a competition so i need to capture the usual user variables such as Name, Email, Score, Var1, Var2.
I have no problem sending the variables out of flash using this script
stop();
var myVars:LoadVars = new LoadVars();
myVars.onLoad = function() {
if (myVars.verify == "success") {
status_txt.text = "data Saved";
} else {
status_txt.text = "Failed Save";
}
};
submit_btn.onRelease = function() {
if (!email_txt.length || email_txt.indexOf("@") == -1 || email_txt.indexOf(".") == -1) {
status_txt.text = "Invalid Email.";
} else if (!name_txt.length) {
status_txt.text = "Missing Name";
} else {
myVars.userName = name_txt.text;
myVars.userEmail = email_txt.text;
myVars.userScore = score_txt.text;
myVars.userCity = City_txt.text;
myVars.sendAndLoad("info.php, myVars, "POST");
}
};
the php script i need help with though here it is.
<?php
$userName = $_POST['userName'];
$userEmail = $_POST['userEmail'];
$userScore = $_POST['userScore'];
$userCity = $_POST['userCity'];
$add = $userName . "<" . $userEmail . ">;";;
$open = fopen('users.txt', 'a');
$write = fwrite($open, $add);
if($write) {
echo "&verify=success&";
} else {
echo "&verify=fail&";
}
?>
obviously first off the only fields that will be writen to txt will be the first two, as I haven't included the score and city variables. This is because i am not sure of the syntax, so some help here would be great.
Secondly the txt file that is writen will show as folows.
john<[email protected]>;bob<[email protected]>;
I imagine with the correct syntax it will read like this
john<[email protected]><5645><Joburg>;bob<bdid@gmail. com><2343><Cape Town>;
I want to create a flash page with all the collected user details but in this format it is difficult for me to to load all the variables and create text boxes that will display the relavent content.
The only solution i could think of was to have the php script to number each profile ie
1User = john&
1Email = [email protected]&
1Score = 5645&
1City = Joburg&
2User = bob&
2Email = [email protected]&
2Score = 2343&
2City = Cape Town&
and so on.
But i have no idea how to modify the php to write consecutive lables like this. I'm not even sure if this makes much sence as a solution.
So any ideas or thoughts are very welcome. I'm almost completely bald in the one section of my head now.
J
Last edited by Beef Cake; 02-06-2008 at 07:42 PM.
-
Hi,
the idea of using
1User = john&
1Email = [email protected]&
is great. You would need to reverse things, however:
User1=john&[email protected]&
One way to do that would be to write all data for a user into one line, and have php read the existing file (rather than just append to it) to find the last index used:
Code:
<?
$nextnum = 0;
$fp = fopen("users.txt","r+");
flock($fp, LOCK_EX);
while(!feof($fp))
{ $line = fgets($fp);
if(ereg("User([0-9]+)", $line, $matched)) $nextnum = $matched[1]+1;
}
fseek($fp, 0, SEEK_END);
fputs($fp,"User$nextnum=$username&Score$nextnum=$userscore&....&\n");
flock($fp, LOCK_UN);
fclose($fp);
If your server supports a database, you could use a database and a php script to return data from the database. If not, you could still write to a text file with just tab or semicolon-separated fields and read that back through a php script that counts entries (or wraps them up in xml, if you prefer). In either case, you would be reading from php rather than a text file and thus avoid caching issues
Musicman
-
Thanks a million Man,
that worked perfectly this is what my final script looked like.
$nextnum = 0;
$fp = fopen("users.txt","r+");
flock($fp, LOCK_EX);
while(!feof($fp))
{ $line = fgets($fp);
if(ereg("User([0-9]+)", $line, $matched)) $nextnum = $matched[1]+1;
}
fseek($fp, 0, SEEK_END);
fputs($fp,"User$nextnum=$userName&Score$nextnum=$u serScore&Email$nextnum=$userEmail&City$nextnum=$us erCity&Sex$nextnum=$userSex&News$nextnum=$userNews &Question1_$nextnum=$userQone&Question2_$nextnum=$ userQtwo&Question3_$nextnum=$userQthree&Question4_ $nextnum=$userQfour&Question5_$nextnum=$userQfive& Question6_$nextnum=$userQsix&Question7_$nextnum=$u serQseven&Question8_$nextnum=$userQeight&\n");
flock($fp, LOCK_UN);
fclose($fp);
two more things if i may - just from an asthetic point of view, how would you make a line break between the users own variables and between the different user sets. I thought the \n would take care of that - but when i think of it i really don't know what that means anyway haha
also the php code for the success or failure of the proceedure no longer works.
if($write) {
echo "&verify=success&";
} else {
echo "&verify=fail&";
}
any suggestions - Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|