CS-CASES. org
CS-CASES. org

Reputation: 23

Background image from JSON

Alright, so I have a json file stored like the following,

{  
   "market_name":"\u2605 Bayonet",
   "market_hash_name":"\u2605 Bayonet",
   "icon_url":"\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_nsVtzdOEdtWwKGZZLQHTxDZ7I56KU0Zwwo4NUX4oFJZEHLbXU5A1PIYQh5hlcX0nvUOGsx8DdQBJjIAVHubSaKQZ53P3NZXMXvYmykdLSxqWkZ7-HkjMIvpIj3u2Y84733gzh_RU_MG_zIYLEdQ45fxiOrdJh0ExF",
   "name_color":"8650AC",
   "quality_color":"EB4B4B"
}

Now using PHP I'm getting icon_url and then I'm trying to add this as a background image, but since the url has all these /\/\ it won't work for a background image. How can I solve this?

This is how I'm trying to echo the div,

    <div class='image' style='background-image:url('" . $icon . "')'></div>

Edit: it does work when used with <img> but I'm soly looking for background-image

This is the full code used:

$getInventory = $db->query('SELECT * FROM `inventories` WHERE `user` = '.$db->quote($user['steamid']));
        $fetchInventory = $getInventory->fetchAll();

        $prices = file_get_contents('pricesdb.txt');$prices = json_decode($prices, true);
        $images = file_get_contents('iconsdb.txt');$images = json_decode($images, true);

        $inventoryArray = array();
        foreach ($fetchInventory as $key => $value) {
            $item = $value['item'];
            $id = $value['id'];
            $worth = number_format($prices['response']['items'][$item]['value']/100, 2, '.', 1);
            $icon;
            $rarity;

            foreach($images['items'] as $key => $value){            
                if($value['market_hash_name'] == $item){                    
                    $icon = $value['icon_url'];
                    $rarity = $value['quality_color'];
                }           
            }           

            $item_wear = preg_replace('/^.*(\(.*\)).*$/', '$1', $item);
            $item_wear_short = preg_replace("/(?![A-Z])./", "", $item_wear);
            $item_rarity = $rarity;

            $exp_item_version = explode(" | ", str_replace($item_wear,"", $item));

            $item_version = $exp_item_version[0];
            $item_name = $exp_item_version[1];

            $itemArray = array($id, $icon, $worth, $item_version, $item_rarity, $item_name);

            array_push($inventoryArray, $itemArray);
        }       

        usort($inventoryArray, function($a, $b) {
            return $b[2] - $a[2];
        });

        foreach ($inventoryArray as $key => $value) {
            $id = $inventoryArray[$key][0];
            $icon = $inventoryArray[$key][1];
            $worth = $inventoryArray[$key][2];
            $item_version = $inventoryArray[$key][3];
            $item_rarity = $inventoryArray[$key][4];
            $item_name = $inventoryArray[$key][5];

            echo "<div class='item'  data-item='" . $id . "'>
            <div class='rarity rarity-gold'></div>
            <div class='image' style='background-image:url('" . $icon . "')'></div>
            <div class='market-name'>
            <div class='type'>
            " . convert_ascii($item_version) . "
            </div>
            <div class='name'>
            " . $item_name . "
            </div>
            </div>
            <div class='wear'>
            FN
            </div>
            <div class='price'>$" . $worth . "</div>
            </div>
            ";

        }

Upvotes: 1

Views: 4194

Answers (3)

Oen44
Oen44

Reputation: 3206

Here lies your problem.

style='background-image:url('" . $icon . "')'

You are using single quotes as style and then as url. Simple solution.

Edit

<div class='image' style='background:url(\"" . $icon . "\")'></div>

Upvotes: 1

Adil Aliyev
Adil Aliyev

Reputation: 1169

You can use stripslashes.

<?php

$str = '\/\/steamcommunity-a.akamaihd.net\/economy\/image\/-9a81dlWLwJ2UUGcVs_n$

echo (stripslashes($str));

For more detailed info please take a look: http://php.net/manual/en/function.stripslashes.php

Upvotes: 0

bkrukowski
bkrukowski

Reputation: 66

Use json_decode, \/\/ will be convert to //, it is correct protocol relative path. If it does not work issue is in different place.

Upvotes: 1

Related Questions