Jump to content

Pulling in a line of text (I get an error every 3rd use)


jimmyhumbled

Recommended Posts

Hello - this is my first post here but after looking round it looks like a handy place to know about.

 

I am new to PHP and have been struggling with a script. The working idea is to pull in at random a link from a plain text file and show it on my website. The code is as follows:

 

    <?php

    $adfile = "url.txt";

    $ads = array();

   

    $fh = fopen($adfile, "r");

    while(!feof($fh)) {

        $line = fgets($fh, 10240);

        $line = trim($line);

        if($line != "") {

            $ads[] = $line;

        }

    }

 

    $num = count($ads);

    $idx = rand(0, $num-1);

    echo $ads[$idx];

?>

 

 

The text file is set out with a new link on each line as follows

 

<a href="www.link1.com">The text link</a>

<a href="www.link2.com">The text link</a>

etc for 10 lines

 

Everything seems to work but about 1/2 the the link is not pulled in at all and the script makes the next line down on the page a link that doesnt go anywhere, I wondered if anyone would have time to have a scan of the above and let me know if they could see what might be wrong,

 

Thanks

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.