Results 1 to 9 of 9

Thread: Echo multiple from foreach, only echos last result

  1. #1
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default Echo multiple from foreach, only echos last result

    I am using this code right here:
    PHP Code:
    <?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:
    Code:
    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):
    Code:
    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.
    Code:
    15358
    Tribal+Fluffy+Bunnies
    T.F.B.
    54
    10372
    98521330
    105316064
    1
    However, here is anotehr data/result set:
    Code:
    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.

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    What is this for, other than to confuse me?
    Code:
    if($tag == $list[$i])
    Put this directly before that and report the output.
    Code:
    echo "{$tag} == {$list[$i]}";
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    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:
    Code:
    Meklar
    T.F.B.
    A small postion of what it returns is this:
    Code:
    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.

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Quote Originally Posted by motormichael12 View Post
    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?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #5
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    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:
    Code:
    $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'");
    }

  6. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  7. #7
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    I don't want to out put every row, only the ones with a tag matching entered data.

  8. #8
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  9. #9
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •