Search code examples
phparraysfunctionexplodedynamic-arrays

Array/Data Changes


I am developing a clan website: piratesofpopulous.comli.com/ It is a game's clan website, I wanted to get the game history of my clan members and show them on their profile page,You can see it here:

http://piratesofpopulous.comli.com/user.php?username=Waleed

That page is not published for users and I am still working on it. The problem is I used explode method to explode this page: http://www.popre.net/game.php?u=38456 . Then use the arrays where game's were saved i.e $expl[596].

I am using this code:

//Breaking the URL
$url = "$profile_link"; <-- This profile link will be driven from the database, Its saved when user is registered.
parse_str( parse_url( $url, PHP_URL_QUERY), $params);

//After Breaking putting the value of u in function
function gameStats($u)
{
$result = file_get_contents('http://popre.net/game.php?u='. $u . '');
$expl = explode('"',$result);

echo"<br>Level: $expl[580]<br>";
echo"Players: $expl[596]<br><br>";
echo"Result: $expl[584]<br><br>";
echo"Points: $expl[586]<br><br>";
}
gameStats( $params['u']);

I am breaking the url because the users are registered using a url like this i.e: www.popre.net/user.php?u=38456 <-- Link contains user.php?u=someid

But the games are saved at game.php not in user.php i.e: www.popre.net/game.php?u=38456 <-- game.php/u=same_user_id

I broke the url to to get the value of "u" from their profile link and then put it in game.php to get arrays, Then show the selective arrays......

You can view all arrays by:

print_r($expl);

But the problem is that, For few mins examply the explode array $expl[596] is changed to something else containing other data i.e $expl[580], But after few more mins it changes back to $expl[596]. Then the data and games are shown correctly. You can view the page on which I am showing these here: http://piratesofpopulous.comli.com/user.php?username=Waleed I am still working on it.

So, Is there any way to get the perfect array number So the data is permanently shown without showing any other data?

Or any other method that works if the arrays are changed?

Or any thing else?

Any help would be good for me.....

EDIT:

I am doing this to get the Map(game level),Players,Result and Points. But after sometime the explode array i.e:Player $expl[600]; changed to $expl[596],It keeps changing but after sometimes it will get back to $expl[600].Is there anyway to still get data from it while it keeeps changing?

This is my code that I am using:

            $url = "$profile_link"; <-- You can use this profile link http://popre.net/user.php?u=38456
            parse_str( parse_url( $url, PHP_URL_QUERY), $params);
            function gameStats($u)
            {
            $result = file_get_contents('http://popre.net/game.php?u='. $u . '');
            $expl = explode('"',$result);

            echo"<div id='games'>";
            echo"<h3>Game 1</h3>";
            echo"<div class='data'>Level:    $expl[584]</div><br>";
            echo"<div class='data'>Player:   $expl[600]</div><br><br>";
            echo"<div class='data'>Result:   $expl[588]</div><br><br>";
            echo"<div class='data'>Points:   $expl[590]</div><br><br></b>";
            echo"</div>";

            }
            gameStats( $params['u']);

Thanks


Solution

  • You need to use preg_match_all() .

    eg: for all script

    $url = "www.popre.net/user.php?u=38456";
    parse_str( parse_url( $url, PHP_URL_QUERY), $params);
    
    function gameStats($u){
        $result = file_get_contents('http://popre.net/game.php?u='. $u . '');
        $return = array();
        $pattern = "\<td rowspan=\"2\" class=\"gamelevel\">\<span><a href=\"(.*)\">(.*)<\/a><\/span>\n(.*)<a href=\"(.*)\">(.*)<\/a><\/td>\n(.*)<td class=\"gamepointsresult dgame(.*)\">(.*)<\/td>(.*)<td class=\"gamepoints\">(.*)<\/td><td class=\"gamepoints\">(.*)<\/td><td class=\"gamepoints\">(.*)<\/td>\n(.*)<tr class=\"(one|two)\"><td colspan=\"4\" class=\"gameplayerssmall\">(.*)vs.(.*)<\/td>\<\/tr>";
        $match = preg_match_all('/'.$pattern.'/', $result, $matches);
    
        /* level */      foreach($matches[2] as $num => $one){ $return[$num]['level'] = $one; }
        /* map */        foreach($matches[5] as $num => $one){ $return[$num]['map'] = $one; }
        /* result */     foreach($matches[8] as $num => $one){ $return[$num]['result'] = $one; }
        /* points */     foreach($matches[10] as $num => $one){ $return[$num]['points'] = strip_tags($one); }
        /* mu */         foreach($matches[11] as $num => $one){ $return[$num]['mu'] = strip_tags($one); }
        /* sigma */      foreach($matches[12] as $num => $one){ $return[$num]['sigma'] = strip_tags($one); }
        /* player1 */    foreach($matches[15] as $num => $one){ $return[$num]['player1'] = strip_tags($one); }
        /* player2 */    foreach($matches[16] as $num => $one){ $return[$num]['player2'] = strip_tags($one); }
    
        return $return;
    }
    
    $gameInfo = gameStats($params['u']);
    
    echo "<table border='1'>
            <tr>
                <td>NO</td><td>LEVEL</td><td>MAP</td><td>RESULT</td><td>POINTS</td><td>MU</td><td>SIGMA</td><td>Player 1</td><td>Player 2</td>
            </tr>\n";
    
    foreach($gameInfo as $num => $one){
        echo "<tr>
            <td>".($num+1)."</td><td>".$one['level']."</td><td>".$one['map']."</td><td>".$one['result']."</td><td>".$one['points']."</td><td>".$one['mu']."</td><td>".$one['sigma']."</td><td>".$one['player1']."</td><td>".$one['player2']."</td>
        </tr>\n";
    }
    
    echo "</table>";
    

    You will get all results in that array, same for the others, then combine the array, as all results will have same number of elements in every array.