[go: up one dir, main page]

0% found this document useful (0 votes)
21 views5 pages

Php Project Collection

The document contains a collection of six PHP mini projects, including a student grades table, a JSON file management form, a MySQL form for inserting and displaying student data, a month-year calendar generator, a cookie form and display, and a full array manipulation demo. Each project demonstrates different PHP functionalities such as file handling, database interaction, form processing, and array manipulation. The projects serve as practical examples for learning PHP programming.

Uploaded by

dghanem188
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Php Project Collection

The document contains a collection of six PHP mini projects, including a student grades table, a JSON file management form, a MySQL form for inserting and displaying student data, a month-year calendar generator, a cookie form and display, and a full array manipulation demo. Each project demonstrates different PHP functionalities such as file handling, database interaction, form processing, and array manipulation. The projects serve as practical examples for learning PHP programming.

Uploaded by

dghanem188
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP Mini Projects Collection

1. Student Grades Table


<?php
$student = [
"student1" => [5,6,7,8,9],
"student2" => [10,4,1,2],
"student3" => [6,7,8,9,10]
];

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;
}
?>

2. JSON File Add + Validation Form


<?php
$filename = 'students.json';

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 ($_SERVER['REQUEST_METHOD'] === 'POST') {


$newName = trim($_POST['name']);
$newGrade = (int) $_POST['grade'];
$exists = false;
foreach ($students as $student) {
if (strtolower($student['name']) === strtolower($newName)) {
$exists = true;
break;
}
}

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>";
}
?>

3. Form + Insert + Display from MySQL


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
STD: <input type="text" name="std"><br>
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
Email: <input type="text" name="email"><br>
Phone Number: <input type="text" name="phnum"><br>
<input type="submit" value="Submit">
</form>

<?php
session_start();
$st = $_GET['std'] ?? null;
$fn = $_GET['fname'] ?? null;
$ln = $_GET['lname'] ?? null;
$e = $_GET['email'] ?? null;
$ph = $_GET['phnum'] ?? null;

if ($st && $fn && $ln && $e && $ph) {


$conn = mysqli_connect("localhost", "root", "", "university");
$check = "SELECT * FROM student WHERE std = '$st'";
$result = mysqli_query($conn, $check);

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);
}

$s = "SELECT * FROM student WHERE std = '$st'";


$result = mysqli_query($conn, $s);
echo "<table
border='2'><tr><th>STD</th><th>First</th><th>Last</th><th>Email</th><th>
Phone</th></tr>";
while ($a = mysqli_fetch_assoc($result)) {
echo
"<tr><td>{$a["std"]}</td><td>{$a["fname"]}</td><td>{$a["lname"]}</
td><td>{$a["email"]}</td><td>{$a["phone"]}</td></tr>";
}
echo "</table>";
mysqli_close($conn);
}
?>

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'];

echo "<table><tr><th colspan='7'>" . date('F Y', $firstDay) .


"</th></tr><tr>";
foreach ($dayNames as $d) echo "<th>$d</th>";
echo "</tr><tr>";
for ($i = 0; $i < $startDay; $i++) echo "<td></td>";

$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>";
}
?>

5. Cookie Form & Display


<form method="POST" action="">
Name: <input type="text" name="name" />
University Branch: <input type="text" name="branch" />
<input type="submit" value="Send" />
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$cookie_name = $_POST['name'] ?? '';
$cookie_branch = $_POST['branch'] ?? '';

if ($cookie_name) setcookie("name", $cookie_name, time() + 3600,


"/");
if ($cookie_branch) setcookie("branch", $cookie_branch, time() +
3600, "/");

header("Location: " . $_SERVER['PHP_SELF']);


exit();
}

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>";
}
?>

6. Full Array Manipulation Demo


<?php
$numbers = array(10, 5, 20, 15, 25, 20, 10, 30);
echo "Original: " . implode(", ", $numbers) . "<br>";
$sum = array_sum($numbers);
$average = $sum / count($numbers);
echo "Sum: $sum<br>Average: $average<br>";
echo "Max: " . max($numbers) . "<br>Min: " . min($numbers) . "<br>";
sort($numbers);
echo "Sorted: " . implode(", ", $numbers) . "<br>";
$numbers[] = 30;
echo "After adding 30: " . implode(", ", $numbers) . "<br>";
$unique = array_unique($numbers);
echo "Unique: " . implode(", ", $unique) . "<br>";
$removed = array_pop($numbers);
echo "After removing $removed: " . implode(", ", $numbers) . "<br>";
$search = 20;
echo in_array($search, $numbers) ? "$search exists<br>" : "$search does
not exist<br>";
$counts = array_count_values($numbers);
foreach ($counts as $n => $c) echo "Number $n occurs $c times<br>";
$slice = array_slice($numbers, 2, 3);
echo "Sliced: " . implode(", ", $slice) . "<br>";
$more = array(40, 50);
$merged = array_merge($numbers, $more);
echo "Merged: " . implode(", ", $merged) . "<br>";
$common = array_intersect($numbers, $more);
echo "Common: " . implode(", ", $common) . "<br>";
$diff = array_diff($numbers, $more);
echo "Difference: " . implode(", ", $diff) . "<br>";
?>

You might also like