JoshuaDempsey Posted February 20, 2013 Share Posted February 20, 2013 I have been tasked to create a small program in any language I know, which is PHP. It will be a console app (I can't install a LAMP stack etc) and it has to: Allow one of six minibeasts to be selected from a menu Enter and store the name and grid co-ordinates of the selected minibeast Display how often each minibeast was found Display the minibeasts found in each grid co-ordinate So far in PHP I have got a welcome screen, then some of the menu from which they can input what minibeast they want to select information for (sort of). I have the actual menu worked out, it's a simple "echo" after all, but I need to work out how to execute different code based on their input. This is my main file: require_once 'functions.php'; require_once 'config.php'; welcome(); // Adds the welcome message to the program menu(); // Adds the selection menu to the console app getUserInput(); This is functions.php with all my functions: <?php // This holds all of the functions for the // program // Welcome function (just text really) function welcome(){ echo " Welcome to the console application. "; echo " To begin using the program, type a number from the list below and then press \"ENTER\" to execute it. "; echo " For help, type \"help\" and execute it, for credits type \"credits\" and execute and for prog. info type \"info\". Version $vernumber "; } // Next is the selection menu function menu(){ echo " Please execute a number from below: 1. Slug 2. Centipede 3. Ladybird 4. Snail 5. Woodlouse 6. Worm 7. Exit "; } // Add the input catcher function getUserInput(){ fwrite(STDOUT, "Enter your choice\n"); $selected = fgets(STDIN); if ($selected = "1"){ echo "hi"; } elseif($selected = "2"){ } } ?> This is where the problems start. I take their input as $selected, but the if statement is for some reason not working. Then I need to work out how to execute the function again if they enter nothing. Could this be accomplished in a while.. loop? Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/274743-help-with-a-small-program/ Share on other sites More sharing options...
mweldan Posted February 20, 2013 Share Posted February 20, 2013 if they enter nothing, it will capture this: \n so you will likely have to check for it too. echo (!empty($te) && $te != "\n") ? $te : "nothing"; Quote Link to comment https://forums.phpfreaks.com/topic/274743-help-with-a-small-program/#findComment-1413738 Share on other sites More sharing options...
JoshuaDempsey Posted February 20, 2013 Author Share Posted February 20, 2013 (edited) if they enter nothing, it will capture this: \n so you will likely have to check for it too. echo (!empty($te) && $te != "\n") ? $te : "nothing"; Excellent help! Thank you. I replaced "nothing" with the function for them to enter the number again, but I noticed it only runs twice before exiting the program. Is there anyway I could use a loop to continue running the function again if certain conditions aren't met e.g. number less than 1 and more than 7 and no input. Thanks EDIT: now there is a bug whereby anything I input it asks me for more input ): Edited February 20, 2013 by JoshuaDempsey Quote Link to comment https://forums.phpfreaks.com/topic/274743-help-with-a-small-program/#findComment-1413750 Share on other sites More sharing options...
AyKay47 Posted February 21, 2013 Share Posted February 21, 2013 Use the comparison operator == instead of the assignment operator. if( $selected = 1 ) will always equate to boolean TRUE. Quote Link to comment https://forums.phpfreaks.com/topic/274743-help-with-a-small-program/#findComment-1413757 Share on other sites More sharing options...
AyKay47 Posted February 21, 2013 Share Posted February 21, 2013 A while(true) { } loop with the appropriate break; logic should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/274743-help-with-a-small-program/#findComment-1413760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.