Pedro Alves Posted February 2, 2017 Share Posted February 2, 2017 I'm not getting by code to work The result should be 125150175And this always gives this 125 125 125 125 how i solve the problemFollow 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.