Home > Software engineering >  Data not showing in table
Data not showing in table

Time:01-04

I want to fetch past 1 month data of exchange rates and i am using fastforex.io api for the data. The data are not showing in table. Can anyone help me with it.

// Function to generate the exchange rate table
function exchange_rate_table_shortcode() {
  // Set the API endpoint URL
  $url = "https://api.fastforex.io/time-series?from=AED&to=USD&start=2022-05-11&end=2022-06-11&api_key=xxxxxxxx"; // api key on purpose
  
  // Send the request to the API endpoint
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
  curl_close($ch);
  
  // Decode the response
  $data = json_decode($response, true);
  
  // Start building the table HTML
  $table = "<table>";
  $table .= "<tr><th>Date</th><th>Exchange Rate</th></tr>";
  
  // Loop through the data and add a row to the table for each date
  foreach ($data as $date => $rate) {
    $table .= "<tr><td>" . $date . "</td><td>" . $rate . "</td></tr>";
  }
  
  // Finish building the table HTML
  $table .= "</table>";
  
  // Return the table HTML
  return $table;
}

// Register the shortcode
add_shortcode("exchange_rate_table", "exchange_rate_table_shortcode");

here's the api response

{
  "start": "2022-05-11",
  "end": "2022-06-11",
  "interval": "P1D",
  "base": "AED",
  "results": {
    "USD": {
      "2022-05-11": 0.27236,
      "2022-05-12": 0.27233,
      "2022-05-13": 0.27236,
      "2022-05-14": 0.27232,
      "2022-05-15": 0.27232,
      "2022-05-16": 0.27233,
 }
  },
  "ms": 7
}

CodePudding user response:

Basically your date & rate data in results of USD element.

So your foreach loop code look like below:

// Loop through the data and add a row to the table for each date
  foreach ($data['results']['USD'] as $date => $rate) {
    $table .= "<tr><td>" . $date . "</td><td>" . $rate . "</td></tr>";
  }

CodePudding user response:

Building it up with a static json:

        $json = '{
   "start":"2022-05-11",
   "end":"2022-06-11",
   "interval":"P1D",
   "base":"AED",
   "results":{
      "USD":{
         "2022-05-11":0.27236,
         "2022-05-12":0.27233,
         "2022-05-13":0.27236,
         "2022-05-14":0.27232,
         "2022-05-15":0.27232,
         "2022-05-17":0.27233
      }
   },
   "ms":7
}';

        $data = json_decode($json, true);

        // Start building the table HTML
        $table = "<table>";
        $table .= "<tr><th>Date</th><th>Exchange Rate</th></tr>";

        $rates = $data['results'];

        //Assuming that the currency rate could be anything or there could be multiple currency rates as well.
        // Loop through the data and add a row to the table for each date
        foreach ($rates as $currency => $dates) {
            foreach ($dates as $key => $value) {
                $table .= "<tr><td>" . $key . "</td><td>" . $value . "</td></tr>";

            }
            //$table .= "<tr><td>" . $date . "</td><td>" . $rate . "</td></tr>";
        }

// Finish building the table HTML
        $table .= "</table>";

        return $table;

Difference:

You were trying to display the array as a value, thus you might have gotten the following: Array to string conversion error.

Why is this code helpful? Because if in the case of tomorrow, the results include more than one key: "USD", "CAD", etc. Thus this code might be helpful for the use case.

  • Related