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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 <?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
 <?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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?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

1
2
3
4
 <?php 
$con=mysql_connect("localhost","root","") or die ("no connection to the db");
mysql_select_db("student",$con) or die ("failed");
?>
Be Sociable, Share!

7 Responses to Objected Oriented Programming in PHP

  1. Somebody essentially lend a hand to make severely posts I would state. That is the first time I frequented your website page and so far? I amazed with the analysis you made to create this particular put up incredible. Excellent task!

  2. I simply want to tell you that I am just all new to blogging and site-building and honestly enjoyed your web blog. Probably I’m planning to bookmark your website . You amazingly have good well written articles. Bless you for revealing your website page.

  3. I just want to mention I am just new to blogging and definitely savored this web blog. Very likely I’m want to bookmark your site . You definitely have really good writings. With thanks for sharing your web page.

  4. I couldn’t have really asked for a more rewarding blog. You’re ever present to give excellent information, going right to the point for quick understanding of your target audience. You’re surely a terrific pro in this field. Thanks a ton for always being there for individuals like me.

Leave a Reply

Your email address will not be published. Required fields are marked *

− 6 = 2