Closed Thread Icon

Topic awaiting preservation: It never fails! Urgh! (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12232" title="Pages that link to Topic awaiting preservation: It never fails! Urgh! (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: It never fails! Urgh! <span class="small">(Page 1 of 1)</span>\

 
Dark
Neurotic (0) Inmate
Newly admitted
posted posted 05-25-2002 10:33

In PHP when i pull things out of txt file it always like skips are misses something.
Like say I have a PHP script that will randomly pull an img src out of txt file.
It works fine for a couple of trys, but then it shows nothing. It never fails to not pull out and display it.

Do you know what I mean? What causes this?

I thinking hard about just using Javascipt. It never seems to miss like PHP does.

Dark

ShrineMaster
Obsessive-Compulsive (I) Inmate

From: Somewhere in Iowa
Insane since: Feb 2002

posted posted 05-25-2002 11:07

If you have a blank line at the end of the file it may be checking that. If the value is nul you could always stick the random selection in a loop.

while (!$image) {
$image = (Whaever you have to get the value);
}

If you're putting lines from the text file into an array before chosing the image, check to see if the value you're adding to the array is valid before adding it.


Dark
Neurotic (0) Inmate
Newly admitted
posted posted 05-25-2002 11:17

Ok that makes sense. How would I add a loop into this?
I just read up on srand + the rand functions today.
I'm still completly new at this, lol.


<?php

$Img = @file("Scripts/RM01.txt");
$count = count($Img)-1;
srand((double) microtime() * 1000000);
$Pull = rand(0,$count);

echo "<a href='Harmony.php'><img src='$Img[$Pull]' border=0></a>";

?>


Dark

[This message has been edited by Dark (edited 05-25-2002).]

ShrineMaster
Obsessive-Compulsive (I) Inmate

From: Somewhere in Iowa
Insane since: Feb 2002

posted posted 05-26-2002 01:32

Use WHILE() { } http://www.php.net/manual/en/control-structures.while.php

code:
<?php

$Img = @file("Scripts/RM01.txt");
$count = count($Img)-1;
srand((double) microtime() * 1000000);
$loopcount=0;
while(!$Img[$Pull] && $loopcount < 100) {
$Pull = rand(0,$count);
$loopcount++;
}

if ($Img[$Pull]) {
echo "<a href='Harmony.php'><img src='$Img[$Pull]' border=0></a>";
} else {
echo "<a href='Harmony.php'>Harmony</a>";
}
?>



The WHILE loop checks to see if the vaule exists. The $loopcount prevents it from going into an infinite loop.

More info from the FILE() documentation: http://www.php.net/manual/en/function.file.php

"file() has a strange behaviour when reading file with both \n and \r as
line delimitator (DOS files), since it will return an array with every
single line but with just a \n in the end. It seems like \r just
disappears"

If your text file was written using windows which uses both \r and \n at the end of lines, your script is reading it as if every other line is blank.

Hope this helps.

Kriek
Maniac (V) Inmate

From: Florida
Insane since: Jul 2001

posted posted 05-26-2002 02:46

ok now it seems to be missing more often.

Here look at this . http://www.jonkriek.com/Test.php

In the text file there is nothing, but the image src like:

Gallery/Banner_Music.jpg
Gallery/Banner_Music2.jpg
Gallery/Banner_Music3.jpg
Gallery/Banner_Music4.jpg

Is there something that I need to add to the txt file?
Like something that will tell it not to grab the last line?



[This message has been edited by Kriek (edited 05-26-2002).]

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 05-26-2002 02:55

Wierd it looks like $Img[$Pull] is returning true even though it's returning a 'blank' value.

This is kinda a hack but I'm on my way out
You can change this line
while(!$Img[$Pull] && $loopcount < 100) {

To
while(!$Img[$Pull] && $loopcount < 100 && (strlen($Img[$Pull] > 1)) {

I don't think the strlen will error if the val is false



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 05-26-2002 03:35

I'm not sure, but maybe you could test to make sure your not pulling a blank line from the text file.

code:
<?php

$Img = @file("Scripts/RM01.txt");
$count = count($Img)-1;
srand((double) microtime() * 1000000);
$loopcount=0;
while(!$Img[$Pull] && $loopcount < 100) {
if($Img[$Pull] != "") {
$Pull = rand(0,$count);
}
$loopcount++;
}

if ($Img[$Pull]) {
echo "<a href='Harmony.php'><img src='$Img[$Pull]' border=0></a>";
} else {
echo "<a href='Harmony.php'>Harmony</a>";
}



Somebody please jump in and say so if this won't work.

-Butcher-

Kriek
Maniac (V) Inmate

From: Florida
Insane since: Jul 2001

posted posted 05-26-2002 05:14

ShrineMaster rewrote the script and it works beautiful.
Never misses a beat. Thank you so much.

Would you mind explaining what was wrong with the first script?
And/or why it was messing up so often? It was was written on very limited knowledge of php
and a couple of tuts on functions.


<BLOCKQUOTE><FONT face="Verdana, Arial">code:</font><HR><pre>
<?php

$tmp=0;
$fp = fopen ("Scripts/RM01.txt","r");
while ($data = fgetcsv ($fp, 2000, "

« BackwardsOnwards »

Show Forum Drop Down Menu