2017-08-31 122 views
0

我目前正在一个名为“tbl_users”的表中转发名为“followers_count”的列中的数据。该网站有几个用户,每个用户都有自己的页面。在这些页面上,用户可以点击一个关注按钮,并使用JSON显示跟踪计数。该代码迄今为止工作,除了它只显示/更新表中第一个“userID”的“followers_count”的数据。我的问题是,我将如何更改代码,以便它知道每个用户的页面显示他们的followers_count?PHP&JSON按用户显示列数据

在changes.php:

<?php 
require_once 'class.channel.php'; 

$user_change = new USER(); 

$seqFollows = $user_change->runQuery("SELECT followers_count FROM tbl_users"); 
$seqFollows->execute(); 
$row = $seqFollows->fetch(PDO::FETCH_ASSOC); 
$follow_count = $row['followers_count']; 

header('Content-type: application/json'); 
$array = array('followers_count'=>$follow_count); 
echo json_encode($array); 
?> 

在index.php的ID =(用户页面模板):

<div> 
    Channel Adds: <div id="follow_count" style="display:inline;"></div> 
</div> 

<script type="text/javascript"> 

var timer; 

timer = setInterval(function() { 

    $(document).ready(function(){ 
     $.getJSON('changes.php', function(data) { 
      $('#follow_count').html(data.followers_count); 
     }); 
     }); 

}, 1 * 1000); 

</script> 

此外,在ID的index.php =,这是代码这决定了我目前正在查看的页面:

$currentID = (isset($_GET['id']) && !empty($_GET['id'])) ? trim($_GET['id']) : '' ; 

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE userID=:uid"); 
$stmt->execute(array(":uid"=>$currentID)); 
$row = $stmt->fetch(PDO::FETCH_ASSOC); 

回答

2

您需要告诉changes.php用户标识值是什么。你将需要修改你的index.php文件来提供这个值。一种选择是类似的修改AJAX调用:

​​

然后你changes.php文件将需要得到的$_GET['id']一样的index.php值。

+0

谢谢你,完美的工作:) – Cordell