0% found this document useful (0 votes)
8 views42 pages

OSP RECORD 1 (8)

The document outlines a practical lab manual for a Master of Computer Applications course focusing on Open Source Programming. It includes various exercises involving PHP and MySQL, such as creating a student mark list, a shopping cart application, and accessing data from a database. Each exercise provides a step-by-step procedure for implementation along with sample code snippets.

Uploaded by

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

OSP RECORD 1 (8)

The document outlines a practical lab manual for a Master of Computer Applications course focusing on Open Source Programming. It includes various exercises involving PHP and MySQL, such as creating a student mark list, a shopping cart application, and accessing data from a database. Each exercise provides a step-by-step procedure for implementation along with sample code snippets.

Uploaded by

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

MASTER OF COMPUTER

APPLICATIONS
“OPEN SOURCE PROGRAMMING lAb”

REG. NO :

SEMESTER-I

November – 2024

DEPARTMENT OF COMPUTER APPLICATIONS

SRINIVASAN COLLEGE OF ARTS & SCIENCE

(AFFILIATED TO BHARATHIDASAN UNIVERSITY, TIRUCHIRAPPALLI)

PERAMBALUR-621 212
DEPARTMENT OF COMPUTER APPLICATIONS
SRINIVASAN COLLEGE OF ARTS & SCIENCE
PERAMBALUR-621 212
CERTIFICATE

This is to certify that the bonafide record of the work done in the
computer laboratory during the academic year 2024-2025.
SUBMITTED BY
NAME : ________________________
REG.NO : ________________________
CLASS : ________________________
TITLE / SUB.CODE : ________________________
SEMESTER : ______________________

For The Practical Examination Held on ______________

STAFF IN-CHARGE HEAD OF THE DEPARTMENT


EXAMINERS:

1. 2.
CONTENTS
PAGE REMARKS/
EX.NO DATE LIST OF PROGRAMS
NO. SIGNATURE
Students marklist preparation using control
statement and display the result in tabular
01
format.

Develop a php program that adds products that


02 are selected from a web page to a shopping
cart.

Develop a php program to access the data


03
stored in a mysql table.

Develop a php program interface to create a


04
database and to insert a table into it.

Develop a php program using classes to


05
create a table.

Develop a php program to upload a file to the


06
server.

Develop a php program to create a


07 directory,and to read contents from the
directory.

Develop a shell program to find the details of


08
an user session.

Develop a shell program to change the


09
extension of a given file.
EX NO:01
DATE:

STUDENTS MARKLIST PREPARATION USING CONTROL


STATEMENT AND DISPLAY THE RESULT IN TABULAR FORMAT

Aim:

To develop a server side PHP program to display the marks, total, grade of a student in
tabular format by accepting user inputs for name, R.no. and marks in the HTML forms.

Procedure:

Step-1: To start and open the notepad and write the HTML program.

Step-2: Declare and define the method to using the POST() method and link the PHP file which

is saved under the same directories in the extension .php

Step-3: Save the file using file name .html

Step-4: Close the HTML file.

Step-5: Open the notepad and the write the PHP program.

Step-6: Declare and define the POST() method and link the HTML file which saved under

the same directories in the extension .html

Step-7: Save the file using file name .php

Step-8: Close the PHP file.

Step-9: To connect to any one of the web server WAMP or IIS.

Step-10: Run the program in the localhost/filename.html

Step-11: To close the program and exit.


//1. Student Mark List using Control Statements

// Save this stu.html

<link rel="stylesheet" href="b.css">


<body>
<form action="stu1.php" method="post">
<center>
<h1>enter student data</h1>
<table border="1">

<tr>
<td><h3>student name:</h3></td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td><h3>student reg:</h3></td>
<td><input type="number" name="regno"></td>
</tr>
<tr>
<td><h3>mark1</h3></td>
<td><input type="number" name="m1"></td>
</tr>
<tr>
<td><h3>mark2</h3></td>
<td><input type="number" name="m2"></td>
</tr>
<tr>
<td><h3>mark3</h3></td>
<td><input type="number" name="m3"></td>
</tr>

</table>
<input type="submit" value="submit">
</center>
</form>
</body>
// stu1.php

<?php
$stuname=$_POST["name"];
$regno=$_POST["regno"];
$m1=$_POST["m1"];
$m2=$_POST["m2"];
$m3=$_POST["m3"];
$tot=$m1+$m2+$m3;
$avg=$tot/3;
if($avg>80)
{
$grade="distinion";
}
elseif($avg>70)
{
$grade="grade1";
}
elseif($avg>60){
$grade="grade2";}

elseif($avg>50)
{
$grade="pass";
}
else
{
$grade="fail";
}
echo"
<html>
<body>
<center>
<h1>student marksheet</h1>
<table border=1>
<tr>
<td><p>student name</p></td>
<td>$stuname</td>
</tr>
<tr>
<td><p>student regno</p></td>
<td>$regno</td>
</tr>
<tr>
<td><p>mark1</p></td>
<td>$m1</td>
</tr>
<tr>
<td><p>mark2</p></td>
<td>$m2</td>
</tr>
<tr>
<td><p>mark3</p></td>
<td>$m3</td>
</tr>
<tr>
<td><p>total</p></td>
<td>$tot</td>
</tr>
<tr>
<td><p>percentage</p></td>
<td>$avg</td>
</tr>
</table>
</center>
</body>
</html>"
?>
OUTPUT
EX NO:02
DATE:

SHOPPING CART

Aim:

To develop a PHP program to adds products that are selected form a web page to a
shopping cart.

Procedure:

Step-1: To start and open the notepad and write the HTML program.

Step-2: Declare and define the method to using the POST() method and link the PHP file which

is saved under the same directories in the extension .php

Step-3: Save the file using file name .html

Step-4: Close the HTML file.

Step-5: Open the notepad and the write the PHP program.

Step-6: Declare and define the POST() method and link the HTML file which saved under

the same directories in the extension .html Step-7: Save the file using file name .php

Step-8: Close the PHP file.

Step-9: To connect to any one of the web server WAMP or IIS.

Step-10: Run the program in the localhost/filename.html

Step-11: To close the program and exit.


//2.Shopping Cart

shop.html <html>

<head><center>

<font size=6 color=green>

AMAZON SHOPPING

</font></center><br>

<marquee>

<font color=red size=5> ORDER

NOW!!!HURRY UP!!!

</font></marquee>

</head>

<body>

<center>

<form action="shop1.php" method="post">

<table border=2 width=40%>

<tr>

<th>Product name</th>

<th>Price</th>

</tr>

<tr>

<td><input type="checkbox" name="vistacd" value="Vista CD"> windows

vistaCD</td>

<td>Rs.3000</td>
</tr>
<tr><td>
<input type="checkbox" name="j2eecd" value="J2EE CD"> windows

j2eeCd</td>

<td>Rs.700</td>

</tr>

<tr><td>

<input type="checkbox" name="win7" value="Windows 7"> Windows7</td>

<td>Rs.5000</td>

</tr>

<tr>

<td><input type="checkbox" name="vs" value="Visual Studio"> Visual

Studio</td>

<td>Rs.10000</td>

</tr>

</table>

<input type="submit" name="submit" value=Accept>

<input type="reset" name="reset" value=Cancel>

</center>

</form>

</body>

</html>

//shop1.php

<html>

<head>

<center>

<font color=green size=8>

SHOPPING CART

</font>

</center>
</head><br>

YOUR SELECTED PRODUCTS ARE:<br>

<?php if(isset($_POST['vistacd']))

