PHP Parameter to Hype

Maybe a dumb question: I want to use Hype for an animation that should take place on our intranet. The website "knows" who is using the page - and I want to transfer that information (ID of the user) to the Hype-Animation, so that personalized information is loaded via Ajax for that user.

Thanks for helping me to find the best way to do this.

You can fetch the data from a database using Json or just write it into the page when rendering it. Either way using Hype Data Magic should make it very easy.

To give a basic overview:

So it sounds like you already have a .php page that contains a Hype animation?

Typically the PHP code would write out some javascript that will store the User ID in some variable. Are you doing this part already? It would be something vaguely equivalent to:

<?php
echo "<script>\n";
echo "var userID = \"".$userID."\";\n";
echo "</script>\n";
?>

So given that, you could then generate an AJAX request using the javascript userID variable from above. You'd probably want to do this early on; I'd recommend using the first scene's On Scene Load event, since this makes it a bit easier to talk to the Hype document.

In the response handler, then you could store any responses, and also set values within elements on the current scene (making sure to set Unique Element IDs or Class Names on them) or invoke specific Hype API functions. This part depends a bit more on what you want to do and your overall scene setup.

There's of course many different ways to go about it, but this is a general flow.

Tools like @MaxZieb's mentioned Hype Data Magic also help a lot with the mechanics of getting data in/out.

1 Like

Thank you very much. This helped me a lot - and I'll have a look into DataMagic which sounds really promising ...

2 Likes

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

Herzlichen Dank für die Hilfe.

2 Likes