Show result of a PHP file in HTML

2

I'm 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 numerical value that I want to display in a cell of a table. Unlike other php files that can be executed when I click on a button, I need the result that my php shows to be shown as soon as the page is loaded.

    
asked by Lucas. D 05.06.2016 в 23:28
source

4 answers

3

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 information of the stock (total amount of products) that is obtained through the api. They handle a series of inconveniences on whether to show the result in an html file or in 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:

   function contar_item($param, $array){
      $count = 0;
      foreach($myarray['result']['items'] as $item){
         if($item['defindex'] == $param){ 
           $count++;
         }
       }
       return $count;
    }

And when the php script is running, call that function, for example:

<?php
    for ($i=0; $i<$array_local; $i++){
     echo "<a href='#' class='item' data-id='".$i."'> ".contar_item($i,$array)." </a>";
    }
?>

Note that $ array_local can be an integer, or an array. If it is an array you can use the foreach and use the indexes for the parameters.

If it is html, you must make an ajax call for each item and pass as a parameter the product identifier (or the value that represents the number "5021" in your code). If this is the case, the foreach must do from javascript, for example using jquery, you would have something like this:

$( "a.item" ).each(function() {
  var id_item = $(this).data('item');
  var node = $(this);
   $.ajax({
            type:"POST",
            url:"llamadaAPI.php",
            data:"item="+id_item,
            success: function(data){$(node).html(data);}
        });
});

calledAPI.php will receive a POST called item, which will have to be used as a search parameter. At the end you should print the counter.

This is assuming that the api does not accept more filtering parameters, as I have worked with this type of responses, you can send more filtering parameters. Something like this:

link

Where parametro1 can be the catalog, and parameter2 the id of the product.

In this last case, if you want to make the answer less heavy, I recommend that you section the total of products, and whether you are loading more automatically when doing a scroll, put a "load more" button, or use pagination .

    
answered by 05.09.2016 / 06:54
source
1

Assuming that you have an index.php file created, running on a server that supports php, then run that file in the browser and it will be shown to you automatically, for example:

<?php
//El siguiente array simula la llamada al api que mencionas.
$myarray = array(
                 "result" => 
                        array( "items"=>
                                array(1,2,3)
                             ) 

                );

//Aqui genero una tabla para mostrar los datos del arreglo, 
//esto lo puedes editar a tu conveniencia.
$table = "<table border='1'>";
$table .= "<tr>";
$table .= "<th>Item nro</th>";
$table .= "</tr>";
foreach($myarray['result']['items'] as $item)
{   
    $table .= "<tr>";
    $table .= "<td>$item</td>";
    $table .= "</tr>";

}

$table .= "<table border='1'>";

echo $table;
?>

I hope I have helped you.

    
answered by 06.06.2016 в 19:29
0

Imports the code of the 'php function' which gives the value you want to show in your other file where you want to display it.

use /* Aquí va la dirección de tu archivo con la función. */

Simply to show the value, use the function echo that provides php.

It is important to emphasize that there is no way to execute PHP code in an HTML file , unless you are using a template system such as Blade.

    
answered by 06.06.2016 в 17:36
0

In an html file it would be impossible to show the result.

You would have to pass your .html file to .php and at the beginning of this do an include.

<?php    
include('tufuncion.php');
?>

So your index.php would load your api function and you would only have to open php tags and do an echo in the cell where you want to show the number.

If you did not want to pass the file to .php I think you would only have the option of doing it with ajax.

    
answered by 06.06.2016 в 18:21