Mils
Mils

Reputation: 1498

The content does not change in my table

I load data from an xml file (file.xml) whose content is dynamic, but the problem the content in my table is the same but I see in my file the content changes. As if the loading is done once.

Please help.

Thank you

A peace of code : (sorry if you find a problem in my code)

   window.setInterval(function() { 
        <?php 
        $xml = simplexml_load_file("file.xml");
        foreach($xml as $node)
        {
            $name = "";
            $value = -1;

            foreach($node->attributes() as $a => $b) {
                if($a == "name")
                {
                    $name = (string)$b;
                }
                else if($a == "value")
                {
                $value = (string)$b;
                }
            }

            $vars[$name] = $value;
        }

        $json = json_encode($vars);
        ?>

        //// code to show the result in my table
    }, 1000);

Upvotes: 0

Views: 71

Answers (1)

Bill Effin Murray
Bill Effin Murray

Reputation: 436

PHP stands for PRE hypertext processing, meaning the PHP is generated first and foremost.

Also you need to understand that PHP is processed on the server side while javascript is processed on the client side.

So when your PHP function is run, it will load the file as intended but will only load it once. You need to put your PHP function in a .php file of its own and then use an AJAX request every second to get the updated table.

Example in jQuery, given your PHP code is in loadtable.php

window.setInterval(function() {
    $.ajax({
        method : "GET",
        url : "loadtable.php",
        success : function(data){
            //set the body of the page to the result of the PHP file.
            $("body").html(data);
        }
    });
}, 1000);

Upvotes: 1

Related Questions