Jump to content

While loop and file()


andybee

Recommended Posts

Hi,

 

I am trying to get info from a several websites (10 web sites), by loading the websites into a variable by using the method file().

It works fine to load one site into the variable, but when I am trying to load the web sites by using a while loop, I get a warning which says:

 

"function.file]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in"

 

My php code with while loop look like this:

 

<?php

 

$siteCode=1;

while($siteCode<10){

$lines = file("http://www.page.com/site=".$siteCode);

 

  $line1=$lines[1];

  $line2=$lines[2];

 

  echo $line1;

  echo $line2;

 

$siteCount++;

}

?>

 

If I am not using the while loop it works fine, but I can not get info from all the websites. Without while loop the php code look like this:

 

<?php

 

$siteCode=1;

 

$lines = file("http://www.page.com/site=".$siteCode);

 

  $line1=$lines[1];

  $line2=$lines[2];

 

  echo $line1;

  echo $line2;

 

Do any of you know how I can solve this problem!

 

Thank you all! :)

 

Andybee.

Link to comment
https://forums.phpfreaks.com/topic/42482-while-loop-and-file/
Share on other sites

Hi,

 

I think one of your sites is not working or has some other problems.

You can try the sites one by one using:

$siteCode=1;//2,3,4,...

$lines = file("http://www.page.com/site=".$siteCode);

   $line1=$lines[1];
   $line2=$lines[2];

  echo $line1;
  echo $line2;

And to see what site gives the error and than try to repair it.

 

Dymon

Link to comment
https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206133
Share on other sites

Hi,

 

dymon: www.page.com/site=1

          www.page.com/site=2

                  ....

          www.page.com/site=10

 

do not exist. I used them only to illustrate that I want the counter in the loop to select the websites.

 

Matt: When I tryed your code, I got following message:

Warning: file(www.google.com) [function.file]: failed to open stream: No such file or directory in file.php

 

Can someone please help me with this problem!

 

AndyBee. :)

Link to comment
https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206223
Share on other sites

this is your while loop:

<?php

$siteCode=1;
while($siteCode<10){
$lines = file("http://www.page.com/site=".$siteCode);

   $line1=$lines[1];
   $line2=$lines[2];

  echo $line1;
  echo $line2;

$siteCount++;
}
?>

 

this is what it should look like:

<?php
        $siteCode=1;
        while($siteCode < 10){
                $lines = file("http://www.page.com/site=".$siteCode);
                $line1=$lines[1];
                $line2=$lines[2];

                echo $line1;
                echo $line2;

                $siteCode++;
        }
?>

 

or you could do it the way i'd do it:

<?php
        for($i = 1; $i < 10; $i++){
                $lines = file("http://www.page.com/site=".$siteCode);
                echo $lines[1] ."<br />\n";
                echo $lines[2] ."<br />\n";
        }
?>

Link to comment
https://forums.phpfreaks.com/topic/42482-while-loop-and-file/#findComment-206286
Share on other sites

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.