0% found this document useful (0 votes)
71 views10 pages

Program 8

Uploaded by

eshwarappan8
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)
71 views10 pages

Program 8

Uploaded by

eshwarappan8
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/ 10

PROGRAM 8:

a. Develop a PHP program (with HTML/CSS) to keep track of the number of visitors visiting the web page
and to display this count of visitors, with relevant headings.

How to Run:

 Create a file name called index.php, copy the below code and paste it and save it.
 Copy index.php file and open XAAMP directory if installed else install it .
 There you’ll find a folder named “htdocs”.
 Inside the “htdocs” folder, paste index.php file.
 After then open your XAAMP and start the Apache server.
 Open your Google Chrome .
 Then, go to the URL “http://localhost/index.php“.

index.php

<!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;

background-color: #f4f4f4;

display: flex;

justify-content: center;

align-items: center;

height: 100vh;
margin: 0;

.container {

text-align: center;

background-color: white;

padding: 30px 50px;

border-radius: 10px;

box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);

h1 {

font-size: 2.5rem;

color: #333;

h2 {

font-size: 1.5rem;

color: #666;

p{

font-size: 1.25rem;

color: #444;

}
.count {

font-size: 2rem;

font-weight: bold;

color: #2c3e50;

</style>

</head>

<body>

<div class="container">

<h1>Welcome to My Website!</h1>

<h2>Visitor Counter</h2>

<?php

// File to store the count of visitors

$counter_file = "counter.txt";

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

if (!file_exists($counter_file)) {

file_put_contents($counter_file, 0);

// Read the current count from the file

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


// Increment the count

$count++;

// Write the updated count back to the file

file_put_contents($counter_file, $count);

// Display the visitor count

echo "<p>You are visitor number: <span class='count'>$count</span></p>";

?>

</div>

</body>

</html>

Output:
8b. Develop a PHP program (with HTML/CSS) to sort the student records which are stored in the
database using selection sort.

Instructions: How to Run?

 Create a database name called students


 Create a file name called sort_students.php, copy the Below code and paste it and save
it.
 Copy sort_students.php file and open XAAMP directory if installed else install it.
 There you’ll find a folder named “htdocs”.
 Inside the “htdocs” folder, paste sort_students.php file.
 After then open your XAAMP and start the Apache server.
 Open your Google Chrome or Mozilla Firefox.
 Then, go to the URL “http://localhost/sort_students.php“.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Sort Student Records</title>

<style>

body {

font-family: Arial, sans-serif;

background-color: #f4f4f4;

display: flex;

justify-content: center;

align-items: center;

height: 100vh;

margin: 0;
}

.container {

background-color: white;

padding: 30px 50px;

border-radius: 10px;

box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);

width: 80%;

max-width: 800px;

h1 {

text-align: center;

font-size: 2rem;

color: #333;

table {

width: 100%;

border-collapse: collapse;

margin-top: 20px;

table, th, td {

border: 1px solid #ddd;

th, td {
padding: 12px;

text-align: left;

th {

background-color: #f4f4f4;

color: #333;

</style>

</head>

<body>

<div class="container">

<h1>Sorted Student Records</h1>

<?php

// Database connection settings

$servername = "localhost"; // Change to your database server

$username = "root"; // Change to your database username

$password = ""; // Change to your database password

$dbname = "school"; // Change to your database name

// Create a connection to the database

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

// Check the connection

if ($conn->connect_error) {

die("Connection failed: " . $conn->connect_error);

}
// Query to fetch student records

$sql = "SELECT id, name, marks FROM students";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

// Fetch all records into an array

$students = [];

while ($row = $result->fetch_assoc()) {

$students[] = $row;

// Selection Sort Algorithm

$n = count($students);

for ($i = 0; $i < $n - 1; $i++) {

$minIndex = $i;

for ($j = $i + 1; $j < $n; $j++) {

if ($students[$j]['marks'] < $students[$minIndex]['marks']) {

$minIndex = $j;

// Swap the elements

$temp = $students[$i];

$students[$i] = $students[$minIndex];

$students[$minIndex] = $temp;

// Display the sorted records in a table

echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Marks</th></tr>";

foreach ($students as $student) {

echo "<tr>

<td>" . $student['id'] . "</td>

<td>" . $student['name'] . "</td>

<td>" . $student['marks'] . "</td>

</tr>";

echo "</table>";

} else {

echo "<p>No student records found.</p>";

// Close the database connection

$conn->close();

?>

</div>

</body>

</html>

 Create a database named students


 Create a studentdata table and insert some records:

CREATE TABLE studentdata (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
marks INT NOT NULL
);

INSERT INTO students (name, marks) VALUES


('Alice', 56),
('Bob', 72),
('Charlie', 88);

You might also like