0

Im new to php, and i really need some help with this script. I have a text file with names(serverlist.txt). The first line of code gets these names. I want it to get one name, put it in to the last bit of code(), Then repeat the process untill there are no lines of text in the serverlist.txt.

Please help me.

    <?php 
$file = 'serverlist.txt';
$content = "";
if($handle = fopen($file, 'r')) {
    while(!feof($handle)) {
        $content .= fgets($handle);
            foreach($content as $cont) {
            }
    }
    fclose($handle);
}



?>

<html>
<head>
</head>
<body>
<img src="http://minecraft.net/skin/<?php 


echo $cont; ?>.png" alt="">

</body>
</html>
  • Then what is the result of your code..any error? no result? something? – Amir Mar 16 '13 at 07:59
  • My code gives out many names inside one img tag, i want 1 name inside many img tags. So i guess the foreach is my error, beacuse i dont know how to use it to "display" on name at the time and then repeat the process. – user2176480 Mar 16 '13 at 08:11

3 Answers3

0
<?
$content = array ();

$fd = fopen ('serverlist.txt', 'r');
while ($line = trim (fgets ($fd)))
{
    $content [] = $line;
}
fclose ($fd);
?>

<html>
  <body>
    <? foreach ($content as $cont) { ?>
      <img src="http://minecraft.net/skin/<?= $cont ?>.png">
    <? } ?>
  </body>
</html>
Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
  • Thank you for taking the time. This does not work. It gives me all the text inside the txt at the same time inside the img tag. I want 1 name for each of the img tags. – user2176480 Mar 16 '13 at 08:16
0

See if this works for you...

<html>
<head>
</head>
<body>
<?php 
    $file = 'serverlist.txt';
    $content = "";

    if($handle = fopen($file, 'r')) {
        while(!feof($handle)) {
            $content .= fgets($handle);
            foreach($content as $cont) {
                echo '<img src="http://minecraft.net/skin/'.$cont.'.png" alt="">';
            }
        }
    fclose($handle);
    }
?>
</body>
</html>

It seems like all you are trying to do, is use $cont to modify an img src.

Mario Wenig
  • 122
  • 4
  • Thank you, but this does not work. I see now how i can use php inside a , but the main problem is that with this result i would get one image with many name inside it, but i want many images with one name inside them. – user2176480 Mar 16 '13 at 08:06
0

You could store the output of the server names in a variable and then output them in your HTML body.

<?php 
  $file = 'serverlist.txt';
  $servers = '';
  if ($handle = fopen($file, 'r')) {
    while (!feof($handle)) {
      $content = trim(fgets($handle));
      $names = explode(' ', $content);
      foreach ($names as $name) {
        $servers .= '<img src="http://minecraft.net/skin/' . $name. '.png" alt="">';
      }
    }
   fclose($handle);
  }
?>

<html>
<head>
</head>
<body>
  <?php echo $servers; ?>
</body>
</html>
Aiias
  • 4,683
  • 1
  • 18
  • 34
  • Thank you for taking the time for this. This does not work, but i like this idea. The problem is that this gives me all the names in the file at the same time inside the image. Now it gives me many names inside 1 image, i want 1 name inside many images. – user2176480 Mar 16 '13 at 08:04
  • @user2176480 - Could you please post an example of how your data file **serverlist.txt** is formatted? It's hard to know how to deal with the data if we don't know how it is formatted. In other words, is it separated by commas? Multiple lines? etc. – Aiias Mar 16 '13 at 08:16
  • Its only separated by a no break space and lookes like this: name1 name2 name3 name4 name5 name6 – user2176480 Mar 16 '13 at 08:18
  • @user2176480 - Thanks for the info :). I have revised my answer to parse the names one at a time. – Aiias Mar 16 '13 at 08:21
  • This still gives out one picture with all the names inside the $name =/ – user2176480 Mar 16 '13 at 08:26
  • If you could take a look @ http://stackoverflow.com/questions/15447913/php-if-while-and-or-img-resizing-error I would be very happy. – user2176480 Mar 16 '13 at 10:01