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 ;
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>");
}
}
}
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.