PHP inside SCRIPT tag to display JSON array.. help me!

Hi guys,

I would really love to get some advice here. I am using a live currency exchange API i’v created a foreach loop
to display every single CURRENCY inside an array. I would like to know if i am doing this smart… or if there is one better way on how to do it… i would really appreciate it if somebody could review my code for real.

<!doctype HTML>
<html>
<head>
<title>Currency Exchange</title>


<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

<style>
  
  .container {
	max-width: 992px;
  }
  
  .h1 {
	text-align: center;
  }
  
  .ce-wrapper {	
	margin: 20px auto;
	background-color: #F7F7F7;
	border: 1px solid #F4F3F3;
	padding: 20px;
  }
</style>

</head>

<body>


  <div class="container ce-wrapper">
	<div class="row">
		<div class="col-md-8 col-md-offset-2">
		  <h1 class="h1">Wisselkoers</h1>
		  
		  <!-- Currency Exchange -->
		  <?php 
		  // set API Endpoint and access key (and any options of your choice)
		  $endpoint = 'live'; // Live modus
		  $access_key = 'REMOVED_THIS_FOR_SECURITY_REASONS'; // Access Key ID
		  $valuta = 'EUR,GBP,CAD,PLN,AWG,YTL,HRK'; // Beschikbare valuta's

		  // Initialize CURL:
		  $ch = curl_init('http://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&currencies='.$valuta.'');
		  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		  // Store the data:
		  $json = curl_exec($ch);
		  curl_close($ch);

		  // Decode JSON response:
		  $exchangeRates = json_decode($json, true);
		  ?>
		  	
<!--
		  <pre>
		  	<?//php print_r($exchangeRates); ?>
		 </pre>	
-->
			<?php foreach($exchangeRates['quotes'] as $key => $rate) { ?>
  				<?php echo "$key:" . $rate . "<br>"; ?>
  			<?php } ?>
		  
		  	<br>
		  
		  	<?php $initJSON = json_encode($exchangeRates['quotes'], JSON_PRETTY_PRINT); 
		  	echo $initJSON;
		  	?>
		  
		  <script id="initJSON" type="application/json">
		  	<?php echo $initJSON; ?>
		  </script>
		  
	  	</div>
	</div>
  </div>


<!-- JS -->
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>		
</body>

</html>

now… i am wondering if there is a better way on how to DISPLAY a JSON output inside a SCRIPT TAG… without using PHP…

i do get this as result and no errors at all:

		  <script id="initJSON" type="application/json">
		  	{
    "USDEUR": 0.879121,
    "USDGBP": 0.691922,
    "USDCAD": 1.2842,
    "USDPLN": 3.87965,
    "USDAWG": 1.79,
    "USDHRK": 6.59435
}		  </script>

Thanks in advance!!!

That’s fine. Though you will want to assign the object literal to a variable.

$initJSON = 'var myData = '.json_encode($exchangeRates['quotes'], JSON_PRETTY_PRINT).';';

So the best practice is to replace your code with a part of my code.

So there is nothing wrong with what i am doing is that correct?

Thanks!

There isn’t much there to critique.

I’m sure if I saw that entire php file it would be different. The first thing I would probably recommend is not mixing application with display logic.

1 Like

Can you please… show me how you would do it… why i am asking this… i really want to become better.

Its difficult to discuss separating business, application, and display logic without talking about MVC which leads to a whole other conversation. It kind of starts with using a well known framework like Symfony or Laravel that provides all the boiller plate code for separating concerns using common design patterns. You could write the logic yourself but I think only fools write their own frameworks considering the richness of the PHP ecosystem. Lacking a framework and modern design practices your code is only going to be so good no matter how you write it.

1 Like

I am actually building wordpress themes with a starter theme Underscores and working with the codex.

But for the real deal you suggest me to use Symfony or Laravel? Can i also use this inside WordPress?

And which one is better?

Oh…

Well WordPress is the wild wild west. It doesn’t really follow any modern design practices. From an architectural stand-point it is not a good example of anything.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.