0

Okay, this is really frustrating me and I don't know why. I have been scouring the internet for at least two days trying to figure out the problem. I am trying to pull xp amount for a certain skill for all the names stored in the database. It echoed the specified xp for all the users in the database, but was repeating the values with an undefined index. Even though that error is there it still prints the values. I researched the internet and found that break will stop the repeating, but when I add the break it then wont print the values and just gives me the undefined index. THe index does exist.

$sql = "SELECT rsn FROM users";

$qry = $conn->prepare($sql);

$qry->execute();

while($row = $qry->fetch(PDO::FETCH_ASSOC)) {

    $rsn = $row['rsn'];

    $hs = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=". $rsn);

    $hs = explode("\n",$hs);

    $skills = array("Overall","Attack","Defence","Strength","Constitution","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering", "Divination", "Invention");

    $i = 0;

    foreach(array_unique($skills) as $value){

        $hs[$i] = explode(",",$hs[$i]);

        $stats[$value]["rank"] = number_format($hs[$i][0]);

        $stats[$value]["level"] = number_format($hs[$i][1]);

        $stats[$value]["xp"] = number_format($hs[$i][2]);

        $i++;

        /*require_once 'tourny_config_include.php';

        $sql = "UPDATE active_tourny SET xp = :xp AND from_date = :from AND to_date = :to WHERE rsn = :rsn";

        $qry = $conn->prepare($sql);

        $qry->execute(array(':xp' => $xp, ':rsn' => $rsn, ':to' => $tournyTo, ':from' => $tournyFrom));*/

        echo $stats["Attack"]["xp"] . '<br>';

        break;

    }

}
  • If it gives an undefined index, it doesn't exist. You also have an undefined variable, prior to the loop you should define `$stats = array()`. Breaking will stop the loop though, that's exactly what it does. I'm not exactly sure what sort of output you want to achieve, or exactly what the results currently look like though. Could you provide some more detailed information? – Qirel May 09 '17 at 22:39
  • 1
    break like that will just stop your loop on the first time through, essentially making a loop pointless. You are getting the error because the index doesn't exist. – Jonathan Kuhn May 09 '17 at 22:39
  • About undefined indexes, you should have a look at http://stackoverflow.com/q/4261133/4535200 – Qirel May 09 '17 at 22:40
  • It does. The error says that the index "Attack" does not exist. Look in the $skills array and it is there – Daniel Johanson May 09 '17 at 22:42
  • Runescape :). On your first run you add 'Overall' to the $stats array. At that point 'Attack' has not been set in $stats, so it errors. Once $value 'Attack' has been put through the loop, the error should disappear. – RST May 09 '17 at 22:46
  • Another thing is I also said without the break there it outputs the values that is stored in that variable that I am echoing. So for that to happen that means that it has to exists, right? – Daniel Johanson May 09 '17 at 22:48
  • @RST how do I do that? – Daniel Johanson May 09 '17 at 22:51

2 Answers2

0
$qry = $conn->prepare($sql);

$qry->execute();

while($row = $qry->fetch(PDO::FETCH_ASSOC)) {

    $rsn = $row['rsn'];

    $hs = file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=". $rsn);

    $hs = explode("\n",$hs);

    $skills = array("Overall","Attack","Defence","Strength","Constitution","Ranged","Prayer","Magic","Cooking","Woodcutting","Fletching","Fishing","Firemaking","Crafting","Smithing","Mining","Herblore","Agility","Thieving","Slayer","Farming","Runecrafting","Hunter","Construction","Summoning","Dungeoneering", "Divination", "Invention");

    $i = 0;

    foreach($skills as $skill){

        $hs[$i] = explode(",",$hs[$i]);

        $stats[$skill]["rank"] = number_format($hs[$i][0]);

        $stats[$skill]["level"] = number_format($hs[$i][1]);

        $stats[$skill]["xp"] = number_format($hs[$i][2]);

        $i++;

        /*require_once 'tourny_config_include.php';

        $sql = "UPDATE active_tourny SET xp = :xp AND from_date = :from AND to_date = :to WHERE rsn = :rsn";

        $qry = $conn->prepare($sql);

        $qry->execute(array(':xp' => $xp, ':rsn' => $rsn, ':to' => $tournyTo, ':from' => $tournyFrom));*/

        // display Attack xp if available
        if isset($stats["Attack"]["xp"])        
           echo $stats["Attack"]["xp"] . '<br>';

    }

}
RST
  • 3,899
  • 2
  • 20
  • 33
  • I did the isset thing and added a else echo not set clause and I get a blank screen – Daniel Johanson May 09 '17 at 23:12
  • I figured out why the blank screen, the break was causing it. `if(isset($stats["Attack"]["xp"])){ echo $stats["Attack"]["xp"]}else{ echo 'not set';}` This echos both the variable and "not set" – Daniel Johanson May 09 '17 at 23:39
  • You broke the code because you added the `else` the wrong way. So what I meant was, try the code the way I supplied it. It will work. Then change it the way you want one thing at a time, so you will see what code change breaks the code – RST May 10 '17 at 10:01
0

Wow I cannot believe that it was this simple. All I had to do is move

echo $stats["Attack"]["xp"] . '<br>';

outside the foreach loop