0% found this document useful (0 votes)
5 views

Program 8

The first document is a PHP script that implements a simple visitor counter, storing the count in a text file and displaying the total number of visitors. The second document is a PHP script that connects to a MySQL database to fetch and sort student records by name using the selection sort algorithm, then displays the sorted list. Both scripts demonstrate basic PHP functionality for file handling and database interaction.

Uploaded by

flytechi13579
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Program 8

The first document is a PHP script that implements a simple visitor counter, storing the count in a text file and displaying the total number of visitors. The second document is a PHP script that connects to a MySQL database to fetch and sort student records by name using the selection sort algorithm, then displays the sorted list. Both scripts demonstrate basic PHP functionality for file handling and database interaction.

Uploaded by

flytechi13579
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

a.

<!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>
</head>
<body>
<?php
// File to store visitor count
$file = 'count.txt';

// Check if the file exists; if not, create it and initialize count


if (!file_exists($file)) {
file_put_contents($file, 0);
}

// Read current count from file


$count = (int)file_get_contents($file);

// Increment count
$count++;

// Write updated count back to file


file_put_contents($file, $count);

// Display visitor count


echo "<h1>Visitor Count</h1>";
echo "<p>The number of users visited: " . $count . "</p>";
?>
</body>
</html>

b.

<?php
// Database connection parameters
$servername = "localhost";
$username = "username"; // replace with your DB username
$password = "password"; // replace with your DB password
$dbname = "school"; // replace with your DB name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Fetch student records from database


$sql = "SELECT id, name FROM students";
$result = $conn->query($sql);

$students = [];
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$students[] = $row;
}
}

// Selection Sort Algorithm


for ($i = 0; $i < count($students) - 1; $i++) {
$minIndex = $i;
for ($j = $i + 1; $j < count($students); $j++) {
if ($students[$j]['name'] < $students[$minIndex]['name']) {
$minIndex = $j;
}
}

// Swap the found miminimummiminimumnimummiminimummiminimumnimumnimumnimum


element with the first element
if ($minIndex != $i) {
$temp = $students[$i];
$students[$i] = $students[$minIndex];
$students[$minIndex] = $temp;
}
}

// Display sorted student records


echo "<h1>Sorted Student Records</h1>";
foreach ($students as $student) {
echo "<p>ID: " . $student['id'] . " - Name: " .
htmlspecialchars($student['name']) . "</p>";
}

// Close connection
$conn->c

You might also like