Jump to content
Lonster_Monster

Help with Php in a Project

Recommended Posts

Lonster_Monster    2
Lonster_Monster

I am working on a php json to table organizer to see items in a backpack and see the prices of items that are in a separate json file (prices.json) and i have gotten everything just the only problem is the output only shows up 1 item of each item so say u have 20 keys it will only show up 1 and how can i have it count how many times the item with the same defindex to have a count say keys or craft hats i will post the source code for it

Question:

in my code why is it only showing up one item instead of all of a single item

How to code each time the defindex is processed to generate a count feed

 

Code: https://github.com/LonsterMonster/json-view

that is the beginning source code  keep in mind it is in php so you will need a php envirnment to run it

 

On the code Constructive Criticism is strongly encouraged

Share this post


Link to post
Share on other sites
Mengh.    759
Mengh.

For the JSON page of an inventory, it looks pretty different in each half. This is because this is structured as best as possible as to prevent duplication of the same item description data e.g having 5 Cloak & Daggers in your inventory with the same data for its stats. It can be tricky to understand it and the mistake you made was to iterate through the item descriptions, rather than the data for each item instance.

 

My inventory: https://steamcommunity.com/profiles/76561198342617112/inventory/json/440/2

$json = {
	"success": true,
	"rgInventory": {
		"6931878409":{"id":"6931878409","classid":"780611810","instanceid":"11040578","amount":"1","pos":1},
		"6931906464":{"id":"6931906464","classid":"780611623","instanceid":"11040578","amount":"1","pos":2},
		...
	},
	"rgDescriptions": {
		"780611810_11040578":{"appid":"440","classid":"780611810","instanceid":"11040578","icon_url":"fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwsUXA_qvSp8n8nyDvqzBOESnN974JQBiWA_kgd4YbrjNW41c1aSUaEKDKFipVu-WSFj7JQzUIHup-wEeRKv6tVDBt05jw","icon_url_large":"fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwsUXA_qvSp8n8nyDvqzBOESnN974JQBiWA_kgd4YbrjNW41c1aSUaEKDKFipVu-WSFj7JQzUIHup-wEeRKv6tVDBt05jw","icon_drag_url":"","name":"The Cloak and Dagger","market_hash_name":"The Cloak and Dagger","market_name":"The Cloak and Dagger","name_color":"7D6D00","background_color":"3C352E","type":"Level 5 Invis Watch","tradable":1,"marketable":0,"commodity":0,"market_tradable_restriction":"7","market_marketable_restriction":"0","descriptions":[{"value":"Cloak Type: Motion Sensitive.\nAlt-Fire: Turn invisible.  Cannot attack while invisible.  Bumping in to enemies will make you slightly visible to enemies.\nCloak drain rate based on movement speed."},{"value":"+100% cloak regen rate","color":"7ea9d1"},{"value":"No cloak meter from ammo boxes when invisible","color":"d83636"},{"value":"-35% cloak meter from ammo boxes","color":"d83636"},{"value":" "},{"value":"( Not Usable in Crafting )"}],"actions":[{"name":"Item Wiki Page...","link":"http:\/\/wiki.teamfortress.com\/scripts\/itemredirect.php?id=60&lang=en_US"}],"tags":[{"internal_name":"Unique","name":"Unique","category":"Quality","color":"7D6D00","category_name":"Quality"},{"internal_name":"pda2","name":"Secondary PDA","category":"Type","category_name":"Type"},{"internal_name":"Spy","name":"Spy","category":"Class","category_name":"Class"}],"app_data":{"def_index":"60","quality":"6"}}},
		"780611623_11040578":{"appid":"440","classid":"780611623","instanceid":"11040578","icon_url":"fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUxADXBXltD1Rt8TnH_WJRrdSn4JktMcF2GVqyQd_NbHiNjU_IVCUVflcX_M8olq6X35gvJ81V47457UBTPbzYyk","icon_url_large":"fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUxADXBXltD1Rt8TnH_WJRrdSn4JktMcF2GVqyQd_NbHiNjU_IVCUVflcX_M8olq6X35gvJ81V47457UBTPbzYyk","icon_drag_url":"","name":"The Wrangler","market_hash_name":"The Wrangler","market_name":"The Wrangler","name_color":"7D6D00","background_color":"3C352E","type":"Level 5 Laser Pointer","tradable":1,"marketable":0,"commodity":0,"market_tradable_restriction":"7","market_marketable_restriction":"0","descriptions":[{"value":"Take manual control of your Sentry Gun.\nWrangled sentries gain a shield that reduces\ndamage and repairs by 66%.\nSentries are disabled for 3 seconds after becoming unwrangled."},{"value":" "},{"value":"( Not Usable in Crafting )"}],"actions":[{"name":"Item Wiki Page...","link":"http:\/\/wiki.teamfortress.com\/scripts\/itemredirect.php?id=140&lang=en_US"}],"tags":[{"internal_name":"Unique","name":"Unique","category":"Quality","color":"7D6D00","category_name":"Quality"},{"internal_name":"secondary","name":"Secondary weapon","category":"Type","category_name":"Type"},{"internal_name":"Engineer","name":"Engineer","category":"Class","category_name":"Class"}],"app_data":{"def_index":"140","quality":"6"}}}
	}
}

