SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    % in Arrays | Need Help Plz

    Hello,

    Ive been running a script on my website for awhile to grab games I play on Halo 2 from www.bungie.net

    In the script, i have this array (provided by someonewhois):

    PHP Code:
    $king_games = array ("%King%""Crazy King""King""Phantom King""Team Crazy King""Team King");
        
    $plot_games = array ("%Plots%""3 Plots""Land Grab""Gold Rush""Control Issues""Contention");
        
    $ball_games = array ("%Ball%""Team Ball""Fiesta Ball""Oddball""Team Ball""Rocketball""Swordball""Low Ball");
        
    $ctf_games = array ("%CTF%""%Flag%""Multi Flag CTF""CTF Classic""1 Flag CTF""1 Flag CTF Fast""1 Flag Cov BR");
        
    $assault_games = array ("%Bomb%""Multi Bomb""Single Bomb""Single Bomb Fast""Neutral Bomb""Blast Resort""Neut. Bomb Fast""Rapid Assault""Assault""Neutral Bomb BR");

        
    $gametypes = array('king' => $king_games'plots' => $plot_games'oddball' => $ball_games'captures' => $ctf_games'plants' => $assault_games);

        foreach(
    $gametypes AS $k=>$r)
        {
            if (
    in_array($gametype$r))
            {    
                
    $sql6 "
                UPDATE player_stats
                SET 
    $k = '$score'
                WHERE gamertag='
    $name' AND bunid='$gameid'"
                
                 
    $result6 mysql_query($sql6$linkID) or die(mysql_error());
    ;
                break;
            }
        } 
    It works fine, but only if I have the exact name listed. From the code above, you can tell that I was experimenting with the "%word%"

    Ive used this is other cases, and its work perfectly. But i think somehow the array is messing it up; or im doing something wrong.

    Say the word is %Flag%, i want it to recognize things like:

    1 Flag CTF
    2 Flag CTF BR
    Flag BR

    Anything that has Flag in it.

    Im not sure if im doing this right though, can anyone help?

    Thanx in advance.
    BKerr

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anybody have a clue? I really need this. Thx
    BKerr

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I suggest you do is implement the following function somewhere in the script.
    PHP Code:
    function in_game_array($needle NULL$haystack NULL) {
        if (!
    is_array($haystack) || $needle === NULL) {
            return 
    false;
        }
        foreach (
    $haystack as $k => $v) {
            if (
    ereg('\%([a-zA-Z0-9]+)\%'$v$regs)) {
                if (
    stristr($needle$regs[1])) {
                    return 
    stristr($needle$regs[1]);
                } else {
                    continue;
                }
            } else {
                continue;
            }
        }
        return 
    in_array($needle$haystack);

    Next, replace this:
    PHP Code:
            if (in_array($gametype$r))
            { 
    With this:
    PHP Code:
            if (in_game_array($gametype$r))
            { 
    I tested it and it worked well.

    Also, I'm a Halo 2 user on xbox live, so I'd be interesting in seeing this upcoming site. My xbox live gamertag is BullseyePimp, if you are interested in playing me.
    Last edited by altecex; Jun 11, 2005 at 12:39.

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont understand that first part with the whole haystack thing. How is that going to help determine if a gametype is a certain kind (oddball, ctf, plots)
    BKerr

  5. #5
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to pass a needle and haystack variable to that function. The haystack would be all your data and the needle would be the game type you were looking for.

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, im totally confused. I have no idea what is going on.

    Could someone try subsituting it into my script for me please, im totally lost with your talk:

    Whole Script:
    PHP Code:
    <?php


    define
    ('MAGPIE_DIR''/home/buckboa/public_html/halouniv/bkerr/');
    require_once(
    MAGPIE_DIR.'rss_fetch.inc');

    $linkID = @mysql_connect("localhost""buckboa_gamesite""gamesite") or die ('Failed to connect to localhost');;
    mysql_select_db("buckboa_gamesite"$linkID) or die ('Failed to select buckboa_gamesite due to ' mysql_error());
    $sql "SELECT rssurl FROM stat_users WHERE level = 'Auto'";
    $result10 mysql_query($sql) or die('Failed to execute ' $sql ' due to ' mysql_error());

    while (
    $myrow mysql_fetch_assoc($result10))
    {
       
    $rss fetch_rss($myrow['rssurl']);
       foreach (
    $rss->items as $item)
       {
        
    $link $item['link'];
        
    $title $item['title'];    
        
    $date1 $item['pubDate'];
        
    $description $item['description'];
        
    $string $item['link'];

        
    preg_match('/gameid=(.*)&/',$string,$match) || preg_match('/gameid=(.*)/',$string,$match);
        
    $gameid $match[1];

        if (!
    preg_match('&(.*): (.*) on (.*)&'$item['title'], $matches))
        {
          print 
    "ill formed RSS!MATCHES\n";
        }

        
    $playlist $matches[1];
        
    $gametype $matches[2];
        
    $level $matches[3];

        if (!
    preg_match('&(.*), (\d*) (.*) (\d*) (\d*):(\d*):(\d*) GMT&'$item['description'], $date))
        {
          print 
    "ill formed RSS! DATE\n";
        }

        
    $daynum $date[2] . "\n";
        
    $month $date[3] . "\n";
        
    $year $date[4] . "\n";

        
    $desc '<b>Gamertag (Team): Score, Kills, Deaths, Assists</b>';
        
    $pos strpos($item['description'],$desc);
        if (
    $pos === false)
        {
          print 
    "ill formed RSS!DESCRIPTION\n";
        }

        
    $sea substr($item['description'],strlen($desc)+$pos);
        if (!
    preg_match_all('&>(.*) \((\d*)\): (\d*), (\d*), (\d*), (\d*)<br/&iU'$sea$players))
        {
          print 
    "ill formed RSS!DESCRIPTION2\n";
        }

        
    $total count($players[0]);
        for (
    $i=0$i$total$i++)
        {
          
    $Playernb $i;
          
    $name $players[1][$i];
          
    $team $players[2][$i];
          
    $score $players[3][$i];
          
    $kills $players[4][$i];
          
    $deaths $players[5][$i];
          
    $assists $players[6][$i];

          if (
    $playlist == 'Arranged Game')
          {
            
    $gametype 'Custom';
          }

          
    $sql3 "INSERT INTO player_stats (bunid, gamertag, team, score, kills, deaths,  assists, playlist, gametype, level) VALUES ('".$gameid."', '".$name."', '".$team."', '".$score."', '".$kills."',  '".$deaths."', '".$assists."', '".$playlist."', '".$gametype."', '".$level."')";
          
    $result3 mysql_query($sql3);
          
        
    $king_games = array ("%King%""Crazy King""King""Phantom King""Team Crazy King""Team King");
        
    $plot_games = array ("%Plots%""3 Plots""Land Grab""Gold Rush""Control Issues""Contention");
        
    $ball_games = array ("%Ball%""Team Ball""Fiesta Ball""Oddball""Team Ball""Rocketball""Swordball""Low Ball");
        
    $ctf_games = array ("%CTF%""%Flag%""Multi Flag CTF""CTF Classic""1 Flag CTF""1 Flag CTF Fast""1 Flag Cov BR");
        
    $assault_games = array ("%Bomb%""Multi Bomb""Single Bomb""Single Bomb Fast""Neutral Bomb""Blast Resort""Neut. Bomb Fast""Rapid Assault""Assault""Neutral Bomb BR");

        
    $gametypes = array('king' => $king_games'plots' => $plot_games'oddball' => $ball_games'captures' => $ctf_games'plants' => $assault_games);

        foreach(
    $gametypes AS $k=>$r)
        {
            if (
    in_array($gametype$r))
            {    
                
    $sql6 "
                UPDATE player_stats
                SET 
    $k = '$score'
                WHERE gamertag='
    $name' AND bunid='$gameid'"
                
                 
    $result6 mysql_query($sql6$linkID) or die(mysql_error());
    ;
                break;
            }
        }
        }

        
    $sql2 "INSERT INTO game_info (bunid, date, playlist, gametype, level) VALUES ('".$gameid."', '".$daynum$month$year."', '".$playlist."', '".$gametype."', '".$level."')";
        
    $result2 mysql_query($sql2$linkID);
       }
    }

    mysql_close($linkID);
    ?>
    Thanks.
    BKerr

  7. #7
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this.

    First, make a file called functions.php:

    PHP Code:
    <?php

    #functions.php

    function in_game_array($needle NULL$haystack NULL) {
        if (!
    is_array($haystack) || $needle === NULL) {
            return 
    false;
        }
        foreach (
    $haystack as $k => $v) {
            if (
    ereg('\%([a-zA-Z0-9]+)\%'$v$regs)) {
                if (
    stristr($needle$regs[1])) {
                    return 
    stristr($needle$regs[1]);
                } else {
                    continue;
                }
            } else {
                continue;
            }
        }
        return 
    in_array($needle$haystack);
    }
    ?>
    Then, change your main code to this.

    PHP Code:
    <?php


    define
    ('MAGPIE_DIR''/home/buckboa/public_html/halouniv/bkerr/');
    require_once(
    MAGPIE_DIR.'rss_fetch.inc');
    require_once(
    'functions.php');

    $linkID = @mysql_connect("localhost""buckboa_gamesite""gamesite") or die ('Failed to connect to localhost');;
    mysql_select_db("buckboa_gamesite"$linkID) or die ('Failed to select buckboa_gamesite due to ' mysql_error());
    $sql "SELECT rssurl FROM stat_users WHERE level = 'Auto'";
    $result10 mysql_query($sql) or die('Failed to execute ' $sql ' due to ' mysql_error());

    while (
    $myrow mysql_fetch_assoc($result10))
    {
       
    $rss fetch_rss($myrow['rssurl']);
       foreach (
    $rss->items as $item)
       {
        
    $link $item['link'];
        
    $title $item['title'];    
        
    $date1 $item['pubDate'];
        
    $description $item['description'];
        
    $string $item['link'];

        
    preg_match('/gameid=(.*)&/',$string,$match) || preg_match('/gameid=(.*)/',$string,$match);
        
    $gameid $match[1];

        if (!
    preg_match('&(.*): (.*) on (.*)&'$item['title'], $matches))
        {
          print 
    "ill formed RSS!MATCHES\n";
        }

        
    $playlist $matches[1];
        
    $gametype $matches[2];
        
    $level $matches[3];

        if (!
    preg_match('&(.*), (\d*) (.*) (\d*) (\d*):(\d*):(\d*) GMT&'$item['description'], $date))
        {
          print 
    "ill formed RSS! DATE\n";
        }

        
    $daynum $date[2] . "\n";
        
    $month $date[3] . "\n";
        
    $year $date[4] . "\n";

        
    $desc '<b>Gamertag (Team): Score, Kills, Deaths, Assists</b>';
        
    $pos strpos($item['description'],$desc);
        if (
    $pos === false)
        {
          print 
    "ill formed RSS!DESCRIPTION\n";
        }

        
    $sea substr($item['description'],strlen($desc)+$pos);
        if (!
    preg_match_all('&>(.*) \((\d*)\): (\d*), (\d*), (\d*), (\d*)<br/&iU'$sea$players))
        {
          print 
    "ill formed RSS!DESCRIPTION2\n";
        }

        
    $total count($players[0]);
        for (
    $i=0$i$total$i++)
        {
          
    $Playernb $i;
          
    $name $players[1][$i];
          
    $team $players[2][$i];
          
    $score $players[3][$i];
          
    $kills $players[4][$i];
          
    $deaths $players[5][$i];
          
    $assists $players[6][$i];

          if (
    $playlist == 'Arranged Game')
          {
            
    $gametype 'Custom';
          }

          
    $sql3 "INSERT INTO player_stats (bunid, gamertag, team, score, kills, deaths,  assists, playlist, gametype, level) VALUES ('".$gameid."', '".$name."', '".$team."', '".$score."', '".$kills."',  '".$deaths."', '".$assists."', '".$playlist."', '".$gametype."', '".$level."')";
          
    $result3 mysql_query($sql3);
          
        
    $king_games = array ("%King%""Crazy King""King""Phantom King""Team Crazy King""Team King");
        
    $plot_games = array ("%Plots%""3 Plots""Land Grab""Gold Rush""Control Issues""Contention");
        
    $ball_games = array ("%Ball%""Team Ball""Fiesta Ball""Oddball""Team Ball""Rocketball""Swordball""Low Ball");
        
    $ctf_games = array ("%CTF%""%Flag%""Multi Flag CTF""CTF Classic""1 Flag CTF""1 Flag CTF Fast""1 Flag Cov BR");
        
    $assault_games = array ("%Bomb%""Multi Bomb""Single Bomb""Single Bomb Fast""Neutral Bomb""Blast Resort""Neut. Bomb Fast""Rapid Assault""Assault""Neutral Bomb BR");

        
    $gametypes = array('king' => $king_games'plots' => $plot_games'oddball' => $ball_games'captures' => $ctf_games'plants' => $assault_games);

        foreach(
    $gametypes AS $k=>$r)
        {
            if (
    in_game_array($gametype$r))
            {    
                
    $sql6 "
                UPDATE player_stats
                SET 
    $k = '$score'
                WHERE gamertag='
    $name' AND bunid='$gameid'"
                
                 
    $result6 mysql_query($sql6$linkID) or die(mysql_error());
    ;
                break;
            }
        }
        }

        
    $sql2 "INSERT INTO game_info (bunid, date, playlist, gametype, level) VALUES ('".$gameid."', '".$daynum$month$year."', '".$playlist."', '".$gametype."', '".$level."')";
        
    $result2 mysql_query($sql2$linkID);
       }
    }

    mysql_close($linkID);
    ?>
    Last edited by altecex; Jun 11, 2005 at 12:38.

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2004
    Location
    USA
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But is that going to help the %Word% things work in the array? Thats the only problem.
    BKerr

  9. #9
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    San Diego, CA (USA)
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, thats what it does!


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
  •