Jump to content

[SOLVED] Need Help..


Ambuu

Recommended Posts

Alright, So I am making a strategy game in php, and I haven't been able to get past this one part.

 

Here is the main form used for attacking:

 

echo "<form method='POST' action='do_battle.php'>";
echo "<b>Turns</b> (1 - 10): <input type='text' size='3' name='fld_turns'>";
echo "<input type='hidden' name='attackid' value='" . $userid . "'>";

 

In this form, you input a number of turns (1-10) and attack the person.

 

And here is the part that I'm stuck on at the moment:

 

  $sql = "SELECT stats_turns, stats_attack, stats_gold, stats_population FROM stats
          WHERE stats.id='$attacker";
  $result = mysql_query($sql);
  $stats1 = mysql_fetch_assoc($result);
  $turns = $stats1['stats_turns'];
  settype($turns, "int");
  settype($fld_turns, "int");
  if ($fld_turns <= 0 OR $fld_turns > 10) {
    $output = "You need to attack with between one and ten turns.";
    include("./inc/header.inc");
  }else{
    if ($fld_turns > $turns) {
      $output = "You don't have enough turns to attack.";
      include("./inc/header.inc");
    }

The main problem is taking the fld_turns value (from the battle.php form) and putting it into a positive integer. For some reason, everytime I put a number into the form (I've gone from 1 to over 100), the value of fld_turns is always 0. Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/118461-solved-need-help/
Share on other sites

What's with the settype() call?  PHP is a loosely typed language, so types are relatively insignificant, like say...C where you'd have something like:

 

#include <stdio.h>

int main(int argc, char** argv) {
      int fld_turns, turns;
      //etc etc
}

 

That's unnecessary in PHP.

Link to comment
https://forums.phpfreaks.com/topic/118461-solved-need-help/#findComment-609804
Share on other sites

what happens if you just echo $_POST[fld_turns]? Also how come you've set the input box for this to size 3, but if the value is 1 - 10 then it would only ever be 2? (I can't see that affecting anything, I'm just pinickity like that :P)

 

First off, I tried the echo part, and it came up as 10 (when I put ten in). So I just changed:

if ($fld_turns <= 0 OR $fld_turns > 10)

into:

if ($_POST[fld_turns] <= 0 OR $_POST[fld_turns] > 10)

 

And that worked :D

 

Secondly, I just used 3 because it changed the size of the text box. Size 2 for the text box was too small. And besides, I can still input more than 10.

 

Problem solved, thank you!

Link to comment
https://forums.phpfreaks.com/topic/118461-solved-need-help/#findComment-609812
Share on other sites

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.