My first time for PHP

Source: Internet
Author: User
Tags ereg
Objective

With PHP for the first time, functional requirements are simple: to provide a web interface, receive data, and then interact with the database, ultimately responding to XML results. The process is exposed to many of PHP's common syntax and functions, such as PDO operations on the database, XML operations, object-oriented, regular expressions, output output ... Wait a minute. The basic is the Internet search example, and then groped themselves to write code, Because the. NET has been written for n years, the project process has been greatly influenced by. NET, with a three-tier architecture, and even a codebehind-like class for each PHP interface, without the time to understand the development framework under PHP and the projects that are built entirely on their own ideas. In this essay record, in order to later memo query, also want to get PHP professional guidance.

DAL

Originally wanted to encapsulate a dbutility class, but a few days time can not be fully understood PDO and apply freely, have to mix database related operations in the DAL.

Code

/* ***********************************************************
DAL
Wujian 2010-01-19
*********************************************************** */
Define ("Db_basic", "Mysql:host=localhost;dbname=asterisk");
Define ("Db_user", "root");
Define ("db_pswd", "root");

Class DAL
{
private static $pdo;

constructor function
Public Function __construct ()
{
//
}

Determine if the grouping exists
Public Function groupexist ($grpid)
{
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "Select COUNT (*) from Extension_group WHERE ' grp_id ' =". $grpid;
$rs = Query ($cmd), PDO, $this
$arr = $rs-Fetchall ();
$this, PDO = null;
if ($arr [0] [0] = = 1)
{
return true;
}
Else
{
return false;
}
}

Determine if an extension exists
Public Function extensionexist ($ETN)
{
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "Select COUNT (*) from Extension_email WHERE ' etn ' = '". $etn. " ' " ;
$rs = Query ($cmd), PDO, $this
$arr = $rs-Fetchall ();
$this, PDO = null;
if ($arr [0] [0] = = 1)
{
return true;
}
Else
{
return false;
}
}

Add extension (extension number, group Id,email, name, remark)
Public Function Extensionadd ($ETN, $grpid, $email, $name, $desc)
{
/* Prepare Way
$stmt = $this->pdo->prepare ("INSERT into Extensionemail (' ETN ', ' grp_id ', ' email ', ' nm ', ' desc ') VALUES (: A,: B,: C, :d,: E) ");
$stmt->bindparam (': A ', $ETN);
$stmt->bindparam (': B ', $grpid);
$stmt->bindparam (': C ', $email);
$stmt->bindparam (':d ', $name);
$stmt->bindparam (': E ', $desc);
$stmt->execute ();
*/

/* EXEC mode */
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "INSERT into Extension_email (' ETN ', ' grp_id ', ' email ', ' nm ', ' desc ') VALUES ('". $etn. " ', " . $grpid. " , ' " . $email. " ', ' " . $name. " ', ' " . $desc. " ') " ;
$s = $this, PDO---exec ($cmd);
$this, PDO = null;

if ($s = = 1) {
return true;
}
else {
return false;
}
}

Update extension (extension number, group Id,email, name, remark)
Public Function Extensionedit ($ETN, $grpid, $email, $name, $desc)
{
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "UPDATE extension_email SET ' grp_id ' =". $grpid. ", ' email ' = '". $email. "', ' nm ' = '". $name. "', ' desc ' = '". $desc. "' WHERE ' etn ' = '. $ETN;
$s = $this, PDO---exec ($cmd);
$this, PDO = null;

if ($s = = 1) {
return true;
}
else {
return false;
}
}

Activate extension
Public Function Extensionact ($ETN, $state)
{
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "UPDATE extension_email SET ' state ' =". $state. ", ' reg_time ' = Current_timestamp () WHERE ' etn ' =". $ETN;
$s = $this, PDO---exec ($cmd);
$this, PDO = null;

if ($s = = 1) {
return true;
}
else {
return false;
}
}

List of extensions
Public Function extensionlist ($grpid)
{
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "SELECT * from Extension_email WHERE ' grp_id ' =". $grpid;
if ($grpid = = 0)
{
$cmd = "SELECT * from Extension_email";
}
$rs = Query ($cmd), PDO, $this
$arr = $rs-Fetchall ();
$this, PDO = null;
return $arr;
}

Group List
Public Function Grouplist ()
{
$this, PDO = new PDO (Db_basic, Db_user, DB_PSWD);
$cmd = "SELECT * from Extension_group";
$rs = Query ($cmd), PDO, $this
$arr = $rs-Fetchall ();
$this, PDO = null;
return $arr;
}

}

?>

Bll

The business layer invokes the DAL and provides services for the UI

Code

/* ***********************************************************
Bll
Detections 2010-01-19
*********************************************************** */

Require "dal.php";

Class BLL
{
private static $myDAL;

constructor function
Public Function __construct ()
{
$this-mydal = new DAL ();
}

Determine if the grouping exists
Public Function groupexist ($grpid)
{
Return $this, Mydal-groupexist ($grpid);
}

Determine if an extension exists
Public Function extensionexist ($ETN)
{
Return $this, Mydal-extensionexist ($ETN);
}

Add extension (extension number, EMAIL, name, remark)
Public Function Extensionadd ($ETN, $grpid, $email, $name, $desc)
{
Return $this, Mydal, Extensionadd ($ETN, $grpid, $email, $name, $desc);
}

Update extension (extension number, group Id,email, name, remark)
Public Function Extensionedit ($ETN, $grpid, $email, $name, $desc)
{
Return $this, Mydal, Extensionedit ($ETN, $grpid, $email, $name, $desc);
}

Activate extension
Public Function Extensionact ($ETN, $state)
{
Return $this, Mydal-Extensionact ($ETN, $state);
}

List of extensions
Public Function extensionlist ($grpid)
{
Return $this, Mydal-extensionlist ($grpid);
}

Group List
Public Function Grouplist ()
{
Return $this, Mydal-grouplist ();
}

}

