Search This Blog

Showing posts with label PHP Programming. Show all posts
Showing posts with label PHP Programming. Show all posts
Wednesday, 21 May 2014

Manage the Exam Results online using Result Management System!

1 comments

RESULT MANAGEMENT SYSTEM
This is a web-based application developed for the benefit of Students as well as Staff members of various Polytechnic Colleges located in and around Tamilnadu, South India.  Using this web application, Institution can manage the Results obtained by the Students during their Internal as well as Board Exams.
There are three types of users of Result Management System:  Administrator, Users (Staff Memebrs) and Students.  Administrator is the one, who is having the total control of this web site.  He is having the control over all the users of the system.  He can create new users as well as delete existing users if he wants. 
Staff members are the users who can make entries for the students.  They are the ones who manage the data through the screens available on the system.  They can do the data entry student wise or subject wise.  With the help of this application, students can view their marks which include Assignment marks, Internal Test and Model Exam marks, and the Final Exam (Board) marks online.  

Once logged in, users can do the data entry like entering the details about the Students and the Subjects being taught to them.  These two screens are available under the menu item – Masters.  New entries can be made, existing records can be modified or deleted with the help of the options provided on the entry screen.

Staff members can make entries for Assignments, Internal Tests, Model as well as Final (Board) Exams.  Mark entry can be done Student wise or Subject wise.  Links are provided under Result menu for doing the mark entry for each and every student individually.  Options are also available for doing the entry subject wise under the menu – Reports.  Reports can be obtained subject wise for having the results in Assignments, Internal Tests and Board Exams. 
To have a demo of this web application, follow the like futureforyou.in/demo and e-mail us to davidjlivingston@gmail.com to get the username and password for a free trial.

Students when logged in can view the marks obtained in Assignments, Internal Tests and Board Exams separately.  Each student can view only the marks obtained by him.  It helps the parents to get to know the performance of their son/daughter then and there without coming all the way from home to college.

Continue reading →
Wednesday, 13 February 2013

Learning More on Operators in PHP

0 comments

More Operators in PHP

Auto-increment and Auto-decrement Operators
Auto-increment (++) and Auto-decrement (--) operators are unary operators, which increments and decrements the value of a variable by one. These operators are unique in that they work only on variables; the operators change their operands’ values and return the new value.
These operators can be used in two ways: prefix notation and postfix notation. If we put the operator in front of the operand, it returns the new value of the operand (incremented or decremented). If we put the operator after the operand, it returns the original value of the operand (before the increment or decrement occurs). For e.g.:
$count = 10;
echo $count++;
echo ++count;
The above set of statements will display the values 10 and 12 during execution. Because the first echo statement will return the value of variable $count before increment, whereas the second echo will display the value of $count after increment.
Auto-increment and Auto-decrement operators can be applied to strings as well as numbers. Incrementing an alphabetic character turns it into the next letter in the alphabet. The following table gives some examples for auto-incrementing with letters:
Incrementing this Gives this
“a” “b”
“z” “aa”
“spaz” “spba”
“K9” “L0”
“42” “43”
Comparison Operators:
Comparison operators are binary operators, which compare two operands and result in either true, if the comparison is truthful or false otherwise. Comparison operators can be applied on two numbers, two strings or one number and one string.
The comparison of operands is done either using numeric order or lexicographic (textual) order. This depends on the types and values of operands used in the comparison. The following table summarizes the type of comparison performed by the comparison operators:
First Operand
Second Operand
Type of Comparison
Number
Number
Numeric
String that is entirely numeric (Numeric String)
String that is entirely numeric (Numeric String)
Numeric
String that is entirely numeric (Numeric String)
Number
Numeric
String that is not entirely numeric (Non-numeric String)
Number
Lexicographic
String that is entirely numeric (Numeric String)
String that is not entirely numeric (Non-numeric String)
Lexicographic
String that is not entirely numeric (Non-numeric String)
String that is not entirely numeric (Non-numeric String)
Lexicographic
Table : Type of comparison performed by Comparison Operators
The following are the comparison operators in PHP:
equality (==, :==) Returns true if both operands are equal
identity (===, :===) Returns true if both operands are equal and are of the same data type. These operators do not do implicit type casting.