Inventory data is stored in two keys, one for item instances (each Cloak & Dagger is a separate item in terms of assetid so will each be listed individually) in "rgInventory" and one is a sort of dictionary that holds in-game description data that describes an item (or identical items) through using the classid_instanceid amalgamation as a sort of foreign key within "rgDescriptions".

 

2 completely clean Strange Cloak & Daggers will be their own key within "rgInventory" and treated separately there, but will share the same item description / have the same classid and instanceid as they are identical in nature, except for their assetids. If one C&D was to get a Strange Part put on it, it would now not be identical to the other, so will each get their own descriptions in "rgDescriptions".

 

Luckily, as the rgDescriptions uses a hashing system, you can quickly get an items' additional data through accessing $json["rgDescriptions"][$classid."_".$instanceid];

 

Updated code.

<?PHP
$query = "prices.json";//Prices .json of items want to sell
$json = @file_get_contents($query);
$data = json_decode($json, true);
$prices = $data["rgPrice"];
$id2 = "";//Your steam id here
$query = "http://steamcommunity.com/profiles/".$id2."/inventory/json/440/2/";//Invetnroy fetch
$json = @file_get_contents($query);
$data = json_decode($json, true);

$items = $data["rgInventory"] #Changed this as we need to actually iterate through our instances of items, not our dictionary of descriptions. That's why you only had 1 of each item.
$listOfItemDescriptions = $data["rgDescriptions"]; #Added this
error_reporting(E_ALL); 
?>

