Php Project Collection
Php Project Collection
function CalculateAvg($grade){
if(empty($grade)){
return 0;
}
$total= array_sum($grade);
$count= count($grade);
return $total/$count;
}
foreach($student as $name=>$grade){
$avg=CalculateAvg($grade);
echo "<br><br>";
echo "The average of ".$name." is ".$avg;
}
?>
if (!file_exists($filename)) {
$defaultData = [
["name" => "Alice", "grade" => 90],
["name" => "Bob", "grade" => 78]
];
file_put_contents($filename, json_encode($defaultData,
JSON_PRETTY_PRINT));
}
$jsonContent = file_get_contents($filename);
$students = json_decode($jsonContent, true);
if (!$exists) {
$students[] = ["name" => $newName, "grade" => $newGrade];
file_put_contents($filename, json_encode($students,
JSON_PRETTY_PRINT));
}
}
?>
<form method="post">
Name: <input type="text" name="name">
Grade: <input type="number" name="grade">
<input type="submit" value="Add Student">
</form>
<?php
foreach ($students as $student) {
echo "<li>{$student['name']} - {$student['grade']}</li>";
}
?>
<?php
session_start();
$st = $_GET['std'] ?? null;
$fn = $_GET['fname'] ?? null;
$ln = $_GET['lname'] ?? null;
$e = $_GET['email'] ?? null;
$ph = $_GET['phnum'] ?? null;
if (mysqli_num_rows($result) == 0) {
$sql = "INSERT INTO student (std, fname, lname, email, phone)
VALUES ('$st', '$fn', '$ln', '$e', '$ph')";
mysqli_query($conn, $sql);
}
4. Month-Year Calendar
<form method="get">
<select name="month">
<?php for ($m = 1; $m <= 12; $m++) {
$monthName = date('F', mktime(0, 0, 0, $m, 10));
echo "<option value='$m'" .
(isset($_GET['month']) && $_GET['month'] == $m ? "
selected" : "") .
">$monthName</option>";
} ?>
</select>
<select name="year">
<?php
$currentYear = date('Y');
for ($y = $currentYear - 5; $y <= $currentYear + 5; $y++) {
echo "<option value='$y'" .
(isset($_GET['year']) && $_GET['year'] == $y ? "
selected" : "") .
">$y</option>";
} ?>
</select>
<input type="submit" value="Show Calendar">
</form>
<?php
if (isset($_GET['month']) && isset($_GET['year'])) {
$month = $_GET['month'];
$year = $_GET['year'];
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$daysInMonth = date('t', $firstDay);
$startDay = date('w', $firstDay);
$dayNames = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$day = 1;
for ($i = $startDay; $i < 7; $i++) echo "<td>$day</td>", $day++;
echo "</tr>";
while ($day <= $daysInMonth) {
echo "<tr>";
for ($i = 0; $i < 7; $i++) {
echo $day <= $daysInMonth ? "<td>$day</td>" : "<td></td>";
$day++;
}
echo "</tr>";
}
echo "</table>";
}
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$cookie_name = $_POST['name'] ?? '';
$cookie_branch = $_POST['branch'] ?? '';
if (isset($_COOKIE['name']) || isset($_COOKIE['branch'])) {
echo "<p>Name: " . ($_COOKIE['name'] ?? 'Not set') . "</p>";
echo "<p>University Branch: " . ($_COOKIE['branch'] ?? 'Not set') .
"</p>";
} else {
echo "<p>No cookies set yet.</p>";
}
?>