1

On my PHP script, I have an array similar to this:

$panorama = array(
    "default" => array(
        "firstScene" => 2,
        "author" => 'Felipe'
    ), 
    "scenes" => array(
        "circle" => array(
            "title" => "Title 1", 
            "hotSpots" => array(
                "pitch" => "-2.1",
                "createTooltipFunc" => "hotspot"
            )
        )
    )
);

This array will be passed to a Javascript funcion, as a json object, like this:

pannellum.viewer('panorama', <?php echo json_encode($panorama); ?>);

The parameter createTooltipFunc have to receive a callback function named hotspot. So, in the final json, when I pass this PHP array to the Javascript function, this parameter should be like this: "createTooltipFunc" : hotspot, without the double quotes. How do I do that?

Just to give more information, I'm trying to create a tour using the plugin pannellum.js and I'm geting all the info of the json I need from my mysql DB.

miken32
  • 42,008
  • 16
  • 111
  • 154
churros
  • 419
  • 1
  • 8
  • 20

2 Answers2

2

Keep a Javascript variable in between which keeps the object and update the property before it's using with pannellum(Assumes hotspot is a variable).

//  create Javascript object
var obj = <?php echo json_encode($panorama); ?>;

// update createTooltipFunc proeprty with the variable
// where variable name can be extract from $panorama
obj.scenes.circle.hotSpots.createTooltipFunc = <?php echo $panorama['scenes']['circle']['hotSpots']['createTooltipFunc']; ?>;

pannellum.viewer('panorama', obj);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Following Pranav suggestion, I had to put the PHP array into a javascript object and loop through it to find the right node, and then replace the old value for the new, something like this:

var objTour = <?php echo json_encode($panorama); ?>;
            for (var [key, value] of Object.entries(objTour.scenes)) {
                var objdadosScene = value;
                for (var [key, value] of Object.entries(objdadosScene)) {
                    if(key == 'hotSpots'){
                        var objHotspot = objdadosScene.hotSpots;
                        for (var [key, value] of Object.entries(objHotspot)) {
                            var objDadosHotspot = value;
                            for (var [key, value] of Object.entries(objDadosHotspot)) {                             
                                objDadosHotspot['createTooltipFunc'] = hotspot;
                            }
                        }
                    }
                }
            }

I don't know if this was the best way to achieve the result, but it's working. Thanks!

churros
  • 419
  • 1
  • 8
  • 20