实时更新PHP内容

时间:2019-12-02 09:33:04

标签: php jquery database post include

我正在开发一个游戏,该游戏的统计信息栏是从header.php文件中提取的。它从数据库中提取其信息。然后,header.php文件将包含在每个页面的顶部,因为它还包含HTML的所有<head>部分。

我遇到的问题是,因为随后的PHP页面的脚本位于header.php的位置之下,所以当POST数据发送到服务器时,包括页面内容(例如“ units”) (例如,当招聘单位时)位于POST代码下方,因此会进行更新,但是header.php中包含的内容仅在刷新页面后才会更新,我认为这是因为它位于POST代码上方。

我无法将header.php文件移动到任何POST脚本下方。

我目前实现的“解决方案” 是使用jQuery分别刷新header.php文件中的数据的每个部分,该文件目前为25个部分-但可以想象,这真是太糟糕了,因为这意味着单个页面每秒在任何标准请求之上向服务器发送25个请求。从理论上讲,这款游戏可以容纳数百万名玩家,所以这根本不是一个可行的解决方案。

是否有任何解决方案可以确保在进行POST时更新此数据,而不必使用jQuery向服务器发送恒定的请求流?

我在下面包含了当前使用的代码:

<?php
                if(isset($_SESSION['uid'])){
                include("safe.php");
                include("cron_update_stats.php");
                    $currenttime = time();
                    $time = date('i:s', (10 * 60) - ($currenttime % (10 * 60)));
                    $bankgold_r = number_format($stats['bankgold']);
                    $bankfood_r = number_format($stats['bankfood']);
                    $gold_r = number_format($stats['gold']);
                    $food_r = number_format($stats['food']);
                    $worker_r = number_format($unit['worker']);
                    $farmer_r = number_format($unit['farmer']);
                    $warrior_r = number_format($unit['warrior']);
                    $defender_r = number_format($unit['defender']);
                    $thief_r = number_format($unit['thief']);
                    $sword_r = number_format($weapon['sword']);
                    $shield_r = number_format($weapon['shield']);
                    $turns_r = number_format($stats['turns']);
            ?>

            <script>
                setInterval("my_function();",1000); 
                function my_function(){
                    $('#refresh_countdown_clock').load(location.href + ' #countdown_clock');
                    $('#refresh_bankgold').load(location.href + ' #bankgold');
                    $('#refresh_bankgold_m').load(location.href + ' #bankgold_m');
                    $('#refresh_bankfood').load(location.href + ' #bankfood');
                    $('#refresh_bankfood_m').load(location.href + ' #bankfood_m');
                    $('#refresh_gold').load(location.href + ' #gold');
                    $('#refresh_gold_m').load(location.href + ' #gold_m');
                    $('#refresh_food').load(location.href + ' #food');
                    $('#refresh_food_m').load(location.href + ' #food_m');
                    $('#refresh_worker').load(location.href + ' #worker');
                    $('#refresh_worker_m').load(location.href + ' #worker_m');
                    $('#refresh_farmer').load(location.href + ' #farmer');
                    $('#refresh_farmer_m').load(location.href + ' #farmer_m');
                    $('#refresh_warrior').load(location.href + ' #warrior');
                    $('#refresh_warrior_m').load(location.href + ' #warrior_m');
                    $('#refresh_defender').load(location.href + ' #defender');
                    $('#refresh_defender_m').load(location.href + ' #defender_m');
                    $('#refresh_thief').load(location.href + ' #thief');
                    $('#refresh_thief_m').load(location.href + ' #thief_m');
                    $('#refresh_sword').load(location.href + ' #sword');
                    $('#refresh_sword_m').load(location.href + ' #sword_m');
                    $('#refresh_shield').load(location.href + ' #shield');
                    $('#refresh_shield_m').load(location.href + ' #shield_m');
                    $('#refresh_turns').load(location.href + ' #turns');
                    $('#refresh_turns_m').load(location.href + ' #turns_m');
                }
            </script>

            <header class="sticky-top">     

        <div class="d-lg-none d-xl-none">
        <div class="container-flex pt-2 pb-2" style="background-color: #191919">
            <div class="container"> 
            <div class="justify-content-left text-light d-flex flex-column">
            <div class="pl-md-3 pr-md-3"><b class="text-primary">TURNS &raquo;</b> <span id="refresh_turns_m"><span id="turns_m"><?php echo $turns_r; ?> <?php if($stats['turns'] < 250){ echo "<small class='text-warning'>(+5 turns in ".$time.")</small>"; }else{ echo "<small class='text-danger'>(Max. turns)</small>"; }?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">BANK &raquo;</b> Gold: <span id="refresh_bankgold_m"><span id="bankgold_m"><?php echo $bankgold_r; ?></span></span> &#8901; Food: <span id="refresh_bankfood_m"><span id="bankfood_m"><?php echo $bankfood_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">RESOURCES &raquo;</b> Gold: <span id="refresh_gold_m"><span id="gold_m"><?php echo $gold_r; ?></span></span> &#8901; Food: <span id="refresh_food_m"><span id="food_m"><?php echo $food_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">INCOME &raquo;</b> Workers: <span id="refresh_worker_m"><span id="worker_m"><?php echo $worker_r; ?></span></span> &#8901; Farmers: <span id="refresh_farmer_m"><span id="farmer_m"><?php echo $farmer_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">BATTLE &raquo;</b> Warriors: <span id="refresh_warrior_m"><span id="warrior_m"><?php echo $warrior_r; ?></span></span> &#8901; Defenders: <span id="refresh_defender_m"><span id="defender_m"><?php echo $defender_r; ?></span></span> &#8901; Thieves: <span id="refresh_thief_m"><span id="thief_m"><?php echo $thief_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">WEAPONS &raquo;</b> Swords: <span id="refresh_sword_m"><span id="sword_m"><?php echo $sword_r; ?></span></span> &#8901; Shields: <span id="refresh_shield_m"><span id="shield_m"><?php echo $shield_r; ?></span></span></div>
            </div>
            </div>
        </div>
        </div>

        <div class="d-none d-lg-block">
        <div class="container-flex pt-3 pb-3" style="background-color: #191919">
            <div class="container"> 
            <div class="d-flex flex-wrap justify-content-left text-light">
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">TURNS &raquo;</b> <span id="refresh_turns"><span id="turns"><?php echo $turns_r; ?> <?php if($stats['turns'] < 250){ echo "<small class='text-warning'>(+5 turns in ".$time.")</small>"; }else{ echo "<small class='text-danger'>(Max. turns)</small>"; }?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">BANK &raquo;</b> Gold: <span id="refresh_bankgold"><span id="bankgold"><?php echo $bankgold_r; ?></span></span> &#8901; Food: <span id="refresh_bankfood"><span id="bankfood"><?php echo $bankfood_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">RESOURCES &raquo;</b> Gold: <span id="refresh_gold"><span id="gold"><?php echo $gold_r; ?></span></span> &#8901; Food: <span id="refresh_food"><span id="food"><?php echo $food_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">INCOME UNITS &raquo;</b> Workers: <span id="refresh_worker"><span id="worker"><?php echo $worker_r; ?></span></span> &#8901; Farmers: <span id="refresh_farmer"><span id="farmer"><?php echo $farmer_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">BATTLE UNITS &raquo;</b> Warriors: <span id="refresh_warrior"><span id="warrior"><?php echo $warrior_r; ?></span></span> &#8901; Defenders: <span id="refresh_defender"><span id="defender"><?php echo $defender_r; ?></span></span> &#8901; Thieves: <span id="refresh_thief"><span id="thief"><?php echo $thief_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">WEAPONS &raquo;</b> Swords: <span id="refresh_sword"><span id="sword"><?php echo $sword_r; ?></span></span> &#8901; Shields: <span id="refresh_shield"><span id="shield"><?php echo $shield_r; ?></span></span></div>
            </div>
            </div>
        </div>
        </div>

    </header>

0 个答案:

没有答案