inequality (!=, <>) Returns true if both operands are not true

not identical (!==, !) Returns true if both operands and their data types are not equal.

greater than (>) Returns true if the first operand has greater value than the second operand

greater than (>=, :>=) Returns true if the first operand has greater than or equal value than the second operand

lesser than (<) Returns true if the first operand has lesser value than the second operand

lesser than or equal to Returns true if the first operand has lesser than or (<=, :<=) equal value than the second operand

Bitwise Operators:
The bitwise operators act on the binary representation of their operands. Each operand is first turned into a binary representation of the value, and then the operation takes place. All the bitwise operators work on numbers as well as strings, but they vary in their treatment of string operands of different lengths. PHP bitwise operators are as follows:
~ (tilde): This bitwise operator negate, i.e., change 1s to 0s and 0s to 1s in the binary representation of the operands. Floating point values are converted to integers before the operation takes place. If the operand is a string, the resulting value is a string of same length as the original, with each character in the string negated.
Other bitwise operators are of binary type. They are:
& bitwise AND
| bitwise OR
^ (caret) bitwise XOR
<< left Shift
>> right Shift
The bitwise AND operator compares each corresponding bit in the binary representation of its operands. If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0.
Logical Operators:
Logical operators provide ways for building complex expressions. They treat their operands as Boolean value and return a Boolean value. Logical operators are:
&& (and)
|| (or)
xor
! (negation)
Continue reading →

Using Operators and Expressions in PHP

0 comments

Forming Expressions Using Operators in PHP

Expressions are elements that can be evaluated to produce some value during the program execution. The simplest expressions are literals and variables. 

A literal evaluates to itself, while a variable evaluates to the value stored in the variable.More complex expressions can be formed using simple expressions and operators. An operator is a symbol used to specify the operation to be performed on operands. Some operators modify their operands, while most do not.

PHP borrows most of its operands from C and Perl. Most operators in PHP are binary operators; they combine two operands (or expressions) into a single, more complex expression. PHP also supports a number of unary operators and a single ternary operator that combines three expressions into a single expression.

Operator Precedence & Associativity:

The order in which operators in an expression are evaluated depends on their relative precedence. For e.g., among arithmetic operators, the operators * and / are in the higher precedence (evaluated first). And the operators + and – are having lower precedence (evaluated last).

To force a particular order, we can group operands with the appropriate operators in parenthesis. Operators placed within parenthesis are evaluated first (i.e., they get the highest priority to be executed first). But, within parenthesis, they are evaluated in the order of their precedence. For example, the expression:

(2 + 4 * 3) / 2

evaluates the expression to 7 – multiplication first, addition next (with in parenthesis) and then divide the result by 2. This is because of the highest priority given to the expression within parenthesis. The remaining operators, which are placed outside the parenthesis, are evaluated in their order of priority.

Associativity defines the order in which operators with the same order of precedence are evaluated. For example, the expression (2 / 2 * 2) is evaluated as:

(2 / 2) * 2 = 2

and not as: 2 / (2 * 2) = 0.5

Here, the associativity is left to right; that means, in case of ambiguity (expression having operators with same precedence), the operators are evaluated from left to right.

Implicit Casting:

Many operators in PHP require that both their operands are of same type. PHP variables can store values of any data type, which include integers, floating-point numbers, strings and more. The conversion of a value from one type to another is called casting.

PHP converts values from one type to another as necessary. This kind of casting (automatic conversion) is called implicit casting or type juggling in PHP. The rules for type juggling done by arithmetic operators are shown in the below table:

Type of First Operand
Types of Second Operand
Conversion Performed
Integer
Floating-point number
The integer is converted to a floating-point number
Integer
String
The string is converted to a number; if the value after conversion is a floating-point number, the integer is converted to a floating-point number.
Floating-point number
String
The string is converted to a floating-point number

Table: Implicit Casting rules for Binary Arithmetic Operators