?>


Ui

Although there is no correlation between events and page controls in PHP like Codebehind, there is no object inheritance relationship between the classes and pages, but it is still felt that such separation logic will be clearer and may be used for a long time. Net of inertia thinking, do not like to put logic code nested in the page.

extension_add.php

Require "extension_add.class.php";
?>

extension_edit.class.php

Code

/* ***********************************************************
Add extension
Detections 2010-01-19
*********************************************************** */
Load public class
Require "class/common.php";
Load Business Class
Require "class/bll.php";
Define ("Auth_key", "Test");

Class Extensionadd
{
Page initialization
static function Pageload ()
{
$guidID = "";
0: Success 1: Invalid key 2: Submit data format error 3: Extension number already exists 4: Group does not exist 9: interface exception
$result = 9;

if (Isset ($_post ["Data"]))
{
Try
{
$doc = new DOMDocument ();
$doc-LoadXML ($_post ["Data"]);
$root = Simplexml_import_dom ($doc);

$the _authkey = AuthKey, head, $root;
$the _guidid = Guidid, head, $root;
$the _extension = $root, body and extension;
$the _groupid = $root, body and GroupID;
$the _email = $root, body-e email;
$the _name = $root, body, name;
$the _DESC = desc, body, $root

$guidID = $the _guidid;
Data Format Validation
if (Common:: Isextension ($the _extension) && Common:: Isint ($the _groupid) && Common:: Isemail ($the _ email))
{
Key verification
if ($the _authkey = = Auth_key)
{
Create a business object
$myBLL = new BLL ();

Whether the extension exists
if ($myBLL-extensionexist ($the _extension))
{
$result = 3;
}
Else
{
Whether the grouping exists
if ($myBLL-groupexist ($the _groupid))
{
if ($myBLL-Extensionadd ($the _extension, $the _groupid, $the _email, $the _name, $the _desc))
{
$result = 0;
}
}
Else
{
$result = 4;
}
}
}
Else
{
Invalid key
$result = 1;
}
}
Else
{
Incorrect data format
$result = 2;
}
}
catch (Exception $e)
{
Incorrect data format
$result = 2;
}
}
Else
{
Submission data format is incorrect
$result = 2;
}

$xml = " " ;
$xml = $xml. "" ;
$xml = $xml. " " ;
$xml = $xml. " " . $guidID. " " ;
$xml = $xml. " " ;
$xml = $xml. " " ;
$xml = $xml. " " . $result. " " ;
$xml = $xml. " " ;
$xml = $xml. " " ;

Specify page encoding
Header ("Content-type:text/xml; Charset=utf-8 ");
Echo ($xml);
}
}

Extensionadd:: Pageload ();

?>

extension_list.class.php

Code

/* ***********************************************************
List of extensions
Detections 2010-01-19
*********************************************************** */
Load Business Class
Require "class/bll.php";
Define ("Auth_key", "Test");

Class Extensionlist
{
Page initialization
static function Pageload ()
{
$guidID = "";
0: Success 1: Invalid key 2: Submit data format error 3: Extension number already exists 4: Group does not exist 9: interface exception
$ETN = "";

if (Isset ($_post ["Data"]))
{
Try
{
$doc = new DOMDocument ();
$doc-LoadXML ($_post ["Data"]);
$root = Simplexml_import_dom ($doc);

$the _authkey = AuthKey, head, $root;
$the _guidid = Guidid, head, $root;
$the _groupid = $root, body and GroupID;

$guidID = $the _guidid;
Key verification
if ($the _authkey = = Auth_key)
{
Create a business object
$myBLL = new BLL ();
$arr = $myBLL extensionlist ($the _groupid);
foreach ($arr as $row)
{
$ETN = $etn. " " ;
$ETN = $etn. " " . $row ["ETN"]. "" ;
$ETN = $etn. " " . $row ["grp_id"]. "" ;
$ETN = $etn. " " . $row ["Email"]. "" ;
$ETN = $etn. " " . $row ["NM"]. "" ;
$ETN = $etn. " " . $row [ " desc " ] . " " ;
$ETN = $etn. " " . $row ["State"]. "" ;
$ETN = $etn. " " . $row ["Reg_time"]. "" ;
$ETN = $etn. " " ;
}
}
}
catch (Exception $e)
{
//
}
}

$xml = " " ;
$xml = $xml. " " ;
$xml = $xml. " " ;
$xml = $xml. " " . $guidID. " " ;
$xml = $xml. " " ;
$xml = $xml. " " . $etn. " " ;
$xml = $xml. " " ;

Specify page encoding
Header ("Content-type:text/xml; Charset=utf-8 ");
Echo ($xml);
}
}

Extensionlist:: Pageload ();

?>

Common

The main application of regular expressions

Code

/* ***********************************************************
Public
Detections 2010-01-19
*********************************************************** */

Class Common
{
Non-null validation
static function Isnon ($STR)
{
return true;
}

Positive integer Validation
static function Isint ($STR)
{
$reg = "^[0-9]+$";
if (Ereg ($reg, $STR))
{
return true;
}
Else
{
return false;
}
}

Extension Number format verification
static function Isextension ($STR)
{
$reg = "^[0-9]+$";
if (Ereg ($reg, $STR))
{
return true;
}
Else
{
return false;
}
}

Email format Verification
static function Isemail ($STR)
{
$reg = "^ ([_a-za-z0-9+\.] +@ ([_a-za-z0-9]+\.) +[a-za-z0-9]{2,4})? $ ";
if (Ereg ($reg, $STR))
{
return true;
}
Else
{
return false;
}
}
}

?>

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.