Jump to content

Recommended Posts

I have a simple script (below), but instead of doing the math and echoing the quoted stuff as is and the math functions completed, it echoes the same thing all the time.

 

for i : 1..5

58

end for

 

<form name="form1" method="post" action="<?php echo $PHP_SELF; ?>">
  <label>Type of Ship
  <select name="type" id="type">
    <option value="carrier">Aircraft Carrier</option>
    <option value="btlship">Battleship</option>
    <option value="destroy">Destroyer</option>
    <option value="submarn">Submarine</option>
    <option value="patrolb">Patrol Boat</option>
  </select>
  </label>
  <br>
  <label>Orientation
  <select name="orient" id="orient">
    <option value="vert">Vertical</option>
    <option value="hori">Horizontal</option>
  </select>
  </label>
  <br>
  <label>Start 
  <select name="s_x" id="s_x">
    <option value="0">A</option>
    <option value="1">B</option>
    <option value="2">C</option>
    <option value="3">D</option>
    <option value="4">E</option>
    <option value="5">F</option>
    <option value="6">G</option>
    <option value="7">H</option>
    <option value="8">I</option>
    <option value="9">J</option>
  </select>
  </label>
  <label>
  <select name="s_y" id="s_y">
    <option value="0">1</option>
    <option value="1">2</option>
    <option value="2">3</option>
    <option value="3">4</option>
    <option value="4">5</option>
    <option value="5">6</option>
    <option value="6">7</option>
    <option value="7">8</option>
    <option value="8">9</option>
    <option value="9">10</option>
  </select>
  </label>
  <br>
  <label>
  <input type="submit" name="Submit" value="Submit">
  </label>
</form>
<?php
if(!empty($_POST['Submit'])){
   if($_POST['type'] == "carrier"){
      $length = 5;
   }elseif($_POST['type'] == "btlship"){
      $length = 4;
   }elseif($_POST['type'] == "destroy"){
      $length = 3;
   }elseif($_POST['type'] == "submarn"){
      $length = 3;
   }elseif($_POST['type'] == "patrolb"){
      $length = 2;
   }
   
   echo "for i : 1.." . $length . "<br />";
   if($_POST['orient'] == "vert"){
      echo $_POST['type'] . "_x(i,1) := " . $_POST['s_x'] * 25 + 2 . "<br />" . $_POST['type'] . "_x(i,2) := " . $_POST['s_x'] * 25 * $length + 27 . "<br />" . $_POST['type'] . "_y(i,1) := i * 25 + " . $_POST['s_y'] * 25 + 2 . "<br />" . $_POST['type'] . "_y(i,2) := i * 25 + " . $_POST['s_y'] * 25 + 27;
   }elseif($_POST['orient'] == "hori"){
      echo $_POST['type'] . "_x(i,1) := i * 25 + " . $_POST['s_y'] * 25 + 2 . "<br />" . $_POST['type'] . "_x(i,2) := i * 25 + " . $_POST['s_y'] * 25 + 27 . "<br />" . $_POST['type'] . "_y(i,1) := " . $_POST['s_x'] * 25 + 2 . "<br />" . $_POST['type'] . "_y(i,2) := " . $_POST['s_x'] * 25 * $length + 27;
   }
   echo "<br />end for";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/109220-php-preforming-math-function-in-quotes/
Share on other sites

Not sure what your problem is as I'm not understanding you, but whenever you are doing any type of arithmetic within a string your should wrap it in quotes:

echo "for i : 1.." . $length . "<br />";
   if($_POST['orient'] == "vert"){
      echo $_POST['type'] . "_x(i,1) := " . ($_POST['s_x'] * 25 + 2) . "<br />" . $_POST['type'] . "_x(i,2) := " . ($_POST['s_x'] * 25 * $length + 27) . "<br />" . $_POST['type'] . "_y(i,1) := i * 25 + " . ($_POST['s_y'] * 25 + 2) . "<br />" . $_POST['type'] . "_y(i,2) := i * 25 + " . ($_POST['s_y'] * 25 + 27);
   }elseif($_POST['orient'] == "hori"){
      echo $_POST['type'] . "_x(i,1) := i * 25 + " . ($_POST['s_y'] * 25 + 2) . "<br />" . $_POST['type'] . "_x(i,2) := i * 25 + " . ($_POST['s_y'] * 25 + 27) . "<br />" . $_POST['type'] . "_y(i,1) := " . ($_POST['s_x'] * 25 + 2) . "<br />" . $_POST['type'] . "_y(i,2) := " . ($_POST['s_x'] * 25 * $length + 27);
   }
echo "<br />end for";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.