Jump to content

Foreach loop! Seriously stuck here, help!


hauni

Recommended Posts

Look at the code below.

The code checks if a name is in a list; if true, the name should be green and say "name is offline", if false it SHOULD say "name is offline" in a red colored font. However, everything is turning out green and online, no matter what I try. I don't understand this, although how simple it is. I believe it's something wrong with the foreach. Mind to take a look at this?

 

Thank you!

 

<?php

$fp1 = fopen("http://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=Society+of+the+Black+Rose", "r");
while (feof($fp1) != 1)
$text= $text.stripslashes(fgets($fp1,10000)); str_replace("+", " ", $text);
$expl = explode("Branch", $text);
$expl2 = explode("http://www.tibia.com/abouttibia/?subtopic=aboutcipsoft", $expl[1]);

//echo '<pre>'.htmlspecialchars($output).'</pre><hr/>';
preg_match_all('#<a[^>]*>(.+?)</a>#i', $expl2[0], $matched);

$fp = fopen("http://www.tibia.com/community/?subtopic=whoisonline&world=Antica","r");
while (feof($fp) != 1)
$text= $text.stripslashes(fgets($fp,10000)); str_replace("+", " ", $text);

foreach ($matched[1] as $value){
if (strpos($text, $value) > 1) {

$online .= $value. " <b><font color='green'>is online</font></b> <br>"; 
}
else 
{

$offline .= $value. " <b><font color='red'>is offline</font></b> <br>";
}
}    
echo  $online . $offline;

fclose($fp);
?>

 

Link to comment
https://forums.phpfreaks.com/topic/135548-foreach-loop-seriously-stuck-here-help/
Share on other sites

Alrighty, I think I found your problem.  From what I was able to decipher, the problem lies not within your foreach loop, but instead with the data you are storing.  When you call your first data list, the

 

 

You store it as text, so all your guildies names are in the variable text.

 

You then call the page that checks whos online.  But heres the problem.  You ALSO store that in text, so when you are searching for it, it finds it because you stored it in your first call to text. 

 

All you should have to do is change your second call to text near here...

 

$fp = fopen("http://www.tibia.com/community/?subtopic=whoisonline&world=Antica","r");

while (feof($fp) != 1)

$text= $text.stripslashes(fgets($fp,10000)); str_replace("+", " ", $text);

 

and change it to another variable, for example, text2.

 

Here's some code that shows it.

<?php

$fp1 = fopen("http://www.tibia.com/community/?subtopic=guilds&page=view&GuildName=Society+of+the+Black+Rose", "r");
while (feof($fp1) != 1)
$text= $text.stripslashes(fgets($fp1,10000)); str_replace("+", " ", $text);
$expl = explode("Branch", $text);
$expl2 = explode("http://www.tibia.com/abouttibia/?subtopic=aboutcipsoft", $expl[1]);

preg_match_all('#<a[^>]*>(.+?)</a>#i', $expl2[0], $matched);

$fp = fopen("http://www.tibia.com/community/?subtopic=whoisonline&world=Antica","r");
while (feof($fp) != 1)
$text2= $text2.stripslashes(fgets($fp,10000)); str_replace("+", " ", $text2);

foreach ($matched[1] as $value){
if (strpos($text2, $value) > 1) {
$online .= $value. " <b><font color='green'>is online</font></b> <br>"; 
}
else 
{
$offline .= $value. " <b><font color='red'>is offline</font></b> <br>";
}
}    
echo  $online . $offline;

fclose($fp);
?>

 

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.