Jump to content

Need help with a script


bazrim11

Recommended Posts

Im making a game while trying to learn php and as a way to augment weapons and armour im adding gems to the game rubys diamonds emeralds and crystals and im making super gems (more poewerful) and master gems (most powerful)

 

and using this script im converting regular  gems to super gems i got rubys to do it fairly easy but when i started trying to do the diamonds it just tell me i dont have neough gems though i have 1 million in my database. I need help figuring out why this isnt working i tried using if statements and a switch and this is my current code for the converter its pretty simeple but im just missing something.

 

<?
$sruby='super ruby';
$gem=$_POST['start'];
$type=$_POST['finish'];
$result=mysql_query("SELECT * FROM items WHERE username='".$_SESSION['username']."'");
while($row = mysql_fetch_array($result))
$ruby=$row['ruby'];
$diamond=$row['diamond'];
$emerald=$row['emerald'];
$crystal=$row['crystal'];
 
switch (true) {
    case $gem==ruby:
        if($type=='super' && $ruby>='500'){
 
 
 
 
mysql_query("UPDATE items SET ruby=ruby-500 WHERE username='".$_SESSION['username']."'");
mysql_query("UPDATE items SET sruby=sruby+1 WHERE username='".$_SESSION['username']."'");
 
echo('You have just converted your 500 rubies to 1 super ruby');
 
}
else{
echo('You do not have enough rubies');
 
 
 
}
        break;
    case $gem==diamond:
        if($type=='super' && $diamond>='500'){
 
 
 
 
mysql_query("UPDATE items SET sdiamond=sdiamond+1");
Echo("YOu converted 500 diamonds to 1 super diamond");
 
}
else{echo("You do not have enough diamonds");}
 
        break;
    
}
 
 
 
 
 
 
 
 
 
?>
 
Link to comment
https://forums.phpfreaks.com/topic/285432-need-help-with-a-script/
Share on other sites

while($row = mysql_fetch_array($result))
$ruby=$row['ruby'];
$diamond=$row['diamond'];
$emerald=$row['emerald'];
$crystal=$row['crystal'];
If you don't use {}s on a loop then only the next statement will be executed in the loop. Like

while($row = mysql_fetch_array($result)) {
    $ruby=$row['ruby'];
}
$diamond=$row['diamond'];
$emerald=$row['emerald'];
$crystal=$row['crystal'];
After the loop $row will be false (it read everything) and you won't have any diamonds, emeralds, or crystals.

 

In your case, forget the loop. There's only one row.

$row = mysql_fetch_array($result);
$ruby=$row['ruby'];
$diamond=$row['diamond'];
$emerald=$row['emerald'];
$crystal=$row['crystal'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.