1

I am trying to get it so index.php?steamusr=username get's set as the var and if none is specified it just displays a the error specified in the else statement.

I have tried if empty and isset. I have been trying every combo I can find and still nothing. This is my current code, the bottom is the error message. Thanks!

<?php
if (isset($_GET['steamusr'])) {
    $user = $_GET['steamusr'];
    $myinv = 'http://steamcommunity.com/id/$user/inventory/json/295110/1/';
    $content2 = file_get_contents($myinv);
    $json2 = json_decode($content2, true);
    $imgurlbase = 'http://steamcommunity-a.akamaihd.net/economy/image/';

    foreach($json2['rgDescriptions'] as $i){
       $item = $i['market_name'];
       $icon = $i['icon_url'];
       $fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=' . urlencode($item);
       $grab = file_get_contents($fetchdata);
       $id = json_decode($grab, true);
       echo '<img src="'. $imgurlbase . $icon . '/64fx64f">' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'<span class="badge">1</span> ' . $item .'</a>' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'Low</br>'. $id['lowest_price'].'</a>'.'<a class="btn btn-primary btn-lg" href="#" role="button">'.'Avg</br>'. $id['median_price'].'</a>'.'<br>';
     }
else {
    echo '<h1>No steam user set</h2>';
}
}
?>

Here is the error:

[Wed Aug 19 09:24:33.723604 2015] [:error] [pid 21378] [client 67.90.138.68:62702] PHP Parse error: syntax error, unexpected 'else' (T_ELSE) in /var/www/index.php on line 58

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ajankuv
  • 499
  • 3
  • 22
  • can `else` be use with `foreach` ? – Shehary Aug 19 '15 at 13:34
  • a missing `if` condition for the `else` or place it (else) outside your last line, being for the `if (isset($_GET['steamusr'])) {` condition. – Funk Forty Niner Aug 19 '15 at 13:34
  • 1
    actually yeah... it's misplaced. Place `else { echo '

    No steam user set'; }` after your last brace. That goes for `if (isset($_GET['steamusr'])) {`

    – Funk Forty Niner Aug 19 '15 at 13:37
  • 1
    Indent your code sensibly and these sort of errors are Plain to see. – RiggsFolly Aug 19 '15 at 13:40
  • Answer(s) posted below and comments above, but no response from the OP. If what has been posted below did not solve this, then your GET array `$_GET['steamusr']` may be failing you. If you're not already setup for error reporting.... Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Aug 19 '15 at 13:59

2 Answers2

3

The else part works with your first conditional statement, therefore it needs to be placed after your last brace.

<?php
if (isset($_GET['steamusr'])) {
    $user = $_GET['steamusr'];
    $myinv = 'http://steamcommunity.com/id/$user/inventory/json/295110/1/';
    $content2 = file_get_contents($myinv);
    $json2 = json_decode($content2, true);
    $imgurlbase = 'http://steamcommunity-a.akamaihd.net/economy/image/';

    foreach($json2['rgDescriptions'] as $i){
        $item = $i['market_name'];
        $icon = $i['icon_url'];
        $fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=' . urlencode($item);
        $grab = file_get_contents($fetchdata);
        $id = json_decode($grab, true);
        echo '<img src="'. $imgurlbase . $icon . '/64fx64f">' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'<span class="badge">1</span> ' . $item .'</a>' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'Low</br>'. $id['lowest_price'].'</a>'.'<a class="btn btn-primary btn-lg" href="#" role="button">'.'Avg</br>'. $id['median_price'].'</a>'.'<br>';
     }

  // else was here before

} else {
    echo '<h1>No steam user set</h2>';
}

?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

Change this part of code:

foreach($json2['rgDescriptions'] as $i){
$item = $i['market_name'];
$icon = $i['icon_url'];
$fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=' . urlencode($item);
$grab = file_get_contents($fetchdata);
$id = json_decode($grab, true);
echo '<img src="'. $imgurlbase . $icon . '/64fx64f">' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'<span class="badge">1</span> ' . $item .'</a>' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'Low</br>'. $id['lowest_price'].'</a>'.'<a class="btn btn-primary btn-lg" href="#" role="button">'.'Avg</br>'. $id['median_price'].'</a>'.'<br>';
  }
  else {
echo '<h1>No steam user set</h2>';
  }
}

to

foreach($json2['rgDescriptions'] as $i){
    $item = $i['market_name'];
    $icon = $i['icon_url'];
    $fetchdata = 'http://steamcommunity.com/market/priceoverview/?appid=295110&currency=1&market_hash_name=' . urlencode($item);
    $grab = file_get_contents($fetchdata);
    $id = json_decode($grab, true);
    echo '<img src="'. $imgurlbase . $icon . '/64fx64f">' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'<span class="badge">1</span> ' . $item .'</a>' . '<a class="btn btn-primary btn-lg" href="#" role="button">'.'Low</br>'. $id['lowest_price'].'</a>'.'<a class="btn btn-primary btn-lg" href="#" role="button">'.'Avg</br>'. $id['median_price'].'</a>'.'<br>';
  }
}
else {
    echo '<h1>No steam user set</h2>';
  }

Now you were putting your else after the foreach, instead of the if.

Blaatpraat
  • 2,829
  • 11
  • 23