0

I have an application that will connect with an unofficial Overwatch API and get that player's info and store it in my database. I run through how I get the infomration just to make sure it's not a problem somewhere else.

I start with having each of the heroes inside an array like so:

$heroArray = array(
  "zenyatta" => "0",
  "mei" => "0",
  "tracer" => "0",
  "soldier76" => "0",
  "anaamari" => "0",
  "roadhog" => "0",
  "reinhardt" => "0",
  "torbjorn" => "0",
  "mccree" => "0",
  "junkrat" => "0",
  "dva" => "0",
  "winston" => "0",
  "hanzo" => "0",
  "widowmaker" => "0",
  "reaper" => "0",        //this hero is causing problems later
  "pharah" => "0",       //this hero is causing problems later
  "symmetra" => "0",    //this hero is causing problems later
  "genji" => "0",
  "lucio" => "0",
  "bastion" => "0",
  "mercy" => "0",
  "zarya" => "0",
  "sombra" => "0"
  );

The values are set to zero as the values to be filled will be the amount of hours the player has played with this hero. Then the heros are looped through to get the infomration from the JSON file the API has returned.

foreach($heroArray as $key => $value){
  $value = floatval($json[$region]['heroes']['stats']['quickplay'][$key]['general_stats']['time_played']);
}

The array and other values are then passed to the alterPlayer function. The SQL code is as follows:

$sqlQuery= "UPDATE player SET timeplayed = :timeplayed, prestige = :prestige , level = :level, avatar = :avatar, wins = :wins, Zenyatta = :zenyatta, Mei = :mei, Tracer = :tracer, Soldier76 = :soldier76, Anaamari = :anaamari, Roadhog = :roadhog, Reinhardt = :reinhardt, Torbjorn = :torbjorn, Mccree = :mccree, Junkrat = :junkrat, Dva = :dva, Winston = :winston, Hanzo = :hanzo, Widowmaker = :widowmaker, Reaper = :reaper, Pharah = :pharah, Symmetra = :symmetra, Genji = :genji, Lucio = :lucio, Bastion = :bastion, Mercy = :mercy, Zarya = :zarya, Sombra = :sombra, TopHero :heroTopName, SecondHero = :heroSecondName, ThirdHero = :heroThirdName, TopHeroElim = :heroTopElim, TopHeroDeath = :heroTopDeath, SecondHeroElim = :heroSecondElim, SecondHeroDeath = :heroSecondDeath, ThirdHeroElim = :heroThirdElim, ThirdHeroDeath = :heroThirdDeath WHERE Battletag = :battletag";

And then the values are assigned in the params array(I have removed some of the values to save room here):

$params = array(

        "junkrat" => $heroArray['junkrat'],
        "dva" => $heroArray['dva'],
        "winston" => $heroArray['winston'],
        "hanzo" => $heroArray['hanzo'],
        "widowmaker" => $heroArray['widowmaker'],
        "reaper" => $heroArray['reaper'],           //problem
        "pharah" => $heroArray['pharah'],          //problem
        "symmetra" => $heroArray['symmetra'],     //problem
        "genji" => $heroArray['genji'],
        "lucio" => $heroArray['lucio'],
        "bastion" => $heroArray['bastion'],
        "mercy" => $heroArray['mercy']

    );

When I run this in command line I get the following error:

C:\Windows\system32>C:/xampp\php\php.exe C:\xampp\htdocs\overwatch\alterPlayer.php
NULL

Notice: Undefined index: reaper in C:\xampp\htdocs\overwatch\PlayerTableGateway.php on line 123

Notice: Undefined index: pharah in C:\xampp\htdocs\overwatch\PlayerTableGateway.php on line 124

Notice: Undefined index: symmetra in C:\xampp\htdocs\overwatch\PlayerTableGateway.php on line 125
Could not update player

So the heroes that are causing problems are reaper, pharah and symmetra. If anyone can spot the error it would be very much appreciated!!

4
  • Your database is messed up. What if they add a new hero to the game? You'll have to create an extra column. Do something. Commented Dec 4, 2016 at 13:03
  • This is an academic project, if overwatch add anymore heroes I won't be adding them to my application. Commented Dec 4, 2016 at 13:04
  • 1
    I see. But even in minor projects it is good to use the best approaches, just so you get used to it. Commented Dec 4, 2016 at 13:06
  • I getcha, still trying to get the hang of databases and php! This error just has me stumped Commented Dec 4, 2016 at 13:11

1 Answer 1

1

In your foreach, you probably want to do this:

foreach($heroArray as $key => $value){
  $heroArray[$key] = floatval($json[$region]['heroes']['stats']['quickplay'][$key]['general_stats']['time_played']);
}

This will change the array correctly.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks so much for your help! I'm still getting the unidentified index errors for those three heroes.
The problem is coming from your $json variable then. Var dump it before you run your foreach and check if the champions exist within the quickplay array.
The point remains that you should change your foreach-loop. The loop only reads the value into $value, you can't overwrite it like this.
You're right about the json variable being the problem. The $json variable is null so I'll have to investigate as to why that is!
The $json object was null due to the method I was getting the url. So after a lot of hair pulling I've finally gotten it working! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.