Page 1 of 1

Odd Issue

Posted: Tue Oct 08, 2013 2:21 am
by Xaos
So with my simulation/game, the user inputs a variable and the stock goes down or up. Perfectly normal, all that good stuff works. BUT, the snag is whenever the stock goes below 0, it should show the user "Your stock has crashed!" page. But it has to go to the normal page, show the stock is below 0 and give the user the normal page as if it wasn't below 0, and the next click takes them to the "Your stock has crashed" page. It also has a "sohw last stock" function on the on the crash page, and it works correctly (IE if your stock first dips below 0 and goes to -50, you get taken to the page like you would normally and input 5 for your harvesting, regardless of the result it shows -50 as your last result)

Any ideas?

Code: Select all

 <?php

       global $stock;
       global $turns;
       //global $harvestCount;


       $harvestCount = $_POST['harvest'];



       echo '<!DOCTYPE HTML>';

       session_start();
       $stock = $_SESSION['stock1'];
       $turns = $_SESSION['turns1'];
       $harvestX = $_SESSION['totalH1'];
       echo "Last stock was " . floor($_SESSION['stock1']) . "</br>";
       function Birth(){
         global $stock;
          $birthCount = $stock * abs(((rand(0,10))/10)+(rand(0,10)/10));
          if($birthCount > 0){
             $stock += $birthCount;
             $_SESSION['birthCount'] = floor($birthCount);
             echo floor($birthCount) . " " . "fish born!</br>";
             echo floor($stock) . " " . "fish are now in population</br>";
          }
          return $stock;
       }

       function Death(){
          global $stock;
          $deathCount = $stock * abs(((rand(0,10))/10)-(rand(0,10)/10));
          if($deathCount > 0){
             $stock -= $deathCount;
             $_SESSION['deathCount'] = floor($deathCount);
             echo floor($deathCount) . " " . "fish died...</br>";
             echo floor($stock) . " " . "fish remain after death cycle</br>";
          }
          return $stock;
       }

       function Harvest(){
          global $stock;
          global $harvestCount;
          global $harvestX;
          $harvestX = $harvestX + $harvestCount;
          if($harvestCount > 0){
             $stock -= $harvestCount;
             echo floor($harvestCount) . " " . "fish harvested.</br>";
             echo floor($stock) . " " . "fish remain after harvesting.</br>";
          }
          return $stock;
       }

       if($stock > 0){
          Birth();
          Death();
          Harvest();
          $stock = $_SESSION['stock'] = $stock;
          $turns = $turns + 1;
          $turns = $_SESSION['turns'] = $turns;
          $harvestX = $_SESSION['totalH'] = $harvestX;
          if($harvestCount){
             $harvestCount = 0;
          }

       // HTML Coding
          echo '<div id = "div">';
          echo 'Input Harvest Rate';
          echo '<form action = "newfish.php" method = "post">';
          echo '<input type="text" name="harvest" id="harvest">';
          echo '<input type = "submit" value="Submit" id="submit">';
          echo '</form>';
          echo "Stock equals" . " " . floor($stock) . "</br>";
          echo '</div>';
          echo '</html>';
            echo "You've taken " . $turns . " turns</br>";
            echo "You've harvested " . $harvestX . " total fish.";


       }elseif($stock <= 0){
          echo '</html>'  . "Your fish population crashed.";
          die;
       }

    ?>

Code: Select all

<?php

       global $stock;
       global $turns;
       //global $harvestCount;


       $harvestCount = $_POST['harvest'];


       echo '<!DOCTYPE HTML>';

       session_start();
       $stock = $_SESSION['stock'];
       $turns = $_SESSION['turns'];
       $harvestX = $_SESSION['totalH'];
       echo "Last stock was " . floor($_SESSION['stock']) . "</br>";
       function Birth(){
         global $stock;
          $birthCount = $stock * abs(((rand(0,10))/10)+(rand(0,10)/10));
          if($birthCount > 0){
             $stock += $birthCount;
             $_SESSION['birthCount'] = floor($birthCount);
             echo floor($birthCount) . " " . "fish born!</br>";
             echo floor($stock) . " " . "fish are now in population</br>";
          }
          return $stock;
       }

       function Death(){
          global $stock;
          $deathCount = $stock * abs(((rand(0,10))/10)-(rand(0,10)/10));
          if($deathCount > 0){
             $stock -= $deathCount;
             $_SESSION['deathCount'] = floor($deathCount);
             echo floor($deathCount) . " " . "fish died...</br>";
             echo floor($stock) . " " . "fish remain after death cycle</br>";
          }
          return $stock;
       }

       function Harvest(){
          global $stock;
          global $harvestCount;
          global $harvestX;
          $harvestX = $harvestX + $harvestCount;
          if($harvestCount > 0){
             $stock -= $harvestCount;
             echo floor($harvestCount) . " " . "fish harvested.</br>";
             echo floor($stock) . " " . "fish remain after harvesting.</br>";
          }
          return $stock;
       }

       if($stock > 0){
          Birth();
          Death();
          Harvest();
          $stock = $_SESSION['stock1'] = $stock;
          $turns = $turns + 1;
          $turns = $_SESSION['turns1'] = $turns;
          $harvestX = $_SESSION['totalH1'] = $harvestX;
          if($harvestCount){
             $harvestCount = 0;
          }

       // HTML Coding
          echo '<div id = "div">';
          echo 'Input Harvest Rate';
          echo '<form action = "newfish1.php" method = "post">';
          echo '<input type="text" name="harvest" id="harvest">';
          echo '<input type = "submit" value="Submit" id="submit">';
          echo '</form>';
          echo "Stock equals" . " " . floor($stock) . "</br>";
          echo '</div>';
          echo '</html>';
          echo "You've taken " . $turns . " turns</br>";
          echo  "You've harvested " . $harvestX . " total fish.";

       }elseif($stock <= 0){
          echo '</html>' . "Your fish population crashed.";
          die;
       }

    ?>

Re: Odd Issue

Posted: Tue Oct 08, 2013 5:12 am
by Ark
You could try using else instead of elseif.

Re: Odd Issue

Posted: Wed Oct 09, 2013 10:26 pm
by Xaos
I tried that, but still same thing.

Re: Odd Issue

Posted: Thu Oct 10, 2013 6:39 am
by Winawer
You need another check for $stock>0 after you've changed $stock.