douqihou7537 2013-04-22 20:42
浏览 30
已采纳

虽然循环不会结束[关闭]

I am running a text based game and trying to get a function to repeat itself using a while loop but when the condition is met it wont stop.

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

The function is supposed to run until the steps value is 0 which when the function runs once it subtracts from the total. yet it doesnt stop when it hits 0 and i get something like this "Steps: -15535"

i believe this is the "relivent code"

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条回答 默认 最新

  • douzi0609 2013-04-22 20:44
    关注
    $ir['steps']
    

    is not changed/decremented anywhere.

    Decrement it before at the end of the loop:

    $ir['steps'] -= 1;
    

    Just before the closing bracket of the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件