I am working with an API and I have the following code in php
<?php
$link = file_get_contents('http://api.steampowered.com/IEconItems_440/GetPlayerItems/v0001/?key=APIKEY&SteamID=STEAMID64&format=json');
$myarray = json_decode($link, true);
$count = 0;
foreach($myarray['result']['items'] as $item)
{
if($item['defindex'] == 5021)
{
$count++;
}
}
echo $count;
?>
What this will do is throw me a numeric value that I want to display in a table cell. Unlike other php files that can be executed when a button is clicked, I need the result that my php returns to be displayed as soon as the page loads.
From what I have been able to observe, the objective is that for each product that you have in your store, you want to show the stock information (total number of products) that is obtained through the api. They handle a number of issues on whether to display the result in an html file or a php file.
If it's php, the script can run faster by calling a function that counts the items, for example you could have a function like this:
And when the php script is executed, the function is called, for example:
Note that $array_local can be an integer itself, or an array. If it's an array you can use the foreach and use the indices for the parameters.
If it is html, an ajax call must be made for each item and pass the product identifier (or the value that represents the number "5021" in your code) as a parameter. If this is the case, the foreach must be done from javascript, for example using jquery, you would have something like this:
callAPI.php will receive a POST called item, which will have to be used as a search parameter. At the end the counter should be printed.
This assuming that the api does not accept more filter parameters, since from what I have worked with this type of response, more filter parameters can be sent to them. Something like this:
http://miweb.com/parametro1/parametro2/
Where parameter1 can be the catalog, and parameter2 the product id.
In this last case, if you want to make the answer less heavy, I recommend that you section the total products, and either load more automatically when you scroll, put a "load more" button, or use pagination.
Assuming you have an index.php file created, running on a server that supports php, then run that file in the browser and it will show up automatically, for example:
I hope I've helped.
Import the 'php function' code which returns the value you want to display into your other file where you want to display it.
Simply to display the value, use the function
echo
provided by php.It is important to note that there is no way to execute PHP code in an HTML file , unless you are using a template system like Blade.
In an html file it would be impossible to display the result.
You would have to pass your .html file to .php and at the beginning of it do an include.
So your index.php would load your api function and you would just have to open php tags and do an echo in the cell where you want to display the number.
If you don't want to pass the file to .php I think you only have the option to do it with ajax.