|
|
|
This time I will show one longer example In which I will show a practical example usage of database abstraction and form validation.
This example also shows three different layers: Database, Interface and Business Logic.
Class Db – is designed on such way that it is very easy for using together with Smarty Template.
For validation I used one class, which I wrote few years ago – for someone who wants to
Experiment with this example I suggest to substitute this class with Power Form Validation – which I showed on this site few months ago.
This is one very easy example, which enters you in field of object modeling.
On this principle - of course on much higher level I am modeling and programming
All examples, which you can see on my site: www.truesolution.org.
Object oriented possibilities of php5 gives me possibility of fast developing, code reusing…..
Notice: This Example is tested with php5.
At first you should make database and create a table with this query:
|
CREATE TABLE `sf_users` (
`user_id` int(11) NOT NULL auto_increment,
`user_name` varchar(50) NOT NULL default '',
`u_password` varchar(50) NOT NULL default '',
`name_user` varchar(50) NOT NULL default '',
`surname` varchar(50) NOT NULL default '',
`birth_date` date NOT NULL default '0000-00-00',
`country` varchar(30) NOT NULL default '',
`e_mail` varchar(50) NOT NULL default '',
PRIMARY KEY (`user_id`)
) TYPE=MyISAM AUTO_INCREMENT=9 ;
#
# Dumping data for table `sf_users`
#
INSERT INTO `sf_users` (`user_id`, `user_name`, `u_password`, `name_user`, `surname`, `birth_date`, `country`, `e_mail`) VALUES (1, 'fghfgh', 'b1c84f8d672b5d6a84a7a486e81b465a', 'fghfgh', 'fghfgh', '0000-00-00', 'Serbia', 'sdf@dfg.ghj'); | |
config.php
| <?php
$host="localhost";
$user="";
$password="";
$dbname="your database";
?> | |
top.html
| <html>
<head>
<title>DB_lib</title>
</head>
<body style="margin-left:10; margin-top:10;" bgcolor="">
<table bordercolor=#800040 width=100% border=2 bgcolor="#C0C0C0">
<tr>
<th class="Top" colspan=3 valign="middle" align="left" height="50" bgcolor=#DDDDDD>
</th> </tr ></td></tr > | |
left.html
| <tr>
<td width="0" valign="top" >
</td>
<td align="center">
</br></br> | |
bottom.html
| </td><td width="0" ></td></tr>
<tr><td colspan=3 bgcolor=#DDDDDD align=left height=15></td></tr >
<tr>
<th colspan="3" height="50">
</th></tr></table></body>
</html> | |
prvi.css
| <STYLE>
table td{padding: 0; border-width: 0; vertical-allign: top; font-family: Verdana,Arial, Helvetica, sans-setif;}
th {background-color: #2f5376; color: #FFFFFF; padding: 2px; vertical-align: middle;
font-family: Verdana,Arial, Helvetica, sans-serif:}
.position{font-size:70%;font-weight:normal;font-family: Verdana;color:#2f5376; };
.outer{border: 1px solid silver;}
.head {background-color: #c2cdd6; padding: 5px; font-weight: bold;}
.head a {text-decoration: none; color:#2f5376; font-weight:bold;}
.even {background-color: #dee3e7; padding: 5px;}
.odd {background-color: #e9e9e9; padding: 5px;}
.messatop{background-color: #FDF0BD; padding: 1px; font-weight: bold; color:#2f5376; }
.messabodu{background-color: #e9e9e9; padding: 5px; }
</STYLE> | |
db.php
| <?php
class db{
var $host;
var $db;
var $dbuser;
var $dbpassword;
var $dbconnection;
var $sql;
var $return_array = array();
var $db_sel;
var $number_rows;
var $ins_id;
function get_numberrows(){
return $this->number_rows;
}
function set_numberrows($par){
$this->number_rows=$par1;
}
function db(){
include "config.php";
$this->host=$host;
$this->db=$dbname;
$this->dbuser=$user;
$this->dbpassword=$password;
}
function connect(){
@$this->dbconnection =mysql_connect(@$this->host,@$this->dbuser,@$this->dbpassword);
if (!$this->dbconnection) {
$this->ind_connection='false';
print("Konekcija nije uspela");
exit();
return false;
}else{
$this->db_sel = mysql_select_db($this->db);
if (!$this->db_sel){
print("Da li si siguran da je to ta baza");
exit();
}
return true;
}
}
function exe_queru($par1){
if ($this->connect()) {
$this->sql=$par1;
$this->qry = mysql_query($this->sql);
if (!$this->qry){
if ((mysql_errno()>>0)) {
echo "<br>" . mysql_error() ;
}
print("nesto nije u redu sa upitom");
exit();
}else{
$result = mysql_query($par1);
$this->number_rows=@mysql_num_rows($result);
if ($this->number_rows==0) {
}else{
while ($row = mysql_fetch_array($result)) {
array_push($this->return_array, $row);
}
}
}
}
}
function exe_queru_wr($par1){
if ($this->connect()) {
$this->sql=$par1;
$this->qry = mysql_query($this->sql);
if (!$this->qry){
print("nesto nije u redu sa upitom");
exit();
}else{
//$result = mysql_query($par1);
$this->ins_id =mysql_insert_id($this->dbconnection);
return true;
}
}
}
function instal_database(){
@$this->dbconnection =mysql_connect(@$this->host,@$this->dbuser,@$this->dbpassword);
if (!$this->dbconnection) {
$this->ind_connection='false';
print("Konekcija nije uspela");
exit();
return false;
}else{
//$link = mysql_connect('localhost', 'myname', 'secret');
$list = mysql_list_dbs($this->dbconnection);
$k=0;
while ($row = mysql_fetch_object($list)) {
if ($row->Database==$this->db) {
$k=$k+1;
}
}
if ($k>0) {
}else{
$this->sql="create database ".$this->db ;
$this->qry = mysql_query($this->sql);
if (!$this->qry){
print("nesto nije u redu sa upitom");
exit();
}
}
}
}
}
?> | |
db_acces.php
| <?php
class db_acces extends db{
function check_username($par1){
$this->exe_queru("select * from sf_users where user_name="."'".$par1."'");
if ($this->number_rows==0) {
return true;
}else{
return false;
}
}
function reg_guest($par1,$par2,$par3,$par4,$par6,$par7,$par8){
$N=$this->check_username($par1);
if ($N) {
if($this->exe_queru_wr(" insert into sf_users(user_name,u_password,name_user,surname,birth_date,country,e_mail) values('$par1','$par2','$par3','$par4','$par6','$par7','$par8') ") ){
return $this->ins_id;
}else{
return false;
}
}else{
return false;
}
}
function profil_guest($par1){
$this->exe_queru("select * from sf_users where user_id=".$par1);
return $this->return_array;
}
function change_guest($par1,$par2,$par3,$par4,$par5,$par6,$par7){
if ($this->exe_queru_wr(" update sf_users set user_name="."'".$par2."'".", name_user="."'".$par3."'".", surname="."'".$par4."'".", country="."'".$par5."'".", e_mail="."'".$par6."'".", birth_date="."'".$par7."'"." where user_id=".$par1)) {
return true;
}else{
return false;
}
}
function show_users(){
$this->exe_queru(" select * from sf_users ");
return $this->return_array;
}
function del_user($par1){
if ($this->exe_queru_wr("delete from sf_users where user_id=".$par1)) {
return true;
}else{
return false;
}
}
}
?> | |
registrating.php
| <?php
class registrating {
var $user_id;
var $user_name;
var $password;
var $name_user;
var $surname;
var $birth_date;
var $country;
var $e_mail;
function insert()
{
$b = new db_acces();
$n = $b -> reg_guest($this -> user_name, $this -> password, $this -> name_user, $this -> surname, $this -> birth_date, $this -> country, $this -> e_mail);
return $n;
}
function show_insert()
{
include "reg_guest.html";
}
function show_edit()
{
$b = new db_acces();
$n=$b -> profil_guest($this->user_id);
include("guest_profil.html");
}
function edit()
{
$b = new db_acces();
$n = $b -> change_guest($this->user_id,$this -> user_name, $this -> name_user, $this -> surname, $this -> country, $this -> e_mail, $this -> birth_date);
if ($n == "true") {
$this -> suc = true;
}else{
$this -> suc = false;
}
}
function show_list()
{
$b = new db_acces();
$n=$b -> show_users();
include("list_users.html");
}
function delete(){
$b = new db_acces();
$n = $b -> del_user($this -> user_id);
}
}
?> | |
datum_menager.php
| <?php
class datum_menager {
var $curent_date;
var $curent_datetime;
function datum_menager(){}//ovo pisem da bi klasa bila kompatibilna sa php4
function get_curent_date(){
$a=getdate();
return $this->curent_date=($a['year'].'-'.$a['mon'].'-'.$a['mday']);
}
function get_curent_datetime(){
$a=getdate();
return $this->curent_datetime=($a['year'].'-'.$a['mon'].'-'.$a['mday'].' '.$a['hours'].':'.$a['minutes'].':'.$a['seconds']);
}
}
//test
//$dat=new datum_menager();
//$l=$dat->get_curent_date();
//$r=$dat->get_curent_datetime();
//print("Danasnji datum je ".$l."</br>");
//print("Danasnji datum i vreme je ".$r."</br>");
?> | |
form.php
| <?php
class forms {
var $components =array();
var $value =array();
var $errors =array();
var $email_component =array();
var $value_mails =array();
var $name;
var $count_components;
var $count_email;
function forms(){
$this->i=0;
}
function set_empty($name,$value){
$this->components[]=$name;
if (trim($value)=='') {
$this->value[]='false';
}else{
$this->value[]=$value;
}
}
function set_email($name,$value_mail){
$this->email_component[]=$name;
if (trim($value_mail)==''){
$this->value_mails[]='false';
}
if (trim($value_mail)!=''){
$this->value_mails[]=$value_mail;
}
}
function get_value($s){
return $this->value[$s];
}
function get_components(){
return $this->components;
}
function get_component($j){
return $this->components[$j];
}
function get_count(){
return $this->count_components=count($this->get_components());
}
function getcount_emil(){
return $this->count_emil=count($this->email_component);
}
function get_emil($i){
return $this->value_mails[$i];
}
function getemil_component($i){
return $this->email_component[$i];
}
function set_empty_er(){}
function get_errors(){}
function print_errors(){}
function is_email(){}
function emil_er(){}
}
class validation extends forms {
function validation(){
}
function set_empty_er(){
if ($this->get_count()!=0) {
for ($l=0;$l<$this->get_count();$l++){
if (trim($this->get_value($l)== 'false')){
$this->errors[]="You didn't enter value for field: " .$this->get_component($l)."</br>";
}
}
}
}
function is_hostname ($host){
if (!eregi("^[a-z0-9]{1}[a-z0-9\.\-]*\.[a-z]{2,}$",$host ))
return false;
if (ereg("\.\.",$host)||ereg("\.-",$host)||ereg ("-\.",$host))
return false;
return true;
}
function is_email($email){
$tmp= split("@",$email);
if (count($tmp)<1)
return false;
if (count($tmp)>2)
return false;
@$username = $tmp[0];
@$hostname = $tmp[1];
if (!eregi("^[a-z0-9_\+\.\-]+$",$username ))
return false;
if(!$this->is_hostname ($hostname))
return false;
return true;
}
function emil_er(){
if ($this->getcount_emil()>0) {
for ($p=0;$p<$this->getcount_emil();$p++){
if (trim($this->get_emil($p)!='false')) {
if (!$this->is_email($this->get_emil($p))){
$this->errors[]="You didn't enter e-mail on right way " .$this->getemil_component($p)."</br>";
}
}
}
}
}
function get_errors(){
return @$this->errors;
}
function get_error($i){
return $this->errors[$i];
}
function print_errors(){
$num_error=count($this->errors);
if ($num_error>0){
for ($o=0;$o<$num_error;$o++){
print($this->get_error($o)."</br>");
}
}
}
}
?> | |
validation.html
| <TABLE width="500">
<tr>
<td align="center" class="messatop">
Validation Report:
</td>
</tr>
<tr>
<?php for($i=0;$i<count($n);$i++){ ?>
<tr>
<td class="even"><?php print($n[$i]) ?></td>
</tr>
<?php } ?>
</tr>
</TABLE>
</br></br> | |
message.html
| <TABLE width="500">
<tr>
<td align="center" class="messatop">
Message:
</td>
</tr>
<tr>
<td align="center" class="messabodu">
<?php print($dialog); ?>
</td>
</tr> | |
list_users.html
| <table border="0" width="100%">
<tr>
<th colspan="7">
Users
</th>
<tr>
<td width="30%" class="head">Username:</td>
<td class="even">Name:</td>
<td class="even">Surname:</td>
<td class="even">Date:</td>
<td class="even">Country:</td>
<td class="even">E-mail:</td>
<td class='odd'>Actions:</td>
</tr>
</tr>
<?php for($i=0;$i<count($n);$i++){ ?>
<tr>
<td class="head"><A href="index.php?action=see_user&user_id=<?php print($n[$i][0]); ?>"><?php print($n[$i][1]); ?></A></td>
<td class="even"><?php print($n[$i][3]); ?></td>
<td class='odd'><?php print($n[$i][4]); ?></td>
<td class="even"><?php print($n[$i][5]); ?></td>
<td class='odd'><?php print($n[$i][6]); ?></td>
<td class='odd'><?php print($n[$i][7]); ?></td>
<td class="head"><A href="index.php?action=del_user&user_id=<?php print($n[$i][0]); ?>">Delete</A></td>
</tr>
<?php } ?>
</tr>
</table> | |
guest_profil.html
| <form method="post" action="index.php?user_id=<?php print($n[0][0]); ?>" >
<table border="1">
<tr>
<th colspan="2">
Change Profil
</th>
</tr>
<tr>
<td class="head">
User Name: *
</td>
<td class="odd">
<INPUT type="text" name=user_name value="<?php print($n[0][1]); ?>" >
</td>
</tr>
<tr>
<td class="head">
Name: *
</td>
<td class="odd">
<INPUT type="text" name=name_user value="<?php print($n[0][3]); ?>">
</td>
</tr>
<tr>
<td class="head">
Surname: *
</td>
<td class="even">
<INPUT type="text" name=surname value="<?php print($n[0][4]); ?>">
</td>
</tr>
<tr>
<td class="head">
Date of Birth(yyyy/mm/dd):
</td>
<td class="even">
<INPUT type="text" name=birth_date value="<?php print($n[0][5]); ?>">
</td>
</tr>
<tr>
<td class="head">
Country:
</td>
<td class="even">
<INPUT type="text" name=country value="<?php print($n[0][6]); ?>" >
</td>
</tr>
<tr>
<td class="head">
E-mail: *
</td>
<td class="odd" >
<INPUT type="text" name=email value="<?php print($n[0][7]); ?>" >
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name=chan_guest value=change>
</td>
</tr>
</table>
</form> | |
reg_guest.html
| <form method="post" action="index.php" >
<table border="1">
<tr>
<th colspan="2">
Registration
</th>
</tr>
<tr>
<td class="head">
User Name: *
</td>
<td class="odd">
<INPUT type="text" name=user_name >
</td>
</tr>
<tr>
<td class="head">
Password: *
</td>
<td class="even">
<INPUT type="password" name=password >
</td>
</tr>
<tr>
<td class="head">
Name: *
</td>
<td class="odd">
<INPUT type="text" name=name_user >
</td>
</tr>
<tr>
<td class="head">
Surname: *
</td>
<td class="even">
<INPUT type="text" name=surname >
</td>
</tr>
<TR>
<TD class="head" >Date of birth (dd/mm/yyyy):</TD>
<TD class="odd">
<SELECT NAME="day">
<option value="0">Day</option>
<OPTION value="01" >01</option>
<OPTION value="02">02</option>
<OPTION value="03">03</option>
<OPTION value="04">04</option>
<OPTION value="05">05</option>
<OPTION value="06">06</option>
<OPTION value="07">07</option>
<OPTION value="08">08</option>
<OPTION value="09">09</option>
<OPTION value="10">10</option>
<OPTION value="11">11</option>
<OPTION value="12">12</option>
<OPTION value="13">13</option>
<OPTION value="14">14</option>
<OPTION value="15">15</option>
<OPTION value="16">16</option>
<OPTION value="17">17</option>
<OPTION value="18">18</option>
<OPTION value="19">19</option>
<OPTION value="20">20</option>
<OPTION value="21">21</option>
<OPTION value="22">22</option>
<OPTION value="23">23</option>
<OPTION value="24">24</option>
<OPTION value="25">25</option>
<OPTION value="26">26</option>
<OPTION value="27">27</option>
<OPTION value="28">28</option>
<OPTION value="29">29</option>
<OPTION value="30">30</option>
<OPTION value="31">31</option>
</SELECT>
<SELECT NAME="month">
<option value="0">Month</option>
<OPTION value="01" >01</option>
<OPTION value="02">02</option>
<OPTION value="03">03</option>
<OPTION value="04">04</option>
<OPTION value="05">05</option>
<OPTION value="06">06</option>
<OPTION value="07">07</option>
<OPTION value="08">08</option>
<OPTION value="09">09</option>
<OPTION value="10">10</option>
<OPTION value="11">11</option>
<OPTION value="12">12</option>
</SELECT>
<SELECT NAME="year">
<option value="0">Year</option>
<OPTION value="1954">1954</option>
<OPTION value="1955">1955</option>
<OPTION value="1956">1956</option>
<OPTION value="1957">1957</option>
<OPTION value="1958">1958</option>
<OPTION value="1959">1959</option>
<OPTION value="1960">1960</option>
<OPTION value="1961">1961</option>
<OPTION value="1962">1962</option>
<OPTION value="1963">1963</option>
<OPTION value="1964">1964</option>
<OPTION value="1965">1965</option>
<OPTION value="1966">1966</option>
<OPTION value="1967">1967</option>
<OPTION value="1968">1968</option>
<OPTION value="1969">1969</option>
<OPTION value="1970">1970</option>
<OPTION value="1971">1971</option>
<OPTION value="1972">1972</option>
<OPTION value="1973">1973</option>
<OPTION value="1974">1974</option>
<OPTION value="1975">1975</option>
<OPTION value="1976">1976</option>
<OPTION value="1977">1977</option>
<OPTION value="1978">1978</option>
<OPTION value="1979">1979</option>
<OPTION value="1980">1980</option>
<OPTION value="1981">1981</option>
<OPTION value="1982">1982</option>
<OPTION value="1983">1983</option>
<OPTION value="1984">1984</option>
</SELECT>
</TD>
</TR>
<tr>
<td class="head">
Country:
</td>
<td class="even">
<INPUT type="text" name=country >
</td>
</tr>
<tr>
<td class="head">
E-mail: *
</td>
<td class="odd" >
<INPUT type="text" name=email >
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name=reg_guest value=registrate>
</td>
</tr>
</table>
</form> | |
index.php
| <?php
session_start();
include "prvi.css";
include "top.html";
include "left.html";
include "form.php";
include "db.php";
include "registrating.php";
include "db_acces.php";
include "datum_menager.php";
//--------------------------------------------
if ($_GET["action"]=="see_user") {
$a=new registrating();
$a->show_list();
$a->user_id=$_GET["user_id"];
$a->show_edit();
}elseif ($_GET["action"]=="del_user") {
$a=new registrating();
$a->user_id=$_GET['user_id'];
$a->delete();
$a->show_list();
$a->show_insert();
}elseif(isset($chan_guest)){
$a=new registrating();
$a->user_name=$user_name;
$a->name_user=$name_user;
$a->surname=$surname;
$a->birth_date=$birth_date;
$a->country=$country;
$a->e_mail=$email;
$a->user_id=$_GET['user_id'];
$a->edit();
if ($a->suc=="true") {
$dialog="Operation successful:";
include("message.html");
$a=new registrating();
$a->show_list();
$a->show_insert();
}else{
$dialog="Operation canceled:";
include("message.html");
$a=new registrating();
$a->show_list();
$a->show_insert();
}
}elseif(isset($reg_guest)){
$a=new registrating();
$rr =new validation();
$rr->set_empty("Content",$user_name);
$rr->set_empty("Title",$password);
$rr->set_empty("Name",$name_user);
$rr->set_empty("E Mail",$email);
$rr->set_email("E Mail",$email);
$rr->set_empty_er();
$rr->emil_er();
if (!$rr->get_errors()) {
if ($month==0 or $day==0 or $year==0) {
$birth_date="0000-00-00" ;
}else{
$birth_date=$year."-".$month."-".$day;
}
$pass=(md5($password));
$a->user_name=$user_name;
$a->password=$pass;
$a->name_user=$name_user;
$a->surname=$surname;
$a->role=$user->funct;
$a->birth_date=$birth_date;
$a->country=$country;
$a->e_mail=$email;
$a->visited="0";
$a->admin="0";
$a->reg_reason=$registrating_reason;
$a->insert();
if ($a->suc==true) {
$dialog="You are successful pass procedure of registering:";
include("message.html");
$a=new registrating();
$a->show_list();
$a->show_insert();
}else{
$dialog="Username with that name exists";
include("templates/message.html");
$a=new registrating();
$a->show_list();
$a->show_insert();
}
}else{
$n=$rr->errors;
include "validation.html";
$a=new registrating();
$a->show_list();
$a->show_insert();
}
}else{
$a=new registrating();
$a->show_list();
$a->show_insert();
}
include "bottom.html";
?> | |
|
|
| Form Validation Using PHP to highlight non valid fields Categories : PHP, Form Processing, Data Validation, Beginner Guides | | | PHP Image Validation Class - test if a specific file is of a certain image type without relying on the said file extension. Categories : PHP, PHP Classes, Data Validation, Graphics, Beginner Guides | | | Form Elements Class Categories : PHP, PHP Classes, Form Processing | | | Specify your connection settings and create a link to a MySQL database. Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides | | | Basic Authentication with sessions Categories : PHP, Beginner Guides, Authentication, Form Processing, Sessions | | | cPanel Email Accounts Creator Categories : PHP, PHP Classes, Email, Form Processing, Web Services | | | PHP and javascript mouseover, mouseout, and mousedown events Categories : PHP, Java Script, Form Processing, Beginner Guides | | | SQLite PHP Database Wrapper Categories : PHP, PHP Classes, Databases, SQLite, Beginner Guides | | | FormChecker Package - validate any data via classes and patterns.
Categories : PHP, Form Processing, PHP Classes, Regexps | | | News management class Categories : PHP, PHP Classes, Beginner Guides | | | A beginner's session handling class Categories : PHP, PHP Classes, Sessions, Beginner Guides | | | Validator - A PHP class that can can be used for validating Email IDs and Dates Categories : PHP, PHP Classes, Data Validation, Email, Date Time | | | Simple PHP Form Field Generator Categories : PHP, Beginner Guides, Form Processing, HTML and PHP | | | Banknote Validation - A PHP class that provides several methods to quickly validate banknote serial numbers of the following currencies: AUD, CAD, CHF, CNY, EUR, GBP, JPY, USD.
Categories : PHP, PHP Classes, Data Validation, Regexps | | | Forms protected from XSS attacks (FOPAXSS) Categories : PHP, PHP Classes, Form Processing, Security | |
| | | | Jose Santos wrote :1378
Hi !
The PEAR of the PHP, has an package DB, which is uniform to all databases (mysql, oracle, ...).
I`m using very mutch this package in developement of applications.
| |
|
|