26 May 2012

Objected Oriented Programming in PHP

                        I hope you all know about what is object oriented programming and what is procedure oriented programming. if you know those i can skip that section that what is the difference between object oriented and procedure oriented programming.

I hope the following simple example help you to understand more about object oriented programming in php
lets go through that......

I'm going to give you a student registration and display the registered users in front page and we can update the details

Pages that the project contains...

1. index.php
2. Registration.php
3.Edit.php

and there is a folder named class which contains the file including our class and a configuration file such as

1. registration_class.php
2. config.php



i hope you all get the structure of our folder

      STUDENT
            |
            |--------> index.php
            |
            |--------> Registration.php
            |
            |--------> Edit.php
            |
            |-------->class
                             |
                             |----------> registration_class.php
                             |
                             |----------> config.php


Then we can go through each page copy and paste all the pages below and set the file as shown above and run the project ok?



index.php

<?php
    require_once(realpath(dirname(__FILE__)."/class/registration_class.php"));
    $obj_reg    =    new registration();
    $table        =    "tbl_registration";
    $res        =   $obj_reg->selectall($table);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Index</title>
<style type="text/css" media="screen">
    .head{
        width:auto;
        height:25px;
        font-family: Georgia, "Times New Roman", Times, serif;
        font-size: 18px;
        font-weight:300px;
        background-color:#9CF;
        color:#30F;
    }
</style>
</head>

<body>
<div id="Menu" style="text-align:right;margin-bottom:20px; margin-top:20px" >
<a href="Registration.php" style="text-decoration:none; font-size:24px;">Resistration</a>
</div>
<table width="100%" border="0">
  <tr>
    <th class="head" scope="col">Sl</th>
    <th class="head" scope="col">Name</th>
    <th class="head" scope="col">House</th>
    <th class="head" scope="col">Street</th>
    <th class="head" scope="col">City</th>
    <th class="head" scope="col">District</th>
    <th class="head" scope="col">State</th>
    <th class="head" scope="col">Phone</th>
  </tr>

  <?php
  $i=1;
  while($row=mysql_fetch_array($res)){?>
 
  <tr>
    <td height="27" align="center"><?php echo $i ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="Edit.php?bintId=<?php echo $row['pk_bint_user_id']; ?>" style="text-decoration:none;color:#000">
            <?php echo $row['vchr_name']; ?>
        </a>
    </td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_house']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_street']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_city']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_district']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_state']; ?></td>
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo $row['vchr_phone']; ?></td>
  </tr>
 
  <?php
  $i++;

  } ?>
  <tr>
      <td height="33"></td>
    <td></td>
    <td colspan="4" style="text-decoration:blink;color:#F00;font-size:24px;font-weight:bold" align="center"><?php if(mysql_num_rows($res)==0){ echo "No Records Found!"; } ?></td>
    <td></td>
    <td></td>
  </tr>

</table>

</body>
</html>


Registration.php

 <?php
