PHP Parameter to Hype

If you use PHP, you can just write a JSON object into the page containing an arbitrary depth of information:

Example writing complex data to a variable in window called data:

<?php

/* Do what ever is needed to retrieve the data from your datasource (like a DB etc.). This example assumes you have the final complex data structure in $data. You can now just write it directly into your page like the example of Jonathan */

// some data for testing
$data = array (
    array("Volvo",22,18),
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15)
);

// encode data
$dataJSON = json_encode($data);

echo "<script>window.data=JSON.parse('".$dataJSON."');console.log(window.data)</script>";


Example writing complex data to HypeDataMagic for usage in Hype:
Here is a version for Hype Data Magic usage (assuming you have it included through JSDeliver or directly in through the resource library. If you're rendering the page yourself, just include it in your <head> manually.

<?php

/* Do what ever is needed to retrieve the data from your datasource (like a DB etc.). This example assumes you have the final complex data structure in $data. You can now just write it directly into your page like the example of Jonathan */

// some data for testing
$data = array (
    array("Volvo",22,18),
    array("BMW",15,13),
    array("Saab",5,2),
    array("Land Rover",17,15)
);

// encode data
$dataJSON = json_encode($data);

echo "<script>HypeDataMagic.setData(JSON.parse('".$dataJSON."'))</script>";


Example of using fetch in HypeDataMagic:
Then there is also the possibility to fetch the data from a source providing direct JSON. This adds an asynchronous call to rendering the Hype file, though. This example doesn't need the above PHP, but assumes you're getting the data from a dedicated API endpoint rending JSON.

1 Like