Sunday, December 4, 2011

String Functions

Code Example


example.html
<html>
    <body>
        <form action="example.php">
            Enter a string <input type="text" name="string" value="" /><br/>
            <input type="submit" value="   Submit    " name="submit"/>
        </form>
    </body>
</html>
example.php
<?php
 if(isset ($_GET['submit'])){
     $string = $_GET['string'];
     //encrypt the string
     echo 'encrypt the string : '.  crypt($string).'<br/>';

     // lenth of th string  
     echo 'lenth of the string : '.  strlen($string).'<br/>';  

     // possition of a string
     echo 'possition of a in string : '.  strpos($string, 'a').'<br/>';

     //capitalize the first character
     echo 'uper case first char : '.  ucfirst($string).'<br/>';
 }
?>

explode() method

Code Example

split() method is deprecated in php 5.3. Instead of split()  use explode().

explode.php

<?php
$text= "hello friends how are you?";
$newtext= explode(' ', $text);
foreach ($newtext as $str){
    echo $str.'<br/>';
}
?>
output:
hello
friends
how
are
you?

Cookie Example

Code Example

setcookie.php

<?php
//setting a cookie

setcookie('user','Icon Training');

//retrieving a cookie

echo $_COOKIE['user'];
?>
deletecookie.php 

<?php
//deleting the user cookie

setcookie('user','',  time()-3600);

?>

Uploading an image file on server

Code Example

upload.html 
<html>
    <head>
        <title>Upload an image</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="uploadimage.php"  enctype="multipart/form-data" method="post">
            <input type="file" name="file" value="" /><br/>
            <input type="submit" value="   Upload   " />
            
        </form>
    </body>
</html>
uploadimage.php
<?php
$image = $_FILES['file']['name'];
$img_type = $_FILES['file']['type'];
$img_size = $_FILES['file']['size'];
$img_error = $_FILES['file']['error'];
if(!empty ($image) && ($img_type == 'image/gif' || $img_type == 'image/png' || $img_type == 'image/jpeg')
 && $img_size > 0 && $img_size < 40000 && $img_error == 0)
{
    $target="image/".$image;
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
    {
        echo 'image file uploaded successfully';
    }
 else {
        echo 'error';
    }
}
?>

Server side validation

Code Example


registration.html 
<html>
    <head>
        <title>Registration</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="registration.php">
            <table border="0" cellspacing="14">
                <tr>
                    <th colspan="2">Registration form</th>
                </tr>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="fname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email ID</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>Date Of Birth</td>
                        <td><input type="text" name="dob" value="" /></td>
                    </tr>
                    <tr>
                        <td>Phone no: </td>
                        <td><input type="text" name="phone" value="" /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><input type="text" name="address" value="" /></td>
                    </tr>
                     <tr>
                        <td>Gender</td>
                        <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" />Female</td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="   Submit  " /></td>                    
                    </tr>                
            </table>
        </form>
    </body>
</html>
registration.php
<?php
//accessing form data
    $fname=$_GET['fname'];
    $lname=$_GET['lname'];
    $email=$_GET['email'];
    $dob=$_GET['dob'];
    $phone=$_GET['phone'];
    $address=$_GET['address'];
    $gender=$_GET['gender'];
    if(!empty($fname) && !empty ($lname) && !empty ($email) && !empty ($dob) && !empty ($phone) && !empty ($address) && !empty ($gender))
    {
        if(!is_numeric($fname) && !is_numeric($lname) && !is_numeric($email) && !is_numeric($address) && is_numeric($phone))
        {
            // creating connection
            $conection = mysqli_connect('localhost','root','','php_training') or die();
            $query = "INSERT INTO registrationch03 VALUES(0,'$fname','$lname','$email','$dob','$phone','$address','$gender')";
            $data=  mysqli_query($conection,$query);
    
            echo 'registration successfull.';
  
            mysqli_close($conection);
        }
        else {
            echo 'please enter proper data';
        }
    }
    else{
        echo 'Please enter all details';
    }
?>

Simple Registration form

Code Example


Registration.html

