dongxiegao3071 2015-09-21 08:56
浏览 52

从包含的.php文件自动刷新表

I have been trying to get a table to refresh without the page being refreshed. I know this can be done with AJAX but I can't seem to get it working.
This is my situation:
I have a main.php page which has an include (table.php). This include contains a table id: tablevisit. This table is dynamically being made: it loops through every row in the database. This is table.php

echo "<div id='tableContainer'>";



$con = new mysqli("localhost","sample","samplepass","sample");  

$sql = "SELECT * FROM sampletable";
$i = 0;

$dyn_table = '<table border="1" cellpadding="10" class="visitorlist" id="tablevisit">'; 

$query = $con->query($sql);

while($row = $query->fetch_assoc()){
        $id = $row['ID'];
        $name = $row['address'];
        $date = $row['date'];
        $time = $row['time'];
        $url = $row['visit_url'];
        $urlstring = $row['visit_url'];


        if($i % 3 == 0){ 
            $dyn_table .= '<tr><td>'.$name.'</td>';
            $dyn_table .= '<td>'.$date.'</td>';
            $dyn_table .= '<td>'.$time.'</td>';
            $dyn_table .= '<td class="tdshort"><a href='.$url.' target = "_blank" class=" tdoverflow">' . $_SESSION['cuttedurl'] . '</a></td>';
            $dyn_table .= '<td><button type= "button" name = "' . $id . '"  class="dynamixbutton navbar-button btn-danger btn" post_id="' . $id . '" type="submit" >CHAT</button></td>';
            $dyn_table .= '<td>' . $id . '</td>';


        }
        else {
            $dyn_table .= '<tr><td>'.$name.'</td>';
            $dyn_table .= '<td>'.$date.'</td>';
            $dyn_table .= '<td>'.$time.'</td>';
            $dyn_table .= '<td><a href='.$url.' target = "_blank" class=" tdoverflow">' . $_SESSION['cuttedurl'] . '</a></td>';
            $dyn_table .= '<td><button type= "button" name = "' . $id . '" class="dynamixbutton navbar-button btn-danger btn" post_id="' . $id . '" type="submit">CHAT</button></td>';
            $dyn_table .= '<td>' . $id . '</td>';

        }
        $i++;

    }
$dyn_table .='</tr></table>'; 
echo "</div>";

On main.php echo $dyn_table; is called. Now, $dyn_table needs to be called every x seconds. How does one do this? I've tried just refreshing the table but I thought it would be better to just call $dyn_table every x seconds.
I've tried this, this and this but it did not work out for me. And those were html elements, not PHP variables. Any suggestions? Recommendations, tips?
Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douxiong2001 2015-09-21 09:38
    关注

    As far as i understand your question i believe you got no clue on how ajax works. Maybe this might help you.

    In pseudo code it looks like this:

    whenajaxrequestwassuccessfull (var tabledata = getDataFromServerViaAjax('http://URL of The PHP File which echos a json formated data string')) {
    
      var dataobject = convertjsontoobject(tabledata);
    
      loop over the table {
        tablerow->tablefield->setData(dataobject->somedetail)
    
      }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大