2016-10-06 2 views
0

私は2つのファイル、index.phpとget_data.phpを持っていると言う。get_dataで私のDBからのデータを出力するループは、私のDBからユーザー名、画像、アップロード後の画像の秒数/タイマーと言う。インデックスでは、whileループからの出力を表示するためにget_feedを呼び出す必要があります。残りのデータではなく#timerというタイマーdivをリフレッシュするだけです。これをどのように行うことができますか?今のところ私はすべてのコンテンツを、index.phpの#responseに出力している画像をすべてリフレッシュすることができます。この画像は、画像を更新して、画像をリフレッシュするので、多くの画像が表示され、パフォーマンスが低下します。ajaxでphpファイルから呼び出されている外部divを更新するにはどうすればよいですか?

index。 PHP -

<div id="response"></div> 

repeatAjax(); 
function repeatAjax(){ 
    $.ajax({  
    type: "GET", 
    url: "get_feed.php",    
    dataType: "html",     
    success: function(response){      
     $("#response").html(response); 
    }, 
     complete: function() { 
      setTimeout(repeatAjax,1000); 
     } 

    }); 
}; 

get_feed.php -

$today_date = date('Y-m-d H:i:s'); 
$sql = "SELECT image, user, date FROM images"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 

while($row = $result->fetch_assoc()) { 
    $image = $row['image']; 
    $user = $row['user']; 
    $date = $row['date']; 

    $timeFirst = strtotime($today_date); 
    $timeSecond = strtotime($date); 
    $timeSecond = $timeSecond + 86400; 
    $timer = $timeSecond - $timeFirst; 

    echo '<div id="timer">'.$timer.'</div>'; 
    echo '<div id="user">'.$user.'</div>'; 
    echo '<img src="'.$image.'" id="image"'; 
} 
} else { 
echo "0 results"; 
} 

私はちょうどこの例のためにそれを書いたが、上記のスクリプトでエラーがあるかもしれませんが、私はちょうどリフレッシュすることなく毎秒のタイマーをリフレッシュされるためつもりですイメージまたはユーザー名また、サイトに多くの人がいると、サーバが毎秒更新する負荷を処理できない場合、私のサイトがクラッシュする心配です。

ありがとう!

+0

要件制約が許せば、JavaScriptを使ってタイマーを表示することもできます。 –

+0

どうすればこのことになるでしょうか?私はそれを間違って説明しているかもしれないと思うかもしれませんが、ここに例があります - Facebookのようなボタン、そのボタンが青くなり、あなたのようなものが合計に追加され、リアルタイムで更新されますが、私は何もクリックしていないことを知っているが、それは同じ考えのように思える、@Rohith –

答えて

1

これは役に立ちます。

あなたに

success: function(response, status) { 
     $("#response").html(response); 
    }, 

success: function(response){      
     $("#response").html(response); 
    }, 

を交換してみてくださいは

あなたの修正get_feed.phpをindex.phpの上部の2つの異なるHTML応答

$timer=$_GET['tm']; 
    $today_date = date('Y-m-d H:i:s'); 
    $sql = "SELECT image, user, date FROM images"; 
    $result = $conn->query($sql); 

    if ($result->num_rows > 0) { 

    while($row = $result->fetch_assoc()) { 
     $image = $row['image']; 
     $user = $row['user']; 
     $date = $row['date']; 

     $timeFirst = strtotime($today_date); 
     $timeSecond = strtotime($date); 
     $timeSecond = $timeSecond + 86400; 
     $timer = $timeSecond - $timeFirst; 

     if($timer) 
     { 
     echo '<div id="timer">'.$timer.'</div>'; 
    } 
    else 
    {  
     echo '<div id="user">'.$user.'</div>'; 
     echo '<img src="'.$image.'" id="image"'; 
    } 

    } 
    } else { 
    echo "0 results"; 
    } 

を提供ために、 Yあなたは「はYmd Hを使用しているため、代わりにあなたは日付のwhere句を使用する必要があり、あなたは、クエリからすべてのデータを取得するべきではないの重複を避けるために、2つの異なるHTML応答

<div id="response_wrapper"> 
<div id="timer_response"></div> 
<div id="remaining_response"></div> 
</div> 

repeatAjax(); 
function repeatAjax(){ 
    $.ajax({  
    type: "GET", 
    url: "get_feed.php",    
    dataType: "html",     
    success: function(response, status) {      
     $("#remaining_response").html(response); 
    }, 
     complete: function() { 
      setTimeout(repeatAjax,3000);//comment this line of code if you want to prevent further refreshing of image and username 
     } 

    }); 
}; 
repeatAjax2(); 
    function repeatAjax2(){ 
     $.ajax({  
     type: "GET", 
     url: "get_feed.php?&tm=true",    
     dataType: "html",     
     success: function(response, status) {      
      $("#timer_response").html(response); 
     }, 
      complete: function() { 
       setTimeout(repeatAjax2,1000); 
      } 

     }); 
    }; 
+0

私はエラーが表示されます - Uncaught TypeError:response.textは関数ではありません –

+0

私は答えを編集しました。今すぐお試しください –

+0

ありがとうございますが、まだdiv #responseを画像 –

0

に対処する私たちの変更のindex.php: I:日付のs'の形式とJSでちょうどget_feed.phpにdiv要素

を追加します。

if(!empty($alreadyAccess)){ 
    $today_date = date('Y-m-d H:i:s'); 
    $sql = "SELECT image, user, date FROM images"; 
} 
else{ 
    $sql = "SELECT image, user, date FROM images where date '$last_access_date'"; 
} 

のindex.phpで.appendするの.htmlに変更します:

$("#response").append(response); 
+0

ありがとうございます。ここでは、すべての画像にはその画像の寿命を示すタイマーがありますので、各画像、ユーザー名、タイマーのフィードを取得したいのですが、データベースを初めて呼び出すだけで各画像のタイマーを更新します。一度私はデータベースから日付を引っ張って、私はタイマーを実行することができるはずです。 –

関連する問題