Program8
8(a) Visitor Counter (PHP) :
<?php
// File to store the visitor count
$counterFile = "counter.txt";
// Check if the file exists, if not, create it and set count to 0
if (!file_exists($counterFile)) {
file_put_contents($counterFile, 0);
}
// Read the current count from the file
$visitorCount = (int)file_get_contents($counterFile);
// Increment the count
$visitorCount++;
// Save the updated count back to the file
file_put_contents($counterFile, $visitorCount);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Visitor Counter</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f0f8ff;
}
h1 {
color: #333;
}
p {
font-size: 1.2em;
color: #555;
}
</style>
</head>
<body>
<h1>Welcome to Our Website!</h1>
<p>You are visitor number: <strong><?php echo $visitorCount; ?></strong></p>
</body>
</html>
8(b) Student sorting :
SQL:
CREATE DATABASE school;
USE school;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
marks INT NOT NULL
);
INSERT INTO students (name, marks) VALUES
('Alice', 85),
('Bob', 75),
('Charlie', 95),
('Diana', 65),
('Eve', 70);
PHP:
<?php
// Database connection
$host = "localhost";
$username = "root";
$password = ""; // Replace with your database password
$dbname = "school";
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Fetch student records
$sql = "SELECT * FROM students";
$result = $conn->query($sql);
$students = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$students[] = $row;
}
}
// Selection Sort
for ($i = 0; $i < count($students) - 1; $i++) {
$minIndex = $i;
for ($j = $i + 1; $j < count($students); $j++) {
if ($students[$j]['marks'] < $students[$minIndex]['marks']) {
$minIndex = $j;
}
}
// Swap
$temp = $students[$i];
$students[$i] = $students[$minIndex];
$students[$minIndex] = $temp;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sorted Student Records</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f9f9f9;
}
table {
margin: 20px auto;
border-collapse: collapse;
width: 60%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Sorted Student Records</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Marks</th>
</tr>
<?php foreach ($students as $student): ?>
<tr>
<td><?php echo $student['id']; ?></td>
<td><?php echo $student['name']; ?></td>
<td><?php echo $student['marks']; ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
Comments
Post a Comment