Wednesday, 8 June 2016

Design a web page using PHP to insert student information in a database and display all the student details in a page. At the time of insertion student id will be generated automatically and display in same form.

Dbconn.php

<?php
session_start();
ob_start();
error_reporting(0);
$serverName = "localhost";
$dbUserName = "root";
$dbPassword ="";
$dbName = "student";
mysql_connect($serverName,$dbUserName,$dbPassword)
        or die(mysql_error());
mysql_select_db($dbName);

Index.php

<?php
require('dbconn.php');
?>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="" method="post">
        <h1 align="center"><u>Student Database Table</u></h1>
        <table align="center">
            <tr>
                <td>Name</td>
                <td> <input type="text" name="t1"/> </td>
            </tr>
            <tr>
                <td>Roll No</td>
                <td> <input type="text" name="t2"/> </td>
            </tr>
            <tr>
                <td>Marks</td>
                <td> <input type="text" name="t3"/> </td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" value="Submit" name="btn"/> </td>
            </tr>
        </table>
        <?php
        if(isset($_POST['btn'])) 
    {
    $name = $_POST['t1'];
    $roll = $_POST['t2'];
    $marks = $_POST['t3'];
    
    $sql = "INSERT INTO student VALUES('','$name','$roll','$marks')";
    mysql_query($sql) or die(mysql_error());
    }
        ?>
        </form>
        
        <br/><br/><h3><a href="data.php">Show Student Database</a></h3>
    </body>
</html>

Data.php

<?php
    require('dbconn.php');
    ob_start();
?>
<html>
    <head>
        <title>Student Database</title>
        <style>
            td{
                padding: 10px;
            }
            th{
                padding: 10px;
            }
        </style>
    </head>
    <body>
        <h3 align="right"><a href="index.php">Go to Home Page</a></h3>
        <h1 align="center"><u>Student Database</u></h1>
        <table align="center" border="1">
            <th>Id</th>
            <th>Name</th>
            <th>Roll</th>
            <th>Marks</th>
        
            <?php

                $sql = "select * from student";
                $result=mysql_query($sql); 
                while($rws = mysql_fetch_assoc($result)){ ?>
            <tr><td><?php echo $rws['Id']."\n"; ?> </td>
                <td><?php echo $rws['Name']."\n"; ?> </td>
                <td><?php echo $rws['Roll']."\n"; ?> </td>
                <td><?php echo $rws['Marks']."\n"; ?> </td>
                <?php } ?>
        </table>
    </body>
</html>

Write a PHP code which takes input from user and perform the sorting (bubble sorting) the searching (linear searching) techniques depending upon the user choice.

Index.php

<html>
    <head>
        <title>Assignment - 17</title>
    </head>
    
    <frameset cols="20%,80%">
        <frame name="left" src="left.php"/>
         <frame name="right" src=""/>   
    </frameset>
    
</html>

Left.php

<html>
    <head>
        <title>Demo</title>
        <base href="" target="right">
    </head>
    <body>
        <a href="bubble.php" >Bubble Sort</a><br/>
        <a href="linear.php" >Linear Search</a>
    </body>   
</html>

Bubble.php

<html>
    <head>
        <title>Bubble sort</title>
    </head>
    <body>
        <form action="" method="post">
            Enter numbers : <input type="text" name="num"/><br/>
            <input type="submit"/>
            <br/>
            <?php

if($_POST)
{
//get the post value from form
$numbers = $_POST['num'];
//separate the numbers and make into array
$arr = explode(',', $numbers);
function bubbleSort(array $arr)
{
    $n = sizeof($arr);
    for ($i = 1; $i < $n; $i++) {
        for ($j = $n - 1; $j >= $i; $j--) {
            if($arr[$j-1] > $arr[$j]) {
                $tmp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $tmp;
            }
        }
    }
     
    return $arr;
}
$result = bubbleSort($arr);
print_r($result);
        }
?>
        </form>
    </body>
</html>


Linear.php

<html>
    <head>
        <title>Linear Search</title>
    </head>
    <body>
        <form method="post">
            Enter numbers : <input type="text" name="num"/><br/>
            Enter number to search : <input type="text" name="value"/><br/>
            <input type="submit"/>
            <br/>
            <?php
                if($_POST)
                {
//get the post value from form
                    $numbers = $_POST['num'];
//separate the numbers and make into array
                    $arr = explode(',', $numbers);
                    
                    $value = $_POST['value'];
                    
                    function linear_search(&$arr, $value) {
        for ($i = 0; $i < sizeof($arr); $i++) {
            if ($arr[$i] == $value) {
                return $i;
            }
        }
        return -1;
    }
   
    # Print numbers to search
    
    
    # Find the index of 'value'
    $index = linear_search($arr, $value) + 1;
    
    # Print the index where 'value' is located
    echo "\nNumber $value is at index $index\n";
                    
                }
            
            ?>
        </form>
    </body>
</html>

Tuesday, 7 June 2016

Write a C program which will take student info such as name, roll no., department as input and store in a structure student_details. Store information about the students library details which includes roll no. and book_id in a structure library_details. The program will be able to calculate the total number of books issued to the student of a particular department

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct student_details
{
char name[20];
int roll;
int dept;
struct student_details *next;
}student;

typedef struct library_details
{
int roll;
int dept;
int book_id;
struct library_details *next;
}library;

void print_info()
{
printf("\n 1. Enter student details \n");
printf("\n 2. Issue book \n");
printf("\n 3. Count book issued \n");
printf("\n 4. Exit \n");
}

int main()
{
int choice=0;
int roll,dept,book_id,bi;
char name[20];
student *sstart=NULL,*stmp,*sn;
library *lstart=NULL,*ltmp,*ln;

print_info();

while(choice != 4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 : printf("\n Enter name, roll no., dept no : ");
scanf("%s %d %d",&name[20],&roll,&dept);
stmp = (student *)malloc(sizeof(student));
stmp->name[20]=name[20];
stmp->roll=roll;
stmp->dept=dept;
stmp->next=NULL;

if(sstart==NULL)
sstart=stmp;
else
{
sn=sstart;
while(sn->next != NULL)
sn=sn->next;
sn->next=stmp;
}

break;

case 2 : printf("\n Enter roll, dept and book_id : ");
scanf("%d %d %d",&roll,&dept,&book_id);
ltmp = (library *)malloc(sizeof(library));
ltmp->roll=roll;
ltmp->dept=dept;
ltmp->book_id=book_id;
ltmp->next=NULL;

if(lstart==NULL)
lstart=ltmp;
else
{
ln=lstart;
while(ln->next != NULL)
ln=ln->next;
ln->next=ltmp;
}

break;

case 3 : printf("\n Enter dept no and roll no : ");
scanf("%d %d",&dept,&roll);
bi=0;
ln=lstart;
while(ln!=NULL)
{
if(ln->roll == roll && ln->dept == dept)
{
bi++;
}
ln=ln->next;
}
printf("\n Total issued book to dept id %d and roll no %d : %d", dept,roll,bi);

break;

default : printf("WRONG INPUT !!!");
break;
}
}
return 0;
}

Write a program to perform sorting operation of an integer array such that even elements will be sorted in ascending order followed by odd elements sorted is descending order:

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[10],a[10],b[10],m,n,i,j=0,k=0,temp;
printf("\n Enter array size: ");
scanf("%d",&n);
printf("\n Enter the array elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
if(arr[i]%2==0)
{
a[j]=arr[i];
j++;
}
else
{
b[k]=arr[i];
k++;
}
}
printf("\n Sorted array : ");
for(i=0;i<j;i++)
{
for(m=0;m<j;m++)
{
 if(a[i]<a[m])
 {
temp=a[i];
a[i]=a[m];
a[m]=temp;
 }
}
}
for(i=0;i<j;i++)
printf("%d\t",a[i]);
for(i=0;i<k;i++)
{
for(m=0;m<k;m++)
{
if(b[i]>b[m])
{
temp=b[i];
b[i]=b[m];
b[m]=temp;
}
}
}
for(i=0;i<k;i++)
printf("%d\t",b[i]);
getch();
}