PHP Employee Array Menu Driven Program

PHP Employee Array Menu Driven Program

Here is an example of a PHP menu driven application that prompts the user to enter several employees’ details and saves them in an array list, using a while loop. The application will keep looping until the user enters “quit”.

<?php

// Initialize an empty array to store the employees' details
$employee_details = array();

// Initialize a counter to keep track of the number of employees entered
$counter = 0;

// Use a while loop to keep prompting the user for employee details until they enter "quit"
while(true) {
  // Increment the counter
  $counter++;

  // Prompt the user for the employee's name
  echo "Enter the name of employee #$counter (or 'quit' to exit): ";
  $name = trim(fgets(STDIN));

  // If the user entered "quit", break out of the loop
  if($name == "quit") {
    break;
  }

  // Prompt the user for the employee's job title
  echo "Enter the job title of employee #$counter: ";
  $job_title = trim(fgets(STDIN));

  // Prompt the user for the employee's salary
  echo "Enter the salary of employee #$counter: ";
  $salary = trim(fgets(STDIN));

  // Add the employee's details to the array
  $employee_details[] = array(
    "name" => $name,
    "job_title" => $job_title,
    "salary" => $salary
  );
}

// Print the array of employee details
print_r($employee_details);


To create an HTML page that displays the output of the PHP code provided, you will need to use a PHP block inside the HTML page. Here is an example of how you can do this:




<!DOCTYPE html>
<html>
<head>
  <title>Employee Details</title>
</head>
<body>
  <h1>Employee Details</h1>
  <table>
    <tr>
      <th>Name</th>
      <th>Job Title</th>
      <th>Salary</th>
    </tr>
    <?php
      // Loop through the array of employee details and output a table row for each employee
      foreach($employee_details as $employee) {
        echo "<tr>";
        echo "<td>" . $employee['name'] . "</td>";
        echo "<td>" . $employee['job_title'] . "</td>";
        echo "<td>" . $employee['salary'] . "</td>";
        echo "</tr>";
      }
    ?>
  </table>
</body>
</html>

In this example, we have used an HTML <table> element to create a table to display the employee details. Inside the table, we have used a PHP block (enclosed in <?php and ?> tags) to loop through the $employee_details array and output a table row (<tr> element) for each employee. Inside the table row, we have used <td> elements to output the employee’s name, job title, and salary.

The error message “Fatal error: Uncaught Error: Undefined constant “STDIN”” indicates that the PHP interpreter is unable to find a definition for the constant STDIN.

In the PHP code you provided, STDIN is used as a file handle to read input from the standard input stream (e.g., the keyboard). In order to use STDIN, you will need to make sure that the PHP script is being run from the command line. If you are running the script from a web server (e.g., Apache or Nginx), STDIN may not be defined.

To fix this error, you can try running the script from the command line by navigating to the directory where the script is located and running it with the php command. For example:

php script.php

Alternatively, you can modify the code to read input from a different source, such as the $_POST or $_GET superglobals. For example:




echo "Enter the name of employee #$counter (or 'quit' to exit): ";
$name = trim($_POST['name']);

Posted in: PHP

Leave a Reply

Your email address will not be published. Required fields are marked *