PDA

View Full Version : Echo multiple from foreach, only echos last result



motormichael12
07-27-2008, 03:29 AM
I am using this code right here:

<?php
@$tribes = gzfile('http://en7.tribalwars.net/map/ally.txt.gz');
if(!is_array($tribes)) die("Tribe file could not be opened");


$list = $_POST['list'];
$list = trim(str_replace("\tterminate", "", $list));
$list = explode("\n", $list);
$hmmm = array();

/*/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//for($i=0;$i<count($list);$i++)
//{
foreach($tribes as $each_tribe) {

list($id, $name, $tag, $members, $villages, $points, $all_points, $rank) = explode(',', $each_tribe);
$name = urldecode($name);

foreach($each_tribe as $each_tribe2){
echo $each_tribe2;
//$name = addslashes($name);
if($tag == $each_tribe2){
echo $hmmm . $id . "<br>" . $name . "<br>" . $tag . "<br>" . $members . "<br>" . $villages . "<br>" . $points . "<br>" . $all_points . "<br>" . $rank . "<br><br>";

}

}
}
*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
foreach ($list as $tempone) {
foreach ($tribes as $each_tribe) {
list($id, $name, $tag, $members, $villages, $points, $all_points, $rank) = explode(',', $each_tribe);

//echo "$tempone: $tag", "<br>";
if($tag == $tempone){
echo $id . "<br>" . $name . "<br>" . $tag . "<br>" . $members . "<br>" . $villages . "<br>" . $points . "<br>" . $all_points . "<br>" . $rank . "<br><br>";

}

}
echo "\n";
}
*/


for($a=0;$a<count($tribes);$a++){
list($id, $name, $tag, $members, $villages, $points, $all_points, $rank) = explode(',', $tribes[$a]);

for($i=0;$i<count($list);$i++){
if($tag == $list[$i]){
echo $id . "<br>" . $name . "<br>" . $tag . "<br>" . $members . "<br>" . $villages . "<br>" . $points . "<br>" . $all_points . "<br>" . $rank . "<br><br>";

}
}
}





echo "<BR><BR><BR>";
?>

<form name="input" action="t.php"
method="post">
<textarea ROWS=30 COLS=30 NAME="list"><?php for($i=0;$i<count($list);$i++){echo $list[$i]."\n";} ?></textarea>
<input type="submit" value="Submit">
</form>
The gzip file (http://en7.tribalwars.net/map/ally.txt.gz) is also located at http://en7.tribalwars.net/map/ally.txt, and it is a small file. It can be loaded very quickly if anyone wants to look at it.

I know this isn't exactly a clean code but at the moment I can't get it to work. All the commented foreach/for loops are previous attempts. The gzfile looks like this:

14006,Gladiator,Glad,18,0,0,0,413
15048,Retaliation,RET,1,2,570,570,301
45,Pure+Renegade+Soldiers+Academy,PRS-A,116,518,1695403,2477457,60
18432,Car+Stefan+Dusan+Silni,CSDS,20,355,2895261,2895261,49
150,-LTU-,-LTU-,1,11,11926,11926,153
36766,meklar+beta,mek+b,5,5,3198,3198,197
36699,REF+fanclub,REF+0,1,1,895,895,279
27818,Alliance+of+the+Knight,AOTK,1,1,923,923,276
16441,MASTER+HEROES,MH,31,38,68242,68242,101
8745,Knights+of+Oblivion,%3DKO%3D,1,1,422,422,317

I have tried entering the following in the textbox (at the same time, it is one entered data set):

Meklar
Tribal Fluffy Bunnies
T.F.B.
T.F.B.
T.F.B. is the tag for Tribal Fluffy Bunnies, and Meklar's name and tag are both Meklar. All data entered exists, but it only prints the data for the last result. Therefore, in this example it would print hte data for T.F.B., like this.

15358
Tribal+Fluffy+Bunnies
T.F.B.
54
10372
98521330
105316064
1

However, here is anotehr data/result set:

Meklar
Tribal Fluffy Bunnies
T.F.B.
T.F.B.
Meklar



6425
Meklar
Meklar
91
832
5257190
5670780
38

I need it to print the data for each match. How do I do this?

Any help is greatly appreciated, and sorry for so much to read through but I try to be detailed.

Jesdisciple
07-27-2008, 05:26 AM
What is this for, other than to confuse me?
if($tag == $list[$i])Put this directly before that and report the output.
echo "{$tag} == {$list[$i]}";

motormichael12
07-27-2008, 12:28 PM
That is to find out if the $tag from the list of tribes matches the current line for entered data.

I chagned it and entered this:

Meklar
T.F.B.
A small postion of what it returns is this:

Glad == Meklar 14006
Gladiator
Glad
18
0
0
0
397

Glad == T.F.B.14006
Gladiator
Glad
18
0
0
0
397

RET == Meklar 15048
Retaliation
RET
1
2
570
570
303

RET == T.F.B.15048
Retaliation
RET
1
2
570
570
303
The * == Meklar or * == T.F.B. is what decides if it is the right one, so looking at this it hsould find each match correctly. I know it is because if I rearrange them it is fine. It just won't echo any other than the first one.

The number after each *==* set is the rank, echoed in the next line.

Jesdisciple
07-27-2008, 01:57 PM
The * == Meklar or * == T.F.B. is what decides if it is the right one, so looking at this it hsould find each match correctly.I cannot see how you arrive at that conclusion; all comparisons you listed will evaluate to false. Are any correct matches listed there?

It sounds like all matches are correct - so why do you need that if-statement?

motormichael12
07-28-2008, 01:45 AM
Because I only want it to echo if the tag is one of the entered lines.

I am thinking about using the code supplied by the game on every page load, and then using a DB WHERE query to get the data:


A simple example in PHP to write the villages and their names into a MySQL-database:

$lines = gzfile('http://ds1.die-staemme.de/map/village.txt.gz', 'r');
if(!is_array($lines)) die("File could not be opened");
foreach($lines as $line) {
list($id, $name,$x, $y, $player, $points, $rank) = explode(',', $line);
$name = urldecode($name);

$name = addslashes($name);
mysql_query("INSERT INTO village SET id='$id', name='$name', x='$x', y='$y',
player='$player', points='$points', rank='$rank'");
}

Jesdisciple
07-28-2008, 02:24 AM
Putting your data in a database won't fix this problem; if you do it, do it for another reason.

Your problem seems to be flawed input. If you wanted to print every row in your gzfile, you could use this input:
Glad
RET
PRS
CSDS
-LTU-
mek+b
REF+0
AOTK
MH
%3DKO%3D(BTW, somebody messed up with that last one; that's a URL-safe version of =KO= but URL-safety is only appropriate... in URLs.) You can reduce that to only output the lines you're interested in.

motormichael12
07-28-2008, 02:29 AM
I don't want to out put every row, only the ones with a tag matching entered data.

Jesdisciple
07-28-2008, 02:35 AM
You can reduce that to only output the lines you're interested in.So if you don't want lines tagged PRS, remove that tag.

motormichael12
07-28-2008, 02:37 AM
I can't change anything oin the gzfile. It is for a game that I have absolutely no access to other than playing. All my information comes from here:
http://en7.tribalwars.net/help2.php?article=map_data