We can use a string anywhere PHP expects a number. The string is presumed to start with an integer or floating-point number. If no number is found at the start of the string, the numeric value of that string is 0. If the string contains a period (.) or upper- or lower-case e, evaluating it numerically produces a floating-point number. For e.g.,

9 Lives” -1; //8 (int)

3.14 Pies” * 2; //6.28 (float)
             

“1E3 Points of Light” + 1 //1001 (float)

Types of Operators:

Based on their usage, operators are of various types:
  1. Arithmetic operators
  2. String Concatenation operator
  3. Auto increment and Auto Decrement operator
  4. Comparison operators
  5. Bitwise operators
  6. Logical operators
  7. Casting operators
  8. Assignment operators
  9. Miscellaneous operators

The first and foremost operator of all PHP operators is arithmetic operators. Arithmetic operators are set of operators that perform arithmetic operations on numeric values. Most of the arithmetic operators are binary; however, the arithmetic negation (-) and arithmetic assertion (+) operators are unary.

Binary arithmetic operators are: +, -, *, / and % and they perform operations like addition, subtraction, multiplication, division and modulo division respectively. If they are applied on non-numeric values, they are converted into numeric values automatically.

The unary arithmetic operator arithmetic negation (-) multiplies the value of its operand by -1 and hence changes its sign. For e.g., -(3-4) evaluates to 1. The arithmetic assertion operator (+) returns the operand multiplied by +1, which has no effect. It is used as a visual cue to indicate true sign of a value.

The second type of operator in PHP is String Concatenation operator. This operator is denoted by the symbol dot (.), which appends the right-hand operand to the left-hand operand and returns the resulting string. Operands are first converted to strings, if necessary. For e.g.:

$n = 5;

$s = ‘5 +’ . $n . ‘is 10’; //$s is ‘5 + 5 is 10’

Here is a Presentation to have an Overview of PHP















Continue reading →

Various Types of Scope of Variables in PHP

0 comments

PHP Variable Scope
The scope of a variable, which is controlled by the location of the variable’s declaration, determines those parts of the program that access it. There are four types of variable scope in PHP: local, global, static and function parameters.
Local Scope:
A variable declared in a function is local to that function. That is, it is visible only to code in that function (including nested function definitions); it is not accessible outside the function. The following code illustrates this:
function update_counter($x)
{
$counter = $x;
echo $counter++;
}
update_counter(10);
This piece of code will display the value 11 during execution. It has a local variable named counter, which is assigned with the value of the parameter $x and then incremented. The incremented value 11 is available only within the function, because it is stored in a local variable.
Global Scope:
Variables declared outside a function are called global variables. Global variables can be accessed from any part of the program. However, by default, they are not available inside a function.
To allow a function to access a global variable, we have to declare the same variable inside the function with the global keyword. Here’s how we can rewrite the update_counter() to allow it to access the global $counter variable:
function update_counter( )
{
global $counter;
$counter++;
}
$counter = 10;
update_counter();
echo $counter;
This program will display the value 11 as its output. Because the global variable counter is declared again inside the function update_counter() as global and then accessed to increment its content.
Static Variables:
A static variable is a local variable of a function, but retains its value between calls to the function. Static variables are declared using the keyword static. The following program illustrates the use of static variables:
function update_counter( )
{
static $counter = 0;
$counter++;
echo “This function is called $count time(s)”;
}
update_counter();
update_counter();
update_counter();
The above lines of code will display the message “This function called … time(s)” 3 times. Each time it displays the corresponding count value in the output as follows:
This function called 1 time(s)
This function called 2 time(s)
This function called 3 time(s)
Function Parameters:
Function parameters are variables used for passing values as input to a function. They are local, meaning that they are available only within the function, for which they are declared:
function greet($name )
{
echo “Hello, $name \n”;
}
greet(“Raja”);
Continue reading →

Fundamentals of PHP Programming

0 comments

PHP Programming Basics


PHP programs are files containing HTML tags with PHP commands embedded in them. This is in contrast to many other dynamic web-page solutions, which are scripts that generate HTML. The web server processes the PHP commands and sends their output (and any HTML from the file) to the browser.
The following program is a simple PHP program that displays the message “Hello, World!”:
<html>
<head>
<title>Look out World</title>
</head>
<body>
<?php echo ‘Hello, World!’ ?>
</body>
</html>
In this example, the PHP echo command produces output (the string “Hello, World!”) which is inserted in the output file. The tags <?php and ?> encloses the PHP commands, which are processed by the web server to generate the required HTML page.