<?PHP
foreach($items as $item) {
	$image_url = "http://cdn.steamcommunity.com/economy/image/";
	if($item["icon_url"]) {
		$image_url = "http://cdn.steamcommunity.com/economy/image/".$item["icon_url"];
	}

	$classid = @$item['classid'];
	$instanceid = @$item['instanceid'];
	$descriptionId = $classid."_".$instanceid; #Concatenates classid and instanceid for access to item's in-game description.
	$item['description'] = $listOfItemDescriptions[$descriptionId]; #Set it back into item variable for ease of access
  
  	#You can now access in-game description data.
	$defindex = @$item['description']['app_data']['def_index'];
	$itemname = @$item['description']['market_hash_name'];
	$flag_cannot_trade = @$item['description']['tradable'];
	$quality = @$item['description']['app_data']['quality'];
	$type = @$item['description']['tags'][1]['name'];
  
		
	$appid = $item['description']['appid'];
	if ($appid == 440){
		foreach($prices as $price){
			$pdefindex = @$price['app_data']['def_index'].$defindex;
			if($defindex === $pdefindex){
				$pquality = @$defindex.$quality;
				$pricebuy = @$pquality['price']['buy'];
				$pricesell = @$pquality['price']['sell'];
			}
			$quality = getQuality($quality);
			$flag_cannot_trade = getTradable($flag_cannot_trade);
			
?>

Feel free to reply below if you need more help.

 

Share this post


Link to post
Share on other sites
Lonster_Monster    2
Lonster_Monster

First off Thank You very much it shows all items and now the problem it calling the image url i have

$image_url = "http://cdn.steamcommunity.com/economy/image/";
	if($item["icon_url_large"]) {
		$image_url = $item["icon_url_large"];
	} else {
		$image_url = $item["icon_url"];
	}

but it only output the cdn link and this Notice for each of the items

Notice: Undefined index: icon_url 

Undefined index: icon_url_large

 

Share this post


Link to post
Share on other sites
Mengh.    759
Mengh.
Just now, Lonster_Monster said:

First off Thank You very much it shows all items and now the problem it calling the image url i have

$image_url = "http://cdn.steamcommunity.com/economy/image/";
	if($item["icon_url_large"]) {
		$image_url = $item["icon_url_large"];
	} else {
		$image_url = $item["icon_url"];
	}

but it only output the cdn link adn this Notice for each of the items

Notice: Undefined index: icon_url 

Undefined index: icon_url_large

 

Woops, forgot to update that in my code.

 

Move the image url data below setting the item description back into the item variable like so:

<?PHP
	$classid = @$item['classid'];
	$instanceid = @$item['instanceid'];
	$descriptionId = $classid."_".$instanceid; #Concatenates classid and instanceid for access to item's in-game description.
	$item['description'] = $listOfItemDescriptions[$descriptionId]; #Set it back into item variable for ease of access
  
  	#You can now access in-game description data.
	$defindex = @$item['description']['app_data']['def_index'];
	$itemname = @$item['description']['market_hash_name'];
	$flag_cannot_trade = @$item['description']['tradable'];
	$quality = @$item['description']['app_data']['quality'];
	$type = @$item['description']['tags'][1]['name'];

	#Image data
	$image_url = "http://cdn.steamcommunity.com/economy/image/";
	if($item['description']["icon_url_large"]) {
		$image_url = $item['description']["icon_url_large"];
	} else {
		$image_url = $item['description']["icon_url"];
	}
?>

 

Share this post


Link to post
Share on other sites
Lonster_Monster    2
Lonster_Monster

AWESOME Thank you Very Much!!!

the image code didnt work i fixed by doing

$image_url = "http://cdn.steamcommunity.com/economy/image/";
	if($item['description']["icon_url_large"]) {
		$image_url = "http://cdn.steamcommunity.com/economy/image/".$item['description']["icon_url_large"];
	} else {
		$image_url = "http://cdn.steamcommunity.com/economy/image/".$item['description']["icon_url"];
	}

just want to make sure if i have to code correct

Share this post


Link to post
Share on other sites
Mengh.    759
Mengh.
7 minutes ago, Lonster_Monster said:

AWESOME Thank you Very Much!!!

the image code didnt work i fixed by doing

$image_url = "http://cdn.steamcommunity.com/economy/image/";
	if($item['description']["icon_url_large"]) {
		$image_url = "http://cdn.steamcommunity.com/economy/image/".$item['description']["icon_url_large"];
	} else {
		$image_url = "http://cdn.steamcommunity.com/economy/image/".$item['description']["icon_url"];
	}

just want to make sure if i have to code correct

Yep, for your $image_url, looks good if you're using the link straight away.

 

If you need to change the size of an image from the steamcommunity cdn, you can also do

<?PHP
$image_url = "http://cdn.steamcommunity.com/economy/image/".$item['description']["icon_url_large"]."/100fx100f"; #Change 100px (f) to whatever size you need
?>

Normal size: https://steamcommunity.com/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwsUXA_qvSp8n8nyDvqzBOESnN974JQBiWA_kgd4YbrjNW41c1aSUaEKDKFipVu-WSFj7JQzUIHup-wEeRKv6tVDBt05jw/

 

100px x 100px: https://steamcommunity.com/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwsUXA_qvSp8n8nyDvqzBOESnN974JQBiWA_kgd4YbrjNW41c1aSUaEKDKFipVu-WSFj7JQzUIHup-wEeRKv6tVDBt05jw/100fx100f

 

250px x 250px: https://steamcommunity.com/economy/image/fWFc82js0fmoRAP-qOIPu5THSWqfSmTELLqcUywGkijVjZULUrsm1j-9xgEIUwsUXA_qvSp8n8nyDvqzBOESnN974JQBiWA_kgd4YbrjNW41c1aSUaEKDKFipVu-WSFj7JQzUIHup-wEeRKv6tVDBt05jw/250fx250f

 

Really helps when you're dealing with a large inventory and need to load a lot of images on a page 👍.

 

Share this post


Link to post
Share on other sites
Lonster_Monster    2
Lonster_Monster

ok with the size i have change the size in html imag height and width and get the correct size but thanks

 

Share this post


Link to post
Share on other sites
Lonster_Monster    2
Lonster_Monster

I have one more question i am working on the json that i have in the project on github but when i call it on the sell prices it has an error

Undefined index: sell 

here is what i am doing

$pdefindex = $prices['app_data']['def_index']['quality'];
$sellprice = $prices['app_data']['def_index']['quality']['sell'];

 

Share this post


Link to post
Share on other sites
Mengh.    759
Mengh.
4 hours ago, Lonster_Monster said:

I have one more question i am working on the json that i have in the project on github but when i call it on the sell prices it has an error

Undefined index: sell 

here is what i am doing

$pdefindex = $prices['app_data']['def_index']['quality'];
$sellprice = $prices['app_data']['def_index']['quality']['sell'];

 

Using your prices.json file, you have more keys you still have yet to put in.

 

https://github.com/LonsterMonster/json-view/blob/master/prices.json

 

From how you did your json, it should be:

$sellprice = $prices['app_data']['def_index'][$defindex]['quality'][$quality]['price']['sell']

 

I think you may be using too many keys. You may want to think about restructuring your JSON.

 

Share this post


Link to post
Share on other sites
Lonster_Monster    2
Lonster_Monster

Ok thanks that is what i needed i tried putting a . before the variable and that had an error i didnt think of encasing it in the brackets and i have it based on the steam api so the quality call is the same and other stuff

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×