2nd result.Php

<?php// 2. Base classclass User { public $name; public $email; function __construct($name, $email) { $this->name = $name; $this->email = $email; }}// 2. Subclassclass Student extends User { public $marks = []; public $average; public $grade; function __construct($name, $email, $marks) { parent::__construct($name, $email); $this->marks = $marks; $this->calculateAverage(); $this->assignGrade(); } function calculateAverage() { $this->average = array_sum($this->marks) / count($this->marks); } function assignGrade() { if ($this->average >= 75) { $this->grade = “A”; } elseif ($this->average >= 60) { $this->grade = “B”; } elseif ($this->average >= 50) { $this->grade = “C”; } elseif ($this->average >= 35) { $this->grade = “D”; } else { $this->grade = “F”; } }}// 4. Handle form dataif ($_SERVER[“REQUEST_METHOD”] == “POST”) { $name = $_POST[“name”]; $email = $_POST[“email”]; $marks = $_POST[“marks”]; // 4. Create student and store in array $students = []; $student = new Student($name, $email, $marks); $students[] = $student; // 5. Loop and display echo “<h2>Student Grade Results</h2>”; echo “<table border=’1′ cellpadding=’8’>”; echo “<tr><th>Name</th><th>Email</th><th>Average</th><th>Grade</th></tr>”; foreach ($students as $stud) { echo “<tr>”; echo “<td>{$stud->name}</td>”; echo “<td>{$stud->email}</td>”; echo “<td>{$stud->average}</td>”; echo “<td>{$stud->grade}</td>”; echo “</tr>”; } echo “</table>”;}?>

1 Comment

Leave a Reply

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