虽然循环不会结束

时间:2013-04-22 20:42:19

标签: php while-loop

我正在运行基于文本的游戏并尝试使用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();

该函数应该运行,直到步骤值为0,当函数从总计中减去时运行。然而当它达到0并且我得到类似这样的东西时它不会停止“步骤:-15535”

我相信这是“重温代码”

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();

1 个答案:

答案 0 :(得分:2)

$ir['steps']

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

在循环结束之前减少它:

$ir['steps'] -= 1;

在循环的结束括号之前。