require_once(realpath(dirname(__FILE__)."/class/registration_class.php"));
$obj_reg    =    new registration(); // object creation for registration
if(isset($_POST['submit'])){
    $name       =    mysql_real_escape_string($_POST['txtname']);
    $house      =    mysql_real_escape_string($_POST['txthousename']);
    $street     =    mysql_real_escape_string($_POST['txtstreet']);
    $city       =    mysql_real_escape_string($_POST['txtcity']);
    $dist       =    mysql_real_escape_string($_POST['txtdistrict']);
    $state      =    mysql_real_escape_string($_POST['txtstate']);
    $phone      =    mysql_real_escape_string($_POST['txtphone']);
   
    $details = array(
                        "vchr_name"     =>  $name,
                        "vchr_house"     =>  $house,
                        "vchr_street"     =>  $street,
                        "vchr_city"     =>  $city,
                        "vchr_district" =>  $dist,
                        "vchr_state"     =>  $state,
                        "vchr_phone"    =>  $phone);
                       
        $table = "tbl_registration";
        $register=$obj_reg->insert($details, $table);
                if($register){
                    echo "<script> alert('Registered Sucessfuly');</script>";
                    header( 'Location:index.php' ) ;
                }else{
                    echo "<script> alert('Registration Failed');</script>";
                }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Registration</title>
</head>

<body>
<body>
<div id="Menu" style="text-align:right;margin-bottom:20px; margin-top:20px" >
    <a href="index.php" style="text-decoration:none; font-size:24px;">Home</a>
</div>
<table width="100%" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="center" style="color:#36F"><h2><strong>Registration</strong></h2></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form id="form1" name="form1" method="post" action="">
    <table width="100%" border="0" style="font-size:20px">
  <tr>
    <td width="22%">&nbsp;</td>
    <td width="28%">&nbsp;</td>
    <td width="5%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="12%">&nbsp;</td>
  </tr>
  <tr>
    <td height="47">&nbsp;</td>
    <td>Name</td>
    <td>:</td>
    <td><input type="text" name="txtname" id="txtname" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="66">&nbsp;</td>
    <td>House Name</td>
    <td>:</td>
    <td><input type="text" name="txthousename" id="txthousename" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="52">&nbsp;</td>
    <td>Street</td>
    <td>:</td>
    <td><input type="text" name="txtstreet" id="txtstreet" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="55">&nbsp;</td>
    <td>City</td>
    <td>:</td>
    <td><input type="text" name="txtcity" id="txtcity" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>District</td>
    <td>:</td>
    <td><input type="text" name="txtdistrict" id="txtdistrict" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="50">&nbsp;</td>
    <td>State</td>
    <td>:</td>
    <td><input type="text" name="txtstate" id="txtstate" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="51">&nbsp;</td>
    <td>Phone</td>
    <td>:</td>
    <td><input type="text" name="txtphone" id="txtphone" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" id="submit" value="Submit" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

    </form></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

</body>
</html>


Edit.php

 <?php
    require_once(realpath(dirname(__FILE__)."/class/registration_class.php"));
    $obj_reg    =    new registration(); // object creation for registration
    $bintId        =    $_GET['bintId'];
    $table         =     "tbl_registration";
    $where        =    "pk_bint_user_id = $bintId";
    $res        =    $obj_reg->selectrow($table,$where);
  
    if(isset($_POST['Update'])){
        $name       =    mysql_real_escape_string($_POST['txtname']);
        $house      =    mysql_real_escape_string($_POST['txthousename']);
        $street     =    mysql_real_escape_string($_POST['txtstreet']);
        $city       =    mysql_real_escape_string($_POST['txtcity']);
        $dist       =    mysql_real_escape_string($_POST['txtdistrict']);
        $state      =    mysql_real_escape_string($_POST['txtstate']);
        $phone      =    mysql_real_escape_string($_POST['txtphone']);
      
        $details = array(
                            "vchr_name"     =>  $name,
                            "vchr_house"     =>  $house,
                            "vchr_street"     =>  $street,
                            "vchr_city"     =>  $city,
                            "vchr_district" =>  $dist,
                            "vchr_state"     =>  $state,
                            "vchr_phone"    =>  $phone);
                          
            $update=$obj_reg->update($details, $table ,$where);
                    if($update){
                        echo "<script> alert('Registered Sucessfuly');</script>";
                        header( 'Location:index.php' ) ;
                    }else{
                        echo "<script> alert('Updation Failed');</script>";
                    }
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit</title>
</head>

<body>
<body>
<div id="Menu" style="text-align:right;margin-bottom:20px; margin-top:20px" >
    <a href="index.php" style="text-decoration:none; font-size:24px;">Home</a>
</div>
<table width="100%" border="0">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="center" style="color:#36F"><h2><strong>Edit Details</strong></h2></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td><form id="form1" name="form1" method="post" action="">
    <table width="100%" border="0" style="font-size:20px">
  <tr>
    <td width="22%">&nbsp;</td>
    <td width="28%">&nbsp;</td>
    <td width="5%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="12%">&nbsp;</td>
  </tr>
  <tr>
    <td height="47">&nbsp;</td>
    <td>Name</td>
    <td>:</td>
    <td><input type="text" name="txtname" id="txtname" value="<?php echo $res['vchr_name'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="66">&nbsp;</td>
    <td>House Name</td>
    <td>:</td>
    <td><input type="text" name="txthousename" id="txthousename" value="<?php echo $res['vchr_house'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="52">&nbsp;</td>
    <td>Street</td>
    <td>:</td>
    <td><input type="text" name="txtstreet" id="txtstreet" value="<?php echo $res['vchr_street'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="55">&nbsp;</td>
    <td>City</td>
    <td>:</td>
    <td><input type="text" name="txtcity" id="txtcity" value="<?php echo $res['vchr_city'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>District</td>
    <td>:</td>
    <td><input type="text" name="txtdistrict" id="txtdistrict" value="<?php echo $res['vchr_district'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="50">&nbsp;</td>
    <td>State</td>
    <td>:</td>
    <td><input type="text" name="txtstate" id="txtstate" value="<?php echo $res['vchr_state'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="51">&nbsp;</td>
    <td>Phone</td>
    <td>:</td>
    <td><input type="text" name="txtphone" id="txtphone" value="<?php echo $res['vchr_phone'];  ?>" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td height="54">&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Update" id="Update" value="Update" /></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

    </form></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

</body>
</html>

class Folder



registration_class.php


<?php
include_once("config.php");
class registration
{   
    public function insert($info, $table) {
            $sql = "INSERT INTO ".$table." (";
            for ($i=0; $i<count($info); $i++) {           
                $sql .= key($info);
                if ($i < (count($info)-1)) {
                    $sql .= ", ";
                } else $sql .= ") ";
                next($info);
            }
            reset($info);
            $sql .= "VALUES (";
            for ($j=0; $j<count($info); $j++) {
                $sql .= "'".current($info)."'";
                if ($j < (count($info)-1)) {
                   $sql .= ", ";
                } else $sql .= ") ";
                next($info);
            }
            //die($sql);
            $ex=mysql_query($sql) or die("query failed ".mysql_error());
            return $ex;
    }   
   
    public function update($data, $table, $where){
            $cols = array();
            foreach($data as $key=>$val) {
                $cols[] = "$key = '$val'";
            }
            $sql = "UPDATE $table SET " . implode(', ', $cols) . " WHERE $where";
            //die($sql);
            mysql_query($sql) or die("query failed ".mysql_error());
            return true;
    }

    public function select($table,$f1,$f2,$f3,$data,$pid,$weight){
            $sel="SELECT id from $table where $f1='$data' and $f2='$pid' and $f3='$weight'";
            $exe=mysql_query($sel);
            $res=mysql_fetch_array($exe);
            $did=$res[0];
            return $did;   
    }
       
    public function selectall($table){
        $sel="SELECT * from $table";
        //echo $sel;
        $exe=mysql_query($sel);
        return $exe;   
       
    }
    public function selectrow($table,$where){
        $sel="SELECT * from $table WHERE $where ";
        //die($sel);
        $exe=mysql_query($sel);
        $res=mysql_fetch_array($exe);
        //print_r($res);
        return $res;   
       
    }
}
?>



config.php


 <?php
$con=mysql_connect("localhost","root","") or die ("no connection to the db");
mysql_select_db("student",$con) or die ("failed");
?>





No comments:

Post a Comment