<html>
    <head>
        <title>Registration</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="Registration.php">
            <table border="0" cellspacing="14">
                <tr>
                    <th colspan="2">Registration form</th>
                </tr>
                    <tr>
                        <td>First Name</td>
                        <td><input type="text" name="fname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td><input type="text" name="lname" value="" /></td>
                    </tr>
                    <tr>
                        <td>Email ID</td>
                        <td><input type="text" name="email" value="" /></td>
                    </tr>
                    <tr>
                        <td>Date Of Birth</td>
                        <td><input type="text" name="dob" value="" /></td>
                    </tr>
                    <tr>
                        <td>Phone no: </td>
                        <td><input type="text" name="phone" value="" /></td>
                    </tr>
                    <tr>
                        <td>Address</td>
                        <td><input type="text" name="address" value="" /></td>
                    </tr>
                     <tr>
                        <td>Gender</td>
                        <td><input type="radio" name="gender" value="male" />Male<input type="radio" name="gender" value="female" />Female</td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit" value="   Submit  " /></td>
                    
                    </tr>
                
            </table>

        </form>
    </body>
</html>

Registration.php

<?php
    //accessing data from form 

    $fname=$_GET['fname'];
    $lname=$_GET['lname'];
    $email=$_GET['email'];
    $dob=$_GET['dob'];
    $phone=$_GET['phone'];
    $address=$_GET['address'];
    $gender=$_GET['gender'];
    
    // creating connection

    $conection = mysqli_connect('localhost','root','','php_training') or die();
    $query = "INSERT INTO registrationch03 VALUES(0,'$fname','$lname','$email','$dob','$phone','$address','$gender')";
    $data=  mysqli_query($conection,$query);
    
    echo 'registration successfull.';
  
    mysqli_close($conection);
      
?>

Tuesday, November 8, 2011

How to add a new control on joomla control panel ?

To add a new control on your joomla CP kindly follow the steps bellow....
1. Go to the folder  \administrator\modules\mod_quickicon
and open the phpfile mod_quickicon.php.
2. Add this code

array(
'link' => JRoute::_('index.php?option=com_content&task=article.add'),
'image' => 'header/icon-48-article-add.png',
'text' => JText::_('MOD_QUICKICON_ADD_NEW_ARTICLE'),
'access' => array('core.manage', 'com_content', 'core.create', 'com_content', )
),


in the function &getButtons()
Change the link, image and text according to your.....

Tuesday, October 11, 2011

Two Dimensional Associate Array

Code Example

<html>
<head>
<title>Array</title>
</head>
<body>
<?php
$marks=array("Tom"=>array("Eng"=>45,"Maths"=>42,"Sc"=>46),"Pit"=>array("Eng"=>42,"Maths"=>41,"Sc"=>47),"John"=>array("Eng"=>46,"Maths"=>44,"Sc"=>46));
?>

<table align="center" cellspacing="10">
 <tr>
  <th>Name</th>
  <th>English</th>
  <th>Maths</th>
  <th>Science</th>
 </tr>
 <tr>
  <td>Tom</td>
  <td><?php echo $marks['Tom']['Eng']?></td>
  <td><?php echo $marks['Tom']['Maths']?></td>
  <td><?php echo $marks['Tom']['Sc']?></td>
 </tr>
 <tr>
  <td>Pit</td>
  <td><?php echo $marks['Pit']['Eng']?></td>
  <td><?php echo $marks['Pit']['Maths']?></td>
  <td><?php echo $marks['Pit']['Sc']?></td>
 </tr>
 <tr>
  <td>John</td>
  <td><?php echo $marks['John']['Eng']?></td>
  <td><?php echo $marks['John']['Maths']?></td>
  <td><?php echo $marks['John']['Sc']?></td>
 </tr>
 </table> 
</body>
</html>

Largest of three numbers and sum of three number

Code Example

Code for max.html

<html>
<head>
 <title>Maximum</title>
</head>
<body>
 <form action = "e1_registration.php">
 <table align="center" cellspacing="20">
  <tr>
   <td>Enter First Name:</td>
   <td><input type="text" name="firstname"></td>
  </tr>
  <tr>
   <td>Enter Last Name:</td>
   <td><input type="text" name="lastname"></td>
  </tr>
  <tr>
   <td>Phone No.</td>
   <td><input type="text" name="phone"></td>
  </tr>
  <tr>
   <td colspan="2" align = "center"><input type="submit" value="  Submit  "></td>
  </tr>
        </table>
 </form>  
</body>
</html>

Code for max.php


<html>
<body>
<?php
 $num1=$_GET['firstno'];
 $num2=$_GET['secondno'];
 $num3=$_GET['thirdno'];
 echo 'Largest number of '.$num1.', '.$num2.', and '.$num3.'  is : ';
 echo $num1>$num2?($num1>$num3?$num1:$num3):($num2>$num3?$num2:$num3);
 echo '<br>sum of '.$num1.', '.$num2.', and '.$num3.'  is : '.($num1+$num2+$num3);
?>
</body>
</html>

Difference between GET and POST methods


Get:
        1. Get() method append data in URL separated by question mark( ? ) .
        2. Using Get() we can send maximum of 2kb data.
    3. If we are redirecting to a page using Get() method then we can bookmark that page.
    4. This method is unsecure.
Post:
        1. In Post() method data is send via packets.
  2. There is no limitation on the size of data
    3. If we are redirecting to a page using Post() method we can't bookmark that page.
    4. This method is secure.

if - elseif - else

Code Example

<html>
<body>
   <?php
       $day=date("D");
       if ($day==”Sat")
       {
              echo "Have a nice weekend!";
       }
       elseif ($day=="Sun")
       {
             echo "Have a nice Sunday!";
       }
       else
       {
             echo "Have a nice day!";
       }
    ?>
</body>
</html>

if-else statement

Code Example

<html>
<body>
<?php
    $day=date("D");
    if ($day=="Sat")
     {
         echo "Have a nice weekend!";
     } 
    else
    {
          echo "Have a nice day!";
    }
?>
</body>
</html>




Simple if statement

Code Example

<html>
<body>
    <?php
      $day=date("D");
      if ($day=="Sat")echo "Have a nice weekend!";
    ?>
</body>
</html>

echo statement

Echo statement is used to print something on response stream.


Code Example:

<html>
    <body>
      <?php
          echo 'hello  world';
      ?>
    </body>
</html>


Above code is same as following:

<html>
<body>
   hello  world
</body>
</html>


Simple Calculator in PHP

Code Example

code for calculator.html

<html>
  <head>
    <title>Calculator</title>
  </head>
  <body>
 <form action = "calculator.php">
   <table align="center" cellspacing="20">
  <tr>
   <td>Enter First No.:</td>
   <td><input type="text" name="firstno"></td> 
  </tr>
  <tr>
   <td>Enter Second No.:</td>
   <td><input type="text" name="secondno"></td>
  </tr>
  <tr>
   <td>Select function.</td>
   <td><select name="function">
     <option value="sum">Add</option>
     <option value="sub">Subtract</option>
     <option value="mul">Multiply</option>
     <option value="div">Divide</option>
     </select>
   </td>
  </tr>
  <tr>
   <td colspan="2" align = "center"><input type="submit" value="  Submit  ">  </td>
  </tr>
         </table>
    </form>  
</body>
</html>

code for calculator.php

<html>
<head>
   <title>Calculator</title>
</head>
<body>
 <?php
  $num1=$_GET["firstno"];
  $num2=$_GET["secondno"];
  $function=$_GET["function"];
  switch ($function)
  {
   case 'sum':
    echo $num1+$num2;
    break;
   case 'sub':
    echo $num1-$num2;
    break;
   case 'mul':
    echo $num1*$num2;
    break;
   case 'div':
    echo $num1/$num2;
    break;
   default:
    echo 'Please select a funtion';
  }
 ?>
</body>
</html>


Variables, Data Types and Casting in PHP

Code Example 

<html>
  <body>

<?php
  $num1=25;
  $num2=24;
  echo 'sum of '.$num1.', '.$num2.' : '.($num1+$num2);
   
  $num2=' hello'.$num2;
  echo 'sum of '.$num1.', '.$num2.' : '.($num1+$num2);
  $num3=24;
  $num3=$num3.'hello';
  echo 'sum of '.$num1.' and '.$num3.' : '.($num1+$num3).' note here second data is string ';
?>

  </body>
</html>