{ if($_POST['vistacd']!="")

echo $_POST['vistacd']."<br>";

if(isset($_POST['j2eecd']))

{ if($_POST['j2eecd']!="")

echo $_POST['j2eecd']."<br>";

if(isset($_POST['win7']))

if($_POST['win7']!="") echo

$_POST['win7']."<br>";

if(isset($_POST['vs']))

{ if($_POST['vs']!="")

echo $_POST['vs']."<br>";

?>

<form method="post" action="shop2.php">

<input type="submit" value="oredrnow">

</form>

</html>
shop2.php

<?php

echo"<html>

<body>

<center>

<table>

<tr>

<td>

<font size=5>You will receive your products within 7 days

</font>

</td>

</tr>

</table></center>

</body>

</html>";

?>
OUTPUT
EX NO:03
DATE:

ACCESS THE TABLE FROM THE DATABASE

Aim:

To develop a PHP program to access the data stored in the MYSQL table.

Procedure:
Step-1: To start and create the database with name.
Step-2: To create a table in the database with table name and insert the data in the table.
Step-3: Save the table.
Step-4: To select the table and view the records.
Step-5: Open the notepad or any IDE and write the PHP program for connecting php to sql
database.
Step-6: To use the new mysqli() funtion and link the database with table.
Step-7: Save the file using file name (connect.php)//connect.php it is a file that we created to
connect php to database. Step-8: save the PHP file as "connect.php".
Step-9: Create another php file to fetch a data from database.
Step-10: connect the connect.php to actual php program to fetch data.
Step-11: save the file as "fetch.php".
Step-12: To connect to any one of the web server WAMP or IIS.
Step-13: Run the program in the localhost/fetch.php // (fetch.php is a php file that we saved).
Step-14: To close the file and exit.
//3.Access table from Database
//db my sql
1.CREATE DATABASE employees_db;
2.USE employees_db;
3.CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, name
VARCHAR(100) NOT NULL, email VARCHAR(100) NOT NULL,
phone VARCHAR(15), hire_date DATE, job_title VARCHAR(100), salary DECIMAL(10, 2)
);
4.INSERT INTO employees (name, email, phone, hire_date, job_title, salary) VALUES
('Alice', 'alice@example.com', '1234567890', '2023-01-15', 'Manager', 75000.00), ('Bob',
'bob@example.com', '0987654321', '2023-02-20', 'Developer', 60000.00);

//fetch a php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "employees_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "SELECT id, email, phone, salary, name FROM employees";
$result = $conn->query($sql);
?>
<html>
<head>
<title>Employee</title>
</head>
<body bgcolor="pink">
<center>
<table border=2>
<th>phone no</th>
<th>E.Name</th>
<th>Salary</th>
<th>email</th>
</center>
</body>
</html>
<?php
while($row = $result->fetch_assoc())
{
print
"<tr><td>".$row["phone"]."</td>
<td>".$row["name"]."</td>
<td>".$row["salary"]."</td>
<td>".$row["email"]."</td></tr>";
}
echo"</tr></table></center>";
$result->close();
?>

// here we finish the php


OUTPUT
EX NO:04
DATE:

DATABASE CREATION AND TABLE INSERTION

Aim:

To develop a PHP interface program to create a database and to insert a table into it.

Procedure:

Step-1: To start and create the database with name.

Step-2: To create a table in the database with table name and insert the data in the table.

Step-3: Save the table.

Step-4: To select the table and view the records.

Step-5: Open the notepad and write the PHP program.

Step-6: To use the POST() method and link the database with a table.

Step-7: Save the file using file name .php

Step-8: Close the PHP file.

Step-9: To connect the web server.

Step-10: Run the program in the localhost/filename.html

Step-11: To close the file and exit.


//4. Database Creation and Table Insertion

<?php
$servername = "localhost";
$username = "root";
$password = "yourpasswo";

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

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

// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . $conn->error . "<br>";
}

// Select the database


$conn->select_db("myDB");

// Create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {


echo "Table MyGuests created successfully<br>";
} else {
echo "Error creating table: " . $conn->error . "<br>";
}

// Insert values into table


$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error . "<br>";
}

// Close connection
$conn->close();
?>
OUTPUT
EX NO:05
DATE:

CREATE A TABLE USING CLASS

Aim:

To develop a PHP program using classes to create a table.

Procedure:

Step-1: To start and create the database with name.

Step-2: To create a table in the database with table name and insert the data in the table.

Step-3: Save the table.

Step-4: To select the table and view the records.

Step-5: Open the notepad and write the PHP program with class.

Step-6: To use the POST() method and link the database with table.

Step-7: Save the file using file name .php

Step-8: Close the PHP file.

Step-9: To connect the web server.

Step-10: Run the program in the localhost/filename.html

Step-11: To close the file and exit.


//5.Create a Table using Class
<?php
class Database {
private $servername;
private $username;
private $password;
private $dbname;
private $conn;

// Constructor to initialize database connection parameters


public function __construct($servername, $username, $password, $dbname) {
$this->servername = $servername;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
$this->connect();
}

// Method to establish a database connection


private function connect() {
$this->conn = new mysqli($this->servername, $this->username,
$this->password);

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

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $this->dbname";
if ($this->conn->query($sql) === TRUE) {
echo "Database created successfully\n";
} else {
echo "Error creating database: " . $this->conn->error;
}

// Select the database


$this->conn->select_db($this->dbname);
}

// Method to create a table


public function createTable($tableName, $columns) {
$sql = "CREATE TABLE IF NOT EXISTS $tableName ($columns)";
if ($this->conn->query($sql) === TRUE) {
echo "Table $tableName created successfully\n";
} else {
echo "Error creating table: " . $this->conn->error;
}
}

// Method to insert data into a table


public function insertData($tableName, $data) {
$sql = "INSERT INTO $tableName (firstname, lastname, email) VALUES
$data";
if ($this->conn->query($sql) === TRUE) {
echo "New record created successfully\n";
} else {
echo "Error: " . $sql . "<br>" . $this->conn->error;
}
}

// Method to show all data from a table and print them in an HTML table
public function showTableData($tableName) {
$sql = "SELECT id, firstname, lastname, email, reg_date FROM
$tableName";
$result = $this->conn->query($sql);

if ($result->num_rows > 0) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>First Name</th><th>Last
Name</th><th>Email</th><th>Registration Date</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["id"] . "</td><td>" . $row["firstname"] .
"</td><td>" . $row["lastname"] . "</td><td>" . $row["email"] . "</td><td>" .
$row["reg_date"] . "</td></tr>";
}
echo "</table>";
} else {
echo "No records found\n";
}
}
// Destructor to close the database connection
public function __destruct() {
$this->conn->close();
}
}
// Usage
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";
$db = new Database($servername, $username, $password, $dbname);
$tableName = "MyGuests";
$columns = "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON
UPDATE CURRENT_TIMESTAMP";
$db->createTable($tableName, $columns);
// Insert data into the table
$data = "('John', 'Doe', 'john@example.com'),
('Mary', 'Moe', 'mary@example.com'),
('Julie', 'Dooley', 'julie@example.com')";
$db->insertData($tableName, $data);
// Show all data from the table
$db->showTableData($tableName);
?>
OUTPUT :
EX NO:06
DATE:

TO UPLOAD THE FILE IN THE SERVER

Aim:

To develop a PHP program to upload file in the server.

Procedure:

Step-1: To create a new file in the directory.

Step-2: Write the data in the file.

Step-3: Read the data from the file.

Step-4: To retrieve or print the data.

Step-5: To upload the above file in the server.

Step-6: Close the file.

Step-7: Close the server and exit.


//html file for 6thprg
<body>
<form action="hello.php" method="post" enctype="multipart/form-data">
//save php as hello.php
<label for="file">Select a file:</label>
<input type="file" name="file" id="file">
<button type="submit">Upload</button>
</form>
</body>

//php file for 6th prg

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["fileToUpload"]) &&
$_FILES["fileToUpload"]["error"] == 0)
{
$target_dir = "C:\\Users\\rkris\\OneDrive\\Desktop\\mca\\";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
if (file_exists($target_dir) && move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file))
{
echo "File uploaded.";
} else {
echo "Upload error.";
}
}
?>
OUTPUT
EX NO:07
DATE:

CREATE A DIRECTORY USING PHP

Aim:

To develop a PHP program to create a directory and read the condences from the

directories.

Procedure:

Step-1: To create a directory.

Step-2: To create a file in the directory.

Step-3: Read the data from the file.

Step-4: To retrieve or print the data.

Step-5: Close the file and directory.

Step-6: Exit.
//7.Directory Creation

<?php

$d = 'C:\Users\rkris\OneDrive\Desktop\mca';

if (is_dir($d))
{
echo "Directory already exists.<br>";
}
else
{
mkdir($d, 0777, true);
echo "Directory created.<br>";
}

// Read and display directory contents


$files = scandir($d);

echo "Contents of the directory:<br>";


foreach ($files as $file) {
if ($file != '.' && $file != '..') {
echo $file . "<br>";
}
}

?>
OUTPUT :
EX NO:08
DATE:

TO FIND THE DETAILS OF AN USER SESSION


Aim:

To write the SHELL program to find the details of an user session.

Procedure:

Step-1: To open a LINUX operating system.

Step-2: To write the shell program.

Step-3: Open the text editor and write the program.

Step-4: Save the program using file name with .sh

Step-5: Compile the file using the terminate.

Step-6: To run the file using sh file name.sh

Step-7: Close and exit.


//8. User Session

#!/bin/bash

# we are getting current user info


USER=$(whoami)
#get the user id
user_id=$(id -u)

# Get the hostname of the system


HOSTNAME=$(hostname)

# Get the current working directory


CWD=$(pwd)

# Get the current shell


SHELL=$(echo $SHELL)

# Get the other logged-in users


other_login=$(who)

# Display the session info


echo "Session Information:"
echo "---------------------"
echo "User: $USER"
echo "userid:$user_id"
echo "Hostname: $HOSTNAME"
echo "Current Directory: $CWD"
echo "Current Shell: $SHELL"
echo "other loggins: $other_login"
OUTPUT :
EX NO:09
DATE:

TO DEVELOP THE SHELL PROGRAM TO CHANGE EXTENSION OF A


FILE
Aim:

To write the SHELL program to change extension of a given file.

Procedure:

Step-1: To open a LINUX operating system.

Step-2: To write the shell program.

Step-3: Open the text editor and write the program.

Step-4: Save the program using file name with change.sh

Step-5: Compile the file using the terminate.

Step-6: To run the file using sh file name.sh

Step-7: To change examle.txt file to example.html

Step-8: Close and exit.


//9. Changing File Extension

#!/bin/bash
mv "$1" "$2" && echo "Renamed $1 to $2" || echo "Error renaming file"
OUTPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy