Search This Blog

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

Leave a Reply