2013-04-22 185 views
-3

我正在运行一个基于文本的游戏,并试图让一个函数重复使用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”

我相信这是“relivent代码”

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

请只发布相关的代码。 – 2013-04-22 20:43:34

+0

请将您的代码显着减少到仅面临的问题以及需要重新编码的代码 – 2013-04-22 20:44:37

+3

那么$ ir ['steps']的位置在循环中发生了什么变化?我在穿过它后无法看到任何地方 – 2013-04-22 20:45:48

回答

2
$ir['steps'] 

没有改变/随时随地递减。前

递减它在循环的末尾:

$ir['steps'] -= 1; 

环路的闭合支架之前。

+0

mysql_query(“UPDATE users SET crystals crystals = crystals + $ rewardtok,steps = steps-1 WHERE userid = $ userid”); – 2013-04-22 20:48:35

+0

是的,你正在更新它在数据库中,但不是在PHP循环本身。你应该在循环中做和数据库一样的事情。如果您更改数据库中的数据,PHP将不会意识到自己的更改。你应该编写代码来这样做。 – beerwin 2013-04-22 20:49:46

+0

ohh im对不起,我在这个新的。我会怎么做? – 2013-04-22 20:51:24