普通网友 2016-08-08 06:20
浏览 23
已采纳

用脚本重复的页面

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

  • 写回答

2条回答 默认 最新

  • doufu5747 2016-08-08 07:00
    关注
    $(document).ready (function () {
                refreshTable();
            });
            function refreshTable(){
                setTimeout (function () {
                    $('div#tabla').load ('index.php#tabla', 'update=true')
                    .always(refreshTable);
                }, 1000);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动