Jump to content

problem with server loop push php


Pedro Alves
 Share

Recommended Posts

I'm not getting by code to work The result should be 125
150
175
And this always gives  this 125 125 125 125

how i solve the problem
Follow the code

server.php

<?php

require_once ('configurations/config.php');

// arquivo cujo conteúdo será enviado ao cliente

//$dataFileName = 'data.txt';


$timestart=time();
$PDO = new PDO('mysql:host=localhost;dbname=game', 'root','');

if(isset($_POST ['timestamp'])){

    $timestamp=$_POST [ 'timestamp' ];

}
else {
    $tempo=$PDO->prepare("SELECT NOW() as now");
    $tempo->execute();
    $row=$tempo->fetchObject();
    $timestamp=$row->now;
}
$sql = $PDO->prepare("SELECT cidade.ouro,cidade.madeira,cidade.metal,cidade.pedra,cidade.energia, cidade.comida,cidade.petrolio FROM users INNER JOIN cidade ON users.cod_user=cidade.cod_user WHERE cidade.timestamp>'$timestamp'AND users.username='admin';");

 $newdata = false;
 $notificacoes=array();
 while (!$newdata &&(time()-$timestart)<20) {
     $sql->execute();
 while ($row=$sql->fetchAll(PDO::FETCH_ASSOC)) {

     $notificacoes=$row;
        $newdata=true;
 }  
 usleep(500000);  
 } 
 $tempo=$PDO->prepare("SELECT NOW() as now");
    $tempo->execute();
    $row=$tempo->fetchObject();
    $timestamp=$row->now;
    $data= array('notificacoes'=>$notificacoes,'timestamp'=>$timestamp);
    echo json_encode($data);
    exit();
?>

client.js

$(function(){
notificacoes();
});
function notificacoes(timestamp){
    var data={};
    var resultado=0;
            var soma=2;
            var valor=25;
    if(typeof timestamp!='undefined')
    data.timestamp=timestamp;
    $.post('server.php',data,function(res){
        for(i in res.notificacoes ){

            resultado=+res.notificacoes[i].ouro+valor;

            $('#response').append(resultado);
        }

        notificacoes(res.timestamp);
    },'json');

}

ola.php

<html>

	<head>

		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

		<script type="text/javascript" src="dist/js/client.js"></script>

	</head>

	<body>

		<h3>Conteúdo</h3>

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

	
	</body>

</html>

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...