Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Building an expert system/decision based system in PHP

    The problem I am trying to solve is I have 16 different animals and I need to write a PHP script that asks the user 4 yes/no questions and each question narrows down the available animals until eventually the answer is displayed. This means that the next question will depend on what the user answered in the previous question. Do you have any idea how I can do this without using loads of if else statements.

    Below is what I have done so far, not finished but if I carry on using if statements I will just end up with too much, there must be a better way to do it. Someone suggested to me using arrays within arrays, but that doesn't help me. Any help would be much appreciated.

    PHP Code:
    <?php
    session_set_cookie_params
    (2592000);
    session_start();
    ?>

    <html>
    <head>
    <title>Creature Guessing Game</title>
    </head>
    <body>
    <h1>Creature Guessing Game</h1>
    <p> Welcome to the creature guessing game! </p>
    <p>Click the button below to start or restart the game </p>
     
    <form method="post" action="Creatures.php">
    <input type='submit' name='start' value='Start Game' />
    </form>
     <?php
    //If form not submitted, display form.
    if (!isset($_POST['start'])){
    ?>
    <?php
    //If form is submitted, process input

    else{ //Question 1
    echo "<p>Does the creature live mainly on the land?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes1' value='Yes' />
    <input type='submit' name='no1' value='No' />
    </form>"
    ;
    }
    if (
    $_POST['yes1']){ //Q1 - Yes
    echo "<p>Does the creature have wings?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes2' value='Yes' />
    <input type='submit' name='no2' value='No' />
    </form>"
    ;
    }
    elseif(
    $_POST['no1']){ //Q1 - No
    echo "<p>Does the creature live in the water?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes3' value='Yes' />
    <input type='submit' name='no3' value='No' />
    </form>"
    ;
    }
    if (
    $_POST['yes2']){ //Q1 - Yes and Q2 - Yes
    echo "<p>Can the creature fly?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes4' value='Yes' />
    <input type='submit' name='no4' value='No' />
    </form>"
    ;
    }
    elseif(
    $_POST['no2']){ //Q1 - Yes and Q2 - No
    echo "<p>Is the creature an insect?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes5' value='Yes' />
    <input type='submit' name='no5' value='No' />
    </form>"
    ;
    }
    if (
    $_POST['yes3']){ //Q1 - No and Q2 - Yes
    echo "<p>Is the creature a reptile?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes6' value='Yes' />
    <input type='submit' name='no6' value='No' />
    </form>"
    ;
    }
    if (
    $_POST['no3']){ //Q1 - No and Q2 - No
    echo "<p>Does the creature have feathers?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes7' value='Yes' />
    <input type='submit' name='no7' value='No' />
    </form>"
    ;
    }
    if (
    $_POST['yes4']){ //Q1 - Yes and Q2 - Yes and Q3 - Yes
    echo "<p>Is the creature your thinking of white?</p>";
    echo 
    "<form method='post' action='Creatures.php'>
    <input type='submit' name='yes8' value='Yes' />
    <input type='submit' name='no8' value='No' />
    </form>"
    ;
    }
    if (
    $_POST['yes8']){ //Answer 1: Q1 - Yes and Q2 - Yes and Q3 - Yes and Q4 - Yes 
    echo "<p>Its a goose!</p>";
    }
    if (
    $_POST['no8']){ //Answer 2: Q1 - Yes and Q2 - Yes and Q3 - Yes and Q4 - No
    echo "<p>Its a hawk!</p>";
    }
    ?>
    </body>
    </html>

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,500
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I would use MySQL for a database, but you don't have to.

    Create a .txt file that contains information about each animal.

    Like this ...


    ID | ANIMAL | LAND | AIR | WATER | AMPHIBIOUS | REPTILE | BIRD | MAMMAL | RODENT | FEATHERS | HOOVED | WHITE | BROWN | BLACK |

    Think of all the things that would represent an animal. Separate them all by pipes |
    Use pipes because they are not commonly used for anything else.

    The ID would be any random number or code, so you can identify the line without referencing the animal name.

    Let's say "Horse"

    241|Horse|y||||||y|||y|y|y|y||||||||||
    635|Ostrich|y|||||y|||||y|y|y||||||||||
    873|Mouse|y|||||||y|||y|y|y||||||||||

    So there is an ID, the name "Horse", and a Y in each location that pertains to the horse.

    You'll create enough columns so that no two animals will have exactly the same
    combination of "y" columns. That will allow the whole list to be narrowed down
    to one animal.

    The questions can also be random ... keyed off the many column names you have.

    The PHP script will load your .txt file into an array and begin removing animals
    as the student answers questions.

    Is the animal hooved?
    If they answer Y, the list array narrows down to the lines that have a y in the hooved column.

    The mouse and ostrich will be eliminated because they are not "hooved".
    But notice that all three animals could be white, brown, or black.
    And the mouse is a rodent. Only the horse is a mammal.

    You can add lines at any time for more animals, and add columns for more features.

    Get a big .txt file created and post it here.
    We can help you with the PHP scripting.


    .

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I made a .txt file below, what do I need to do now?


    ID | ANIMAL | LAND | WATER | AMPHIBIOUS | REPTILE | BIRD | FEATHERS | FLY | INSECT | TENTACLES | SCALES | WHITE | BROWN | BLACK | STRIPES| ARMS |

    1 | Bear | Y | | | | | | | | | | | Y | | | Y |
    2 | Hawk | Y | | | | Y | Y | Y | | | | | | Y | | |
    3 | Ostrich | Y | | | | Y | Y | | | | | | | Y | | |
    4 | Snake | Y | | | Y | | | | | | Y | | Y | | | |
    5 | Tiger | Y | | | | | | | | | | | | | Y | |
    6 | Eel | | Y | | Y | | | | | | | | | | | |
    7 | Octopus | | Y | | | | | | | Y | | | | | | |
    8 | Goldfish| | Y | | | | | | | | | | | | | |
    9 | Penguin | Y | Y | Y | | Y | Y | | | | | Y | | Y | | Y |
    10| Grasshopper| Y | | | | | | | Y | | | | | | | |
    11| Ant | Y | | | | | | | Y | | | | Y | | | |
    12| Goose | Y | Y | Y | | Y | Y | Y | | | | Y | | | | |
    13| Frog | Y | Y | Y | | | | | | | | | | | | |
    14| Wasp | Y | | | | | | Y | Y | | | | | | Y | |
    15| Gorilla | Y | | | | | | | | | | | | Y | | Y |
    16| Kangaroo| Y | | | | | | | | | | | Y | | | Y |


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •