|
-
Jul 24th, 2005, 08:50 PM
#1
Thread Starter
Admodistrator
Add to string
Okay, in vb we use
Str = Str & Newval
But if i use that in php, it duplicates the document and adds it repeatedly. I hope someone understands & thanks
-
Jul 24th, 2005, 09:28 PM
#2
Member
Re: Add to string
I'm not sure, but try this:
$Str .= $Newval, wich is a shorter $str = $str.$Newval
str and newval are variables, right?
... and the rivers shall open for the righteous, and the rivers shall open for the righteous, and rivers shall open for the righteous, someday ....
-
Jul 24th, 2005, 09:37 PM
#3
Re: Add to string
 Originally Posted by |2eM!x
Okay, in vb we use
Str = Str & Newval
But if i use that in php, it duplicates the document and adds it repeatedly. I hope someone understands & thanks
you mean... the one in php acts differently than the one in asp? hmmm.
-
Jul 24th, 2005, 10:36 PM
#4
Thread Starter
Admodistrator
Re: Add to string
Turns out somethings wrong & i dont think its that. Here is my Code:
PHP Code:
<html>
<form>
<body>
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$filename = "IPTimes.txt";
$handle = fopen($filename, "r+") or die("Err1");
$contents = @fread($handle,filesize($filename));
If(Strpos($contents,$ip)===False){
echo "New user..Validating..";
fwrite($handle,$contents."\r\n".$ip.":1");
$NumVisits = 1;
}Else{
$entry = explode("\r\n",$contents);
$Number = 0;
foreach($entry as $x){
If(strpos($entry[$Number],$ip)===0){
$Number2 = explode(":",$entry[$Number]);
$Number2['1']++;
$NumVisits = $Number2['1'];
$entry[$Number]="$ip:$NumVisits";
}
$Str = $Str.$entry[$Number]."\r\n";
Echo $Str."\r\n";
$Number++;
}
}
//echo $NumVisits;
fwrite($handle,$Str);
fclose($handle);
?>
</body>
</form>
</html>
Now, NumVisits is returning the right number - which is perfect. But when writing, $Str takes on values it shouldnt. For example, in "IPTimes.txt", i have my ip (127.0.0.1:1) followed by times ive visited(:1). But after running the file once, it will return
127.0.0.1:1127.0.0.1:2
instead of overwriting that number, it just adds to it?? i dont think the /r/n is working. But sometimes it does add enters?? i got no idea whats wrong
-
Jul 24th, 2005, 10:45 PM
#5
Re: Add to string
perhaps its.. '\r\n'
that's what I use to go new line..
but I remember visualAd saying about a function getting the newline of certain pc's.
-
Jul 24th, 2005, 11:23 PM
#6
Thread Starter
Admodistrator
Re: Add to string
what should i use instead?
-
Jul 24th, 2005, 11:36 PM
#7
Re: Add to string
Hmmm.... we'd have to wait for visualAd for that.. I was looking through the dox. and I can't seem to find it. ...
don't you want to just write it... line per line?
-
Jul 24th, 2005, 11:40 PM
#8
Thread Starter
Admodistrator
Re: Add to string
not really..but i know i could use fwrite in the loop
-
Jul 24th, 2005, 11:41 PM
#9
Re: Add to string
since as usual i don't have anything to do.. I could try it out here.
-
Jul 24th, 2005, 11:48 PM
#10
Re: Add to string
I'm sorry... I read the prob again..
so your problem here is that it is not overwriting but appending right?
-
Jul 24th, 2005, 11:56 PM
#11
Thread Starter
Admodistrator
Re: Add to string
Im not sure what its doing at all. It returns the right values to me with an echo, but when writing its doing something crazy.
I think /r/n isnt valid for writing
-
Jul 25th, 2005, 12:27 AM
#12
Re: Add to string
The problem lies in your loop. You are using a foreach loop with a counter variable. 
If you are using a counter variable then you need to use a for loop:
PHP Code:
for($number = 0; $number < count($entry); $number++)
If(strpos($entry[$Number],$ip) === 0){
$Number2 = explode(":",$entry[$Number]);
$Number2['1']++;
$NumVisits = $Number2['1'];
$entry[$Number]="$ip:$NumVisits";
}
$Str = $Str.$entry[$Number]."\r\n";
Echo $Str."\r\n";
}
Also, the file() function returns an array , one for each line, independant of the plat form - so it is better to use this than manually explode the files contents.
-
Jul 25th, 2005, 01:05 AM
#13
Thread Starter
Admodistrator
Re: Add to string
thanks VAd, but whats the difference? Each time the foreach loop is run it adds one to the counter. It seems like it should do the same thing?
Ill try your code and post results.
-
Jul 25th, 2005, 01:16 AM
#14
Thread Starter
Admodistrator
Re: Add to string
Okay, i dont understand whats wrong at all.
The first time its run, IPTimes.txt has this in it:127.0.0.1:1
Second time:127.0.0.1:1127.0.0.1:2
Third time:
127.0.0.1:1127.0.0.1:2
127.0.0.1:1127.0.0.2
127.0.0.1:1127.0.0.3
I really dont understand this.
-
Jul 25th, 2005, 01:36 AM
#15
-
Jul 25th, 2005, 01:38 AM
#16
Thread Starter
Admodistrator
Re: Add to string
And i dont understand whats wrong! Ive never been so confused. I wish there was a way to walk thru this like other languages
-
Jul 25th, 2005, 02:12 AM
#17
Re: Add to string
Its because you are appending the new line to the original line. If you just want the IP address you need to change this line:
PHP Code:
$entry[$Number]="{$Number2[0]}:$NumVisits";
Which will be the IP portion of the line.
-
Jul 25th, 2005, 02:32 AM
#18
Re: Add to string
DONE!!! lol
Let's just say I got frustrated with the problem and i did edited it. 
also.. remix.. i renamed $x as $ipdata... i guess it got mixed there. 
THIS code is working.. ehehehe.. i'm giving myself a pat in the back. 
PHP Code:
<html>
<form>
<body>
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$filename = "IPTimes.txt";
$handle = fopen($filename, "r+") or die("Err1");
$contents = @fread($handle,filesize($filename));
If($contents===False){
echo "New user..Validating..";
fwrite($handle,$ip.":1"."\r\n");
}Else{
$entry = explode("\r\n",$contents);
$Str = "";
echo "<BR>Entries=".count($entry);
foreach($entry as $ipdata){
if ( trim($ipdata) != ""){
If(strpos(trim($ipdata),$ip)===0){
$ipparts = explode(":",$ipdata);
$ipparts[1]++;
$ipdata = implode(":", $ipparts);
echo "<BR>NEW".$ipdata;
}
$Str = $Str.$ipdata."\r\n";
echo "<BR>".$Str;
}
}
ftruncate($handle, 0);
rewind($handle);
fwrite($handle,trim($Str));
}
fclose($handle);
?>
</body>
</form>
</html>
-
Jul 25th, 2005, 02:35 AM
#19
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
|