Odd Issue

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Odd Issue

Post 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;
       }

    ?>
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Odd Issue

Post by Ark »

You could try using else instead of elseif.
Orgullo Catracho
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Odd Issue

Post by Xaos »

I tried that, but still same thing.
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Odd Issue

Post by Winawer »

You need another check for $stock>0 after you've changed $stock.
Post Reply

Return to “Beginner Help and Support”