Php 而循环不会结束

Php 而循环不会结束,php,while-loop,Php,While Loop,我正在运行一个基于文本的游戏,试图让一个函数使用while循环来重复自己,但当条件满足时,它不会停止 <?php /****************************************************************/ /*********************** Created By Seker ***********************/ /**** Feel free to modify in any way, but keep this sect

我正在运行一个基于文本的游戏,试图让一个函数使用while循环来重复自己,但当条件满足时,它不会停止

<?php
/****************************************************************/
/*********************** Created By Seker ***********************/
/**** Feel free to modify in any way, but keep this section. ****/
/****************************************************************/
session_start();
require "global_func.php";
if ($_SESSION['loggedin'] == 0)
{
header("Location: login.php");
exit;
}
$userid = $_SESSION['userid'];
require "header.php";
$h = new headers;
$h->startheaders();
include "mysql.php";
global $c;
$is =
    mysql_query(
            "SELECT u.*,us.* FROM users u LEFT JOIN userstats us ON u.userid=us.userid WHERE u.userid=$userid",
            $c) or die(mysql_error());
$ir = mysql_fetch_array($is);
check_level();
$fm = money_formatter($ir['money']);
$cm = money_formatter($ir['crystals'], '');
$lv = date('F j, Y, g:i a', $ir['laston']);
$h->userdata($ir, $lv, $fm, $cm);
$h->menuarea();

switch ($_GET['action'])
{
case 'walk':
walk();
break;

case 'walk2':
 walk();
 break;

default:
home();
break;
}

function home()
{


global $ir,$h,$c,$userid;

$q=mysql_query("SELECT u.*,c.* FROM users u LEFT JOIN cities c ON u.location=c.cityid WHERE u.userid=$userid");
$r=mysql_fetch_array($q);

$city = $r['cityname'];
$trav = $ir['steps'];

echo "
    <table width='70%' border='1'>
        <tr>
            <td align='center'>
                <font size='3'><b><u>The $city Streets</u></b></font>
            </td>
        </tr>
        <tr>
            <td align='center'>
                Take your chances with a walk through the $city streets!
            </td>
        </tr>
        <tr>
            <td align='center'>
                Each day, you get <b>10</b> steps! <b>25</b> if you are a donator!
            </td>
        </tr>
        <tr>
            <td align='center'>
                While traveling, you can find crystals, cash, or even gain experience!
            </td>
        </tr>
        <tr>
            <td align='center'>
                But, be careful! There are dangers in the streets!
            </td>
        </tr>
    </table>
";

echo "
    <table width='70%' border='1'>
        <tr>
            <td align='center' colspan='2'>
                <font size='3'><b><u>What would you like to do?</u></b></font>
            </td>
        </tr>
        <tr>
            <td align='center' colspan='2'>
                You can travel <b>{$trav}</b> more times today!
            </td>
        </tr>
            <td align='center' width='50%'>
                <a href='streets.php?action=walk'>[Go For A Walk]</a>
            </td>
            <td align='center' width='50%'>
                <a href='index.php'>[Change Your Mind]</a>
            </td>
        </tr>
    </table>
";
}



function walk()
{
do
{


global $ir,$h,$c,$userid;

$q=mysql_query("SELECT u.*,c.* FROM users u LEFT JOIN cities c ON u.location=c.cityid WHERE u.userid=$userid");
$r=mysql_fetch_array($q);

$chance = mt_rand(1,100);
$city = $r['cityname'];
$trav = $ir['steps'];

if ($ir['steps'] <= 0)
{
    echo "
        You have traveled enough for today! Come back tomorrow!
        <a href='index.php'>Home</a>";
        exit($h->endpage());
}

if ($ir['hospital'])
{
    echo "
        You cannot travel while in the hospital!
        <a href='index.php'>Home</a>";
        exit($h->endpage());
}

if ($ir['jail'])
{
    echo "
        You cannot travel while in jail!
        <a href='index.php'>Home</a>";
        exit($h->endpage());
}


echo "
    <table width=70% border='1'>
        <tr>
            <td align='center' width='50%'>
                <font size='2'>You start your journey through the $city streets!</font>
            </td>
            <td align='center' width='50%'>
                <font size='2'>You can travel up to <b>{$trav}</b> more times today!</font>
            </td>
        </tr>
        <tr>";

if ($chance <= 10)
{
    $rewardtok = mt_rand(5,15);

    mysql_query("UPDATE users SET crystals=crystals+$rewardtok,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You found <b>$rewardtok</b> crystals while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
}

if (($chance >= 15) && ($chance <= 30))
{
    $rewardg = mt_rand(100,5000);
    $reward = money_formatter($rewardg);

    mysql_query("UPDATE users SET money=money+$rewardg,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You found <b>$reward</b> while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
}


if (($chance >= 40) && ($chance <= 50))
{
    $rewardexp = mt_rand(5,50) * ($ir['level'] /2);

    mysql_query("UPDATE users SET exp=exp+$rewardexp,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You gained <b>$rewardexp<b> experience while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
}

if (($chance >= 55) && ($chance <= 65))
{
    $rewardexp = mt_rand(5,50);

    mysql_query("UPDATE users SET exp=exp+$rewardexp,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You gained <b>$rewardexp<b> experience while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
        }

 if (($chance >= 75) && ($chance <= 85))
    {
     $rewardtok = mt_rand(5,15);

    mysql_query("UPDATE users SET crystals=crystals+$rewardtok,steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    Congratulations! You found <b>$rewardtok</b> crystals while walking through the streets!
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";
 }
if ((($chance > 10) && ($chance < 15)) || (($chance > 30) && ($chance < 40)) || (($chance > 50) && ($chance < 55)) || (($chance > 65) && ($chance < 75)) || ($chance > 85))
{
     $rewardtok = mt_rand(5,15);
      mysql_query("UPDATE users SET steps=steps-1 WHERE userid=$userid");

    echo "
                <td align='center' colspan='2'>
                    You found nothing while walking through the streets
                </td>
            </tr>
            <tr>
                <td align='center' width='50%'>
                    <a href='streets.php?action=walk'><b>[Continue Traveling]</b></a>
                </td>
                <td align='center' width='50%'>
                    <a href='streets.php'><b>[Back]</b></a>
                </td>
            </tr>
        </table>";


  }

 }
while ($ir['steps'] > 0);
}
$h->endpage();

不会在任何地方更改/减少

在循环结束前将其递减:

$ir['steps'] -= 1;

就在循环的结束括号之前。

请仅发布相关代码。请将代码显著减少到您面临的问题以及复制它所需的代码,以便在循环中更改$ir['steps']?在浏览了itmysql_查询之后,我看不到任何地方(“更新用户集crystals=crystals+$rewardtok,steps=steps-1,其中userid=$userid”);这包括在每个结果中,我不会说这个问题因为包含HTML而被“JavaScript”或“HTML”标记,也不会因为它是一个函数而被称为“函数”。mysql_查询(“更新用户集crystals=crystals+$rewardtok,steps=steps-1,其中userid=$userid”);是的,您正在数据库中更新它,但不是在php循环本身中。您应该在循环中执行与在数据库中相同的操作。如果您更改了数据库中的数据,PHP将不会自己意识到这些更改。你应该写代码这样做。哦,对不起,我是新来的。我该怎么做?如果答案有效,就接受吧,很高兴我能帮助你。
$ir['steps']
$ir['steps'] -= 1;