/    Sign up×
Community /Pin to ProfileBookmark

List of players

Hi,
Can somebody please tell me what’s wrong with this code?
thebest.txt starts with 0!!!0!!!0!!!0!!! and then should grow with each new player. The get data comes through ajax and arrives ok, i’ve checked. but in the end the ‘thebest.txt’ file is empty -> nothing at all!

Thanks

<?php
$thename = $_GET[‘thename’];
$theschool = $_
GET[‘theschool’];
$thegrade = $_GET[‘thegrade’];
$thesec = $_
GET[‘thesec’];
$thelinenr = $_GET[‘thelinenr’];
$file = ‘thebest.txt’;
$fh = fopen($file, ‘r’) or die(‘Could not open file!’);
$data = fread($fh, filesize($file)) or die(‘Could not read file!’);
fclose($fh);
$signPos=0;
if ($thelinenr>0){
for($counter=0;$counter<$thelinenr+1;$counter++){
$signPos=strpos($data,”!!!”,$signPos+1);
}
}
$cleanedstr2=substr_replace($data,””,0,$signPos);
$temp=$thename.”!!!”. $theschool.”!!!”. $thesec.”!!!”. $thegrade.”!!!”;
$cleanedstr1=substr_replace($data,””,-strlen($data)+$signPos,strlen($data)-$signPos);
$fh = fopen($file, ‘w’) or die(‘Could not open file!’);
fwrite($fh,$cleanedstr1) or die(‘Could not write file!’);
fclose($fh);
$fh = fopen($file, ‘a’) or die(‘Could not open file!’);
fwrite($fh,$temp) or die(‘Could not write file!’);
fwrite($fh,$cleanedstr2) or die(‘Could not write file!’);
fclose($fh);
?>

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@coppocksDec 25.2006 — I'm not sure what you're trying to achieve. I edited the code so I could see...
<i>
</i>&lt;?php
$thename = 'thename';//$_GET['thename'];
$theschool = 'theschool';//$_GET['theschool'];
$thegrade = 'theschool';//$_GET['thegrade'];
$thesec = 'thesec';//$_GET['thesec'];
$thelinenr = 1;//$_GET['thelinenr'];
$file = 'thebest.txt';
$fh = fopen($file, 'r') or die('Could not open file!');
$data = fread($fh, filesize($file)) or die('Could not read file!');
fclose($fh);
$signPos=0;
if ($thelinenr&gt;0){
for($counter=0;$counter&lt;$thelinenr+1;$counter++){
$signPos=strpos($data,"!!!",$signPos+1);
}
}
echo 'DATA: ' . $data . '&lt;br&gt;';
$cleanedstr2=substr_replace($data,"",0,$signPos);
echo 'cleanedstr2: ' . $cleanedstr2 . '&lt;br&gt;';
$temp=$thename."!!!". $theschool."!!!". $thesec."!!!". $thegrade."!!!";
echo 'temp: ' . $temp . '&lt;br&gt;';
$cleanedstr1=substr_replace($data,"",-strlen($data)+$signPos,strlen($data)-$signPos);
echo 'cleanedstr1: ' . $cleanedstr1 . '&lt;br&gt;';
exit;
$fh = fopen($file, 'w') or die('Could not open file!');
fwrite($fh,$cleanedstr1) or die('Could not write file!');
fclose($fh);

$fh = fopen($file, 'a') or die('Could not open file!');
fwrite($fh,$temp) or die('Could not write file!');
fwrite($fh,$cleanedstr2) or die('Could not write file!');
fclose($fh);
?&gt;

Based on the above edits... here is what I see before the write code takes place:

DATA: 0!!!0!!!0!!!0!!!

cleanedstr2: !!!0!!!

temp: thename!!!theschool!!!thesec!!!theschool!!!

cleanedstr1: 0!!!0!!!0

What, in your mind, do you want to see? Tell me that, and maybe I can fix it.
Copy linkTweet thisAlerts:
@manyamileauthorDec 25.2006 — In other words I want to insert a new string in a txt file.

The text file is placed in a table with 4 cells on each row. This new string will be placed on a row. If this row is 5 then 'thelinenr'=20 cells up to there (ok it has a badly chosen name). Each new entry(4 of them) in this table which is built in Javascript (because I know js) is communicated to Php along with the 'thelinenr' information.

Php reads the information stored in the txt file up till now, and if 'thelinenr'=20 then counts 20 '!!!'. Then it creates 3 strings: One with what must be placed in front of the new information cleanedstr1, temp (that contains the new information), and whatever comes after the new information cleanedstr2. And that's about it... Thanks
Copy linkTweet thisAlerts:
@coppocksDec 25.2006 — I guess what I should have said... Show me an example of how you want the textfile to look with the data...

You want the data to appear as one continuous string like this:

thename!!!theschool!!!thesec!!!thegrade!!!thename2!!!theschool2!!!thesec2!!!thegrade2!!!

or one line after the other like this:

thename!!!theschool!!!thesec!!!thegrade!!!

thename2!!!theschool2!!!thesec2!!!thegrade2!!!

thename3!!!theschool3!!!thesec3!!!thegrade3!!!

I just need to see what you're expecting the text file to look like after more than one or 2 entries. After that... piece of cake. Just show me an example.
Copy linkTweet thisAlerts:
@manyamileauthorDec 25.2006 — .....thename!!!theschool!!!thesec!!!thegrade!!!thename2!!!theschool2!!!thesec2!!!thegrade2!!! .......
Copy linkTweet thisAlerts:
@coppocksDec 25.2006 — If that's all you're trying to do... then you have a lot of code for naught. Just do this:

Start with a completely blank text file... and just append to it.

<i>
</i>&lt;?
$thename = 'thename';//$_GET['thename'];
$theschool = 'theschool';//$_GET['theschool'];
$thegrade = 'thegrade';//$_GET['thegrade'];
$thesec = 'thesec';//$_GET['thesec'];
$thelinenr = 1;//$_GET['thelinenr'];
$file = 'thebest.txt';


$temp=$thename."!!!". $theschool."!!!". $thesec."!!!". $thegrade."!!!";

$fh = fopen($file, 'a') or die('Could not open file!');
fwrite($fh,$temp) or die('Could not write file!');
fclose($fh);

?&gt;

The above will give you one continuous string of data after the other.

thename!!!theschool!!!thesec!!!thegrade!!!thename!!!theschool!!!thesec!!!thegrade!!!thename!!!theschool!!!thesec!!!thegrade!!!

That what you want?
Copy linkTweet thisAlerts:
@manyamileauthorDec 25.2006 — No....

I want to insert a string in a specified location (after a given number of !!!)

not append a string (that's why I began and ended with three dots ...).

And I realize it's easy...I just don't what I am doing wrong.

First I used strPos() to find how many characters I have to skip, then I split the text (using substr_replace) in 2 parts then I reassembled the 3 strings.
Copy linkTweet thisAlerts:
@coppocksDec 25.2006 — $thelinenr is always going to be in increments of 4? ie, 4, 8, 12, etc?
Copy linkTweet thisAlerts:
@manyamileauthorDec 25.2006 — Yes.

The 4 variables received.
Copy linkTweet thisAlerts:
@coppocksDec 25.2006 — It would be easier if you separated each record, and did your insertions:

For example, separate each record with a '%%%', and use the '!!!' as a delimiter.

Then insertion becomes easier.

Consider this example:

if your data file looks like this (3 records = 0,1,2):

thename!!!theschool!!!thesec!!!thegrade%%%thename2!!!theschool2!!!thesec2!!!thegrade2%%%thename3!!!theschool3!!!thesec3!!!thegrade3%%%

And we want to insert a new record after the first one:

<i>
</i>&lt;?
$thename = 'thename4';//$_GET['thename'];
$theschool = 'theschool4';//$_GET['theschool'];
$thegrade = 'thegrade4';//$_GET['thegrade'];
$thesec = 'thesec4';//$_GET['thesec'];
$thelinenr = 4;//$_GET['thelinenr'];
$file = 'thebest.txt';

$fh = fopen($file, 'r') or die('Could not open file!');
$data = fread($fh, filesize($file)) or die('Could not read file!');
fclose($fh);

$temp=$thename."!!!". $theschool."!!!". $thesec."!!!". $thegrade."%%%";

$tmpArray = explode('%%%',$data);
$newPos = ($thelinenr / 4);

$newStr = '';

for($i = 0; $i&lt;count($tmpArray); $i++) {
if($i == $newPos - 1) {
$newStr .= $tmpArray[$i]. '%%%' . $temp;
}
elseif ($i == count($tmpArray) - 1) {
$newStr .= $tmpArray[$i];
}
else {
$newStr .= $tmpArray[$i] . '%%%';
}
}

echo $newStr;


Then the $newStr would be:

thename!!!theschool!!!thesec!!!thegrade%%%thename4!!!theschool4!!!thesec4!!!thegrade4%%%thename2!!!theschool2!!!thesec2!!!thegrade2%%%thename3!!!theschool3!!!thesec3!!!thegrade3%%%

Then rather than append to the file, just overwrite the existing data with the new string.

Wouldn't this be easier?
Copy linkTweet thisAlerts:
@manyamileauthorDec 25.2006 — I think that's interesting, and clever.

Thank you.
×

Success!

Help @manyamile spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...