用脚本重复的页面

用脚本重复的页面

问题描述:

I'm learning PHP for a little project, and with mysql I made a little table that updates itself every second from the mysql table.

<!Doctype html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
<title>Pagina de prueba Mysql</title>
<style type="text/css">
    table {
        padding: 10pt;
        margin:auto 10pt;
    }
    .present {
        background-color: green;
        font-size: 20pt;
    }
    .absent {
        background-color: red;
        font-size: 20pt;
    }
</style>
</head>
<body>
<h1>Prueba Mysql</h1>

<?php
    $servername = '192.168.0.101';
    $username = 'dpto';
    $password = 'n1nj4g41d3n';
    $dbname = 'dpto';
// Crear conexion
    $conn = new mysqli($servername, $username, $password, $dbname);
if ($conn -> connect_error) {
    die('Connection failed> ' . $conn->connect_error);
}
else {echo 'Connection succesfull';}
print '<br>';
// Seleccionar weas
$sql_Jose = 'Select presente from dpto where id=2';
$sql_Lucas = 'Select presente from dpto where id=1';
$Lucas = $conn->query($sql_Lucas)->fetch_object()->presente;
$Jose = $conn->query($sql_Jose)->fetch_object()->presente;
if ($Lucas == 0) {$_Lucas = "<td class='absent'> Lucas </td>";}
else {$_Lucas = "<td class='present'> Lucas </td>";}
if ($Jose == 0) {$_Jose = "<td class='absent'> Jose </td>";}
else {$_Jose = "<td class='present'> Jose </td>";}


?>
<div id='tabla'>
<table>
    <tr><?php echo $_Lucas ?></tr>
    <tr><?php echo $_Jose ?></tr>
</table>
</div>
<script type="text/javascript">
$(document).ready (function () {
    var updater = setTimeout (function () {
        $('div#tabla').load ('index.php#tabla', 'update=true');
    }, 1000);
});
</script>
</body>

The script at the end prints out a second "Prueba Mysql" and "Connection succesful", the table just prints once..
How can I make it so everything is just once on the page?? Thank you in advantage

$(document).ready (function () {
            refreshTable();
        });
        function refreshTable(){
            setTimeout (function () {
                $('div#tabla').load ('index.php#tabla', 'update=true')
                .always(refreshTable);
            }, 1000);
}

Actually the problem with below script, whenever page is loaded the setTimeout script is trigger and its reload the page.

So its looping again, put the validation to check url have redirected from the script.

<script type="text/javascript">
$(document).ready (function () {
    var updater = setTimeout (function () {
        $('div#tabla').load ('index.php#tabla', 'update=true');
    }, 1000);
});
</script>