Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/274743-help-with-a-small-program/
Share on other sites

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 by JoshuaDempsey
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.