Language Basics:

PHP is an Open Source Scripting language, which is strongly influenced by other languages, such as Perl and C. A statement in PHP is a collection of PHP code that does something. It can be as simple as a variable assignment or as complicated as a loop with multiple exit points.
PHP uses semicolons to separate simple statements. A compound statement that uses curly braces to mark a block of code, such as conditional test or loop, doesn’t need a semicolon after the closing brace.
PHP is a case-insensitive language. It doesn’t consider the case for built-in constructs and keywords. Thus, the following three lines of code are equivalent in PHP:
echo(“Hello, World!”);
ECHO(“Hello, World!”);
EcHo(“Hello, World!”);
The names of user-defined classes and functions are also case-insensitive in PHP. But for the ordinary variables (identifiers used for naming memory locations), PHP becomes case-sensitive. That is,
$name $NAME $NaMe
are three different variables in PHP.

Literals and Identifiers:

A literal is a data value that appears directly in a program. The following are literals in PHP:
2001
0xFE
1.4142
Hi’
Hello, World!”
true
null
An identifier is simply a name. In PHP, identifiers are used to name variables, functions, constants and classes. Variable names always begin with a dollar sign ($) and are case-sensitive. Where as, function names and class names are case-insensitive.
The rules for forming a valid identifier in PHP are as follows:
  • The first character of an identifier must be an ASCII letter (uppercase or lowercase), the underscore (_), or any of the characters between ASCII 0x7F and ASCII 0xFF.
  • After the initial character, we can have characters from alphabets, numbers or special characters (except white space).
Some valid variable names are given below:
$bill
$head_count
$MaxForce
$_underscore
$_int
Here are some illegal variables names:
$not valid
$3Wa

Constants:

A constant is an identifier given for a simple value; only scalar values – Boolean, integer, double and string – can be constants. A constant can be defined using the function define() as follows:
define(‘PUBLISHER’, “O’ Reilly & Associates”);
echo PUBLISHER;
The above set of statements defines constant named PUBLISHER, which is used in the second statement for displaying its content “O’ Reilly & Associates”.
Once set, the value of a constant can’t be changed with in the same program. But they can be referenced later by their identifier any number of times in a program. The main advantage of using constants is that their value can be changed, if required by changing the value only in its definition.

Keywords:

A keyword is a word reserved by the language for its core functionality. We can’t make use of keywords as identifiers for variables, functions or classes. Some of the keywords defined in PHP are:
and
array()
break
class
case
catch
endif
exit()
echo()
if
All keywords in PHP are case-insensitive.

Here is a presentation that introduces PHP to the reader.









Continue reading →
Monday, 11 February 2013

Test Your Knowledge on PHP & MySQL

0 comments

FAQ on PHP & MySQL

(You can find the links for downloading the materials for MySQL and PHP here.)

Two Marks: (2 x 5 = 10)
  1. Tell whether PHP variable and keywords are case sensitive?
  2. List the different data types supported in PHP.
  3. Write a console application in PHP for generating Prime Numbers between 1 to 50.
  4. How do you create arrays in PHP. Give an example.
  5. Write a sample code in PHP for creating and using Objects.
Big Question:
  1. Discuss in details the functions used for generating Summary Reports in MySQL. (8)
(OR)
  1. List any four string related functions in MySQL and explain its syntax. (8)
  2. Create a table called "project" which contains the following data: (16)
empno
projectID
projectname
budget
15
1
Interface Design
10000
17
2
E-Commerce Solution
20000
19
3
Advertising
50000
The employee number (empno) belongs to the employee who is in charge of project. Choose appropriate datatypes for each of the fields. Create the table and insert the data. Decide which keys you might need (primary key, foreign key) and make sure that these are correctly implemented.
Hint: If MySQL complains about warnings or errors when you insert the data, use "show warnings" and "show errors" to look at the warnings and errors.
  • The company has decided that the budgets are too small. Update the project table. Add 5000 to each of the budgets.
  • The company decides to take project number 3 from employee 19 and give it to employee 20. Update the table to reflect this change.
(OR)
  1. a) How to use MySQL left join to select data from multiple tables? Explain (8)
b) What is metadata in MySQL? How to obtain it and use it? Explain. (8)


  1. a) What is PHP? What does it do? List out its features? (8)
    b) Mention the scalar data-types provided by PHP? Explain with Examples (8)
(OR)
  1. Write a console application in PHP to find the factorial of first 10 numbers. (16)
Continue reading →
Friday, 8 February 2013

Developing Applications in PHP

0 comments

Programming Exercises on PHP

Exercise 1:

Write a console application in PHP to check whether the given number is prime or not.

Solution:
Create a file with the extension Prime.php and place the following code in it:
<?php
    $n = 50;
    $prime = false;
   
    if($n%2 == 0)
    {
        print("The number $n is not a Prime Number");
        $prime = true;
    }


    if($n%3 == 0)
    {
        print("The number $n is not a Prime Number");
        $prime = false;
    }

    if(!$prime)
        print("The number $n is a Prime Number");
?>

Execute the above program from the PHP Client as follows:
php Prime.php

Exercise 2:

Develop a console application in PHP that uses a for-loop to generate the output as shown in the following table.  Find the following for first 10 integers:
  1. Inverse (1/I) of 10 integers
  2. Square (I*I) value of first 10 integers
  3. Factorial (I!) of first 10 integers

I
I*I
I!
I/1

1
1
1
1

2
4
2
0.5

3
9
6
0.33333333333333

4
16
24
0.25

5
25
120
0.2

6
36
720
0.16666666666667

7
49
5040
0.14285714285714

8
64
40320
0.125

9
81
362880
0.11111111111111

10
100
3628800
0.1


Solution:
<table border=1>
<tr>
<th>I</th>
<th>I*I</th>
<th>I!</th>
<th>1/I</th>
</tr>
<?php
        for($i = 1;$i<=10; $i++)
        {
?>
<tr><td>
<?php
                Print("$i");
?>
</td><td>
        <?php        
                $square = $i * $i;
                print("$square");
         ?>
</td><td><?php

                $fact = 1;
                for($j=1; $j<=$i; $j++)
                        $fact = $fact * $j;
                print("$fact");
?>
</td><td><?php

                $invers = 1/$i;
                print("$invers \n");
       }
      ?></td></tr>         
?>
</table>

Exercise 3:

Create a web page containing Tables from 1 to 9, each having entries from 1 to 9.  The output should look like this:

Multiplication table

123456789
1 x 1 = 12 x 1 = 23 x 1 = 34 x 1 = 45 x 1 = 56 x 1 = 67 x 1 = 78 x 1 = 89 x 1 = 9
1 x 2 = 22 x 2 = 43 x 2 = 64 x 2 = 85 x 2 = 106 x 2 = 127 x 2 = 148 x 2 = 169 x 2 = 18
1 x 3 = 32 x 3 = 63 x 3 = 94 x 3 = 125 x 3 = 156 x 3 = 187 x 3 = 218 x 3 = 249 x 3 = 27
1 x 4 = 42 x 4 = 83 x 4 = 124 x 4 = 165 x 4 = 206 x 4 = 247 x 4 = 288 x 4 = 329 x 4 = 36
1 x 5 = 52 x 5 = 103 x 5 = 154 x 5 = 205 x 5 = 256 x 5 = 307 x 5 = 358 x 5 = 409 x 5 = 45
1 x 6 = 62 x 6 = 123 x 6 = 184 x 6 = 245 x 6 = 306 x 6 = 367 x 6 = 428 x 6 = 489 x 6 = 54
1 x 7 = 72 x 7 = 143 x 7 = 214 x 7 = 285 x 7 = 356 x 7 = 427 x 7 = 498 x 7 = 569 x 7 = 63
1 x 8 = 82 x 8 = 163 x 8 = 244 x 8 = 325 x 8 = 406 x 8 = 487 x 8 = 568 x 8 = 649 x 8 = 72
1 x 9 = 92 x 9 = 183 x 9 = 274 x 9 = 365 x 9 = 456 x 9 = 547 x 9 = 638 x 9 = 729 x 9 = 81


Do the following before running PHP from browser (from Terminal
)
:
  1. Install Apache Server using the following command
    sudo apt-get install apache2
  2. Run the following script for setting up Password for root user:
    sudo passwd root
  3. Use the following command from the Terminal to have a UI for root login in the login screen:
    sudo sh -c 'echo "greeter-show-manual-login=true" >> /etc/lightdm/lightdm.conf'
  4. If Step 3 doesn't work on your computer, try the following command and include the command greeter-show-manual-login=true in lightdm.conf file:
    gksudo gedit /etc/lightdm/lightdm.conf
Table.php

<?php
echo "<h1>Multiplication table</h1>";
echo "<table border=2 width=75%";

echo "<tr>";
for ($i = 1; $i<= 9 ; $i++)
    echo "<td>".$i."</td>";
echo "</tr>";

for ($i = 1; $i <= 9; $i++ ) {
    //this is the outer loop
    echo "<tr>";

    for ( $j = 1; $j <= 9; $j++ ) { // inner loop
        echo "<td> $j x $i = ".$i * $j."</td>";
    }

    echo "</tr>";
}

echo "</table>";
?>

Exercise 4:

Create a registration form which contains fields name,Roll No,Gender and a submit button.  All the details should be displayed in the server page when the user clicks the submit button.

Solution:

<html>
<body>
<form action="" method="post">
<table align="center">
<tr>
<td>Name</td>
<td>&nbsp;</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Roll Number</td>
<td>&nbsp;</td>
<td><input type="text" name="roll"></td>
</tr>
<tr>
<td>Gender</td>
<td>&nbsp;</td>
<td><select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select></td>
</tr>
<tr>
<td>&nbsp</td>
<td>&nbsp;</td>
<td><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
<br><br>
<?php
if(isset($_REQUEST['submit']))
{
echo "<br>Name :".$_REQUEST['name'];
echo "<br>Roll Number :".$_REQUEST['roll'];
echo "<br>Gender :".$_REQUEST['gender'];
}
?> 


Slightly modify the above program so that it can store and display all the students who have registered themselves through this form.

Solution:
Replace the PHP code from the above program with the following:

<?php
$con = mysql_connect("localhost","root","admin");
mysql_select_db("testdb",$con);

    echo "Students who are registered already : <br/>";

    echo "<table border=2 width=75%>";
    echo "<tr>";
    echo "<td>Student Name</td><td>Roll Number</td><td>Gender</td>";
    echo "</tr>";

    $result = mysql_query("SELECT * FROM Student");
    if(mysql_num_rows($result)<>0)
    {
        while($row = mysql_fetch_array($result))
        {
            echo "<tr>";
            echo "<td>".$row['Name']."</td>";
            echo "<td>".$row['Roll']."</td>";
            echo "<td>".$row['Gender']."</td>";
            echo "</tr>";
        }
    }

   
    $name = $_REQUEST['name'];
    $rollno = $_REQUEST['roll'];
    $gender = $_REQUEST['gender'];
   
    if ($name <> "")
    {
        $result = mysql_query("SELECT * FROM Student WHERE Name = '$name' and Roll='$rollno'");
        if(mysql_num_rows($result)==0)
        {
            $sql = "INSERT INTO Student VALUES('$name','$rollno','$gender')";
            mysql_query($sql,$con);

            echo "<tr>";
            echo "<td>".$name."</td>";
            echo "<td>".$rollno."</td>";
            echo "<td>".$gender."</td>";
            echo"</tr>";
        }

    }
    echo "</table>";

mysql_close($con);
?> 

This program requires a database named testdb and a table named Student, which is created using the following command in the local mysql server (localhost):
 Student | CREATE TABLE `Student` (
  `Name` varchar(15) DEFAULT NULL,
  `Roll` varchar(10) DEFAULT NULL,
  `Gender` char(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Continue reading →