0% found this document useful (0 votes)
131 views

CRUD Application in Zend Framework

This document describes the setup and configuration of a CRUD (create, read, update, delete) application using the Zend Framework PHP library. It includes code for configuring the database connection, controllers, models and forms. Controllers allow adding, editing and deleting records to a divisions table in the database. Forms are used to collect and validate user input.

Uploaded by

Humus Balzerum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

CRUD Application in Zend Framework

This document describes the setup and configuration of a CRUD (create, read, update, delete) application using the Zend Framework PHP library. It includes code for configuring the database connection, controllers, models and forms. Controllers allow adding, editing and deleting records to a divisions table in the database. Forms are used to collect and validate user input.

Uploaded by

Humus Balzerum
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Localization of E-Governance Project (DIT, UNDP) vishanta.rayamajhi@gmail.

com 1 | P age

CRUD Application in Zend Framework


Prepared by: Er. Vishanta Rayamajhi, Intl IT Expert, UNDP






Localization of E-Governance Project (DIT, UNDP) [email protected] 2 | P age



Localization of E-Governance Project (DIT, UNDP) [email protected] 3 | P age

public/index.php
<?php

er r or _r epor t i ng( E_ALL| E_STRI CT) ;
i ni _set ( ' di spl ay_er r or s' , 1) ;
dat e_def aul t _t i mezone_set ( ' Asi a/ Thi mphu' ) ;

/ / di r ect or y set up and cl ass l oadi ng
set _i ncl ude_pat h( ' . ' . PATH_SEPARATOR . ' . . / l i br ar y/ '
. PATH_SEPARATOR . ' . . / appl i cat i on/ model s'
. PATH_SEPARATOR . get _i ncl ude_pat h( ) ) ;

i ncl ude " Zend/ Loader . php" ;
Zend_Loader : : r egi st er Aut ol oad( ) ;

/ / l oad conf i gur at i on
$conf i g = new Zend_Conf i g_I ni ( ' . . / appl i cat i on/ conf i g. i ni ' , ' di t ' ) ;
$r egi st r y = Zend_Regi st r y: : get I nst ance( ) ;
$r egi st r y- >set ( ' conf i g' , $conf i g) ;

/ / set up dat abase
$db = Zend_Db: : f act or y( $conf i g- >db) ;
Zend_Db_Tabl e: : set Def aul t Adapt er ( $db) ;

/ / set up cont r ol l er
$f r ont Cont r ol l er = Zend_Cont r ol l er _Fr ont : : get I nst ance( ) ;
$f r ont Cont r ol l er - >t hr owExcept i ons( t r ue) ;
$f r ont Cont r ol l er - >set Cont r ol l er Di r ect or y( ' . . / appl i cat i on/ cont r ol l er s' ) ;

Zend_Layout : : st ar t Mvc( ar r ay( ' l ayout Pat h' =>' . . / appl i cat i on/ l ayout s' ) ) ;

/ / r un!
$f r ont Cont r ol l er - >di spat ch( ) ;
public/.htaccess
# Rewr i t e r ul es f or Zend Fr amewor k
Rewr i t eEngi ne on
Rewr i t eCond %{REQUEST_FI LENAME} ! - f
Rewr i t eRul e . * i ndex. php
application/config.ini
[ di t ]
db. adapt er = PDO_MYSQL
db. par ams. host = l ocal host
db. par ams. user name = user
db. par ams. passwor d = passwor d
db. par ams. dbname = zend- di t
Localization of E-Governance Project (DIT, UNDP) [email protected] 4 | P age

application/controllers/IndexController.php
<?php

cl ass I ndexCont r ol l er ext ends Zend_Cont r ol l er _Act i on
{
f unct i on i ndexAct i on( )
{
$t hi s- >vi ew- >t i t l e = " Home Page" ;
$di vi si ons = new Di t ( ) ;
$t hi s- >vi ew- >di vi si ons = $di vi si ons- >f et chAl l ( ) ;

}

f unct i on addAct i on( )
{
$t hi s- >vi ew- >t i t l e = " Add New Di vi si on" ;

$f or m= new Di t For m( ) ;
$f or m- >submi t - >set Label ( ' Add' ) ;
$t hi s- >vi ew- >f or m= $f or m;

i f ( $t hi s- >_r equest - >i sPost ( ) ) {
$f or mDat a = $t hi s- >_r equest - >get Post ( ) ;
i f ( $f or m- >i sVal i d( $f or mDat a) ) {
$di vi si ons = new Di t ( ) ;
$r ow = $di vi si ons- >cr eat eRow( ) ;
$r ow- >di vi si on_name = $f or m- >get Val ue( ' di vi si on_name' ) ;
$r ow- >est abl i shed_on = $f or m- >get Val ue( ' est abl i shed_on' ) ;
$r ow- >st af f _no = $f or m- >get Val ue( ' st af f _no' ) ;
$r ow- >di vi si on_head = $f or m- >get Val ue( ' di vi si on_head' ) ;
$r ow- >di vi si on_head_name = $f or m- >get Val ue( ' di vi si on_head_name' ) ;
$r ow- >emai l = $f or m- >get Val ue( ' emai l ' ) ;
$r ow- >save( ) ;

$t hi s- >_r edi r ect ( ' / ' ) ;
} el se {
$f or m- >popul at e( $f or mDat a) ;
}
}
}

f unct i on edi t Act i on( )
{
$t hi s- >vi ew- >t i t l e = " Edi t Di vi si on" ;

$f or m= new Di t For m( ) ;
$f or m- >submi t - >set Label ( ' Save' ) ;
$t hi s- >vi ew- >f or m= $f or m;

i f ( $t hi s- >_r equest - >i sPost ( ) ) {
$f or mDat a = $t hi s- >_r equest - >get Post ( ) ;
i f ( $f or m- >i sVal i d( $f or mDat a) ) {
$di vi si ons = new Di t ( ) ;
$i d = ( i nt ) $f or m- >get Val ue( ' di vi si on_i d' ) ;
$r ow = $di vi si ons- >f et chRow( ' di vi si on_i d=' . $i d) ;
$r ow- >di vi si on_name = $f or m- >get Val ue( ' di vi si on_name' ) ;
$r ow- >est abl i shed_on = $f or m- >get Val ue( ' est abl i shed_on' ) ;
Localization of E-Governance Project (DIT, UNDP) [email protected] 5 | P age

$r ow- >st af f _no = $f or m- >get Val ue( ' st af f _no' ) ;
$r ow- >di vi si on_head = $f or m- >get Val ue( ' di vi si on_head' ) ;
$r ow- >di vi si on_head_name = $f or m- >get Val ue( ' di vi si on_head_name' ) ;
$r ow- >emai l = $f or m- >get Val ue( ' emai l ' ) ;
$r ow- >save( ) ;

$t hi s- >_r edi r ect ( ' / ' ) ;
} el se {
$f or m- >popul at e( $f or mDat a) ;
}
} el se {
$i d = ( i nt ) $t hi s- >_r equest - >get Par am( ' i d' , 0) ;
i f ( $i d > 0) {
$di vi si ons = new Di t ( ) ;
$di vi si on = $di vi si ons- >f et chRow( ' di vi si on_i d=' . $i d) ;
$f or m- >popul at e( $di vi si on- >t oAr r ay( ) ) ;
}
}
}

f unct i on del et eAct i on( )
{
$t hi s- >vi ew- >t i t l e = " Del et e Di vi si on" ;

i f ( $t hi s- >_r equest - >i sPost ( ) ) {
$i d = ( i nt ) $t hi s- >_r equest - >get Post ( ' di vi si on_i d' ) ;
$del = $t hi s- >_r equest - >get Post ( ' del ' ) ;
i f ( $del == ' Yes' && $i d > 0) {
$di vi si ons = new Di t ( ) ;
$wher e = ' di vi si on_i d=' . $i d;
$di vi si ons- >del et e( $wher e) ;
}
$t hi s- >_r edi r ect ( ' / ' ) ;
} el se {
$i d = ( i nt ) $t hi s- >_r equest - >get Par am( ' i d' ) ;
i f ( $i d > 0) {
$di vi si ons = new Di t ( ) ;
$t hi s- >vi ew- >di vi si on = $di vi si ons- >f et chRow( ' di vi si on_i d=' . $i d) ;
}
}
}
}




application/models/Dit.php
Localization of E-Governance Project (DIT, UNDP) [email protected] 6 | P age

<?php

cl ass Di t ext ends Zend_Db_Tabl e
{
pr ot ect ed $_name = ' di t ' ;
}
application/models/DitForm.php
<?php

cl ass Di t For mext ends Zend_For m
{
publ i c f unct i on __const r uct ( $opt i ons = nul l )
{
par ent : : __const r uct ( $opt i ons) ;
$t hi s- >set Name( ' di t ' ) ;

$di vi si on_name = new Zend_For m_El ement _Text ( ' di vi si on_name' ) ;
$di vi si on_name- >set Label ( ' Di vi si on Name: ' )
- >set Requi r ed( t r ue)
- >addFi l t er ( ' St r i pTags' )
- >addFi l t er ( ' St r i ngTr i m' )
- >addVal i dat or ( ' Not Empt y' ) ;

$est abl i shed_on = new Zend_For m_El ement _Text ( ' est abl i shed_on' ) ;
$est abl i shed_on- >set Label ( ' Dat e of Est abl i shment : ' )
- >set Requi r ed( t r ue)
- >addFi l t er ( ' St r i pTags' )
- >addFi l t er ( ' St r i ngTr i m' )
- >addVal i dat or ( ' Not Empt y' ) ;

$st af f _no = new Zend_For m_El ement _Text ( ' st af f _no' ) ;
$st af f _no- >set Label ( ' No of St af f s: ' )
- >set Requi r ed( t r ue)
- >addFi l t er ( ' St r i pTags' )
- >addFi l t er ( ' St r i ngTr i m' )
- >addVal i dat or ( ' Not Empt y' ) ;

$di vi si on_head = new Zend_For m_El ement _Text ( ' di vi si on_head' ) ;
$di vi si on_head- >set Label ( ' Head of Di vi si on: ' )
- >set Requi r ed( t r ue)
- >addFi l t er ( ' St r i pTags' )
- >addFi l t er ( ' St r i ngTr i m' )
- >addVal i dat or ( ' Not Empt y' ) ;

$di vi si on_head_name = new Zend_For m_El ement _Text ( ' di vi si on_head_name' ) ;
$di vi si on_head_name- >set Label ( ' Di vi si on Head Name: ' )
- >set Requi r ed( t r ue)
- >addFi l t er ( ' St r i pTags' )
- >addFi l t er ( ' St r i ngTr i m' )
- >addVal i dat or ( ' Not Empt y' ) ;

$emai l = new Zend_For m_El ement _Text ( ' emai l ' ) ;
$emai l - >set Label ( ' Emai l Addr ess: ' )
- >set Requi r ed( t r ue)
Localization of E-Governance Project (DIT, UNDP) [email protected] 7 | P age

- >addFi l t er ( ' St r i pTags' )
- >addFi l t er ( ' St r i ngTr i m' )
- >addVal i dat or ( ' Not Empt y' ) ;

$di vi si on_i d = new Zend_For m_El ement _Hi dden( ' di vi si on_i d' ) ;

$submi t = new Zend_For m_El ement _Submi t ( ' submi t ' ) ;
$submi t - >set At t r i b( ' i d' , ' submi t but t on' ) ;

$t hi s- >addEl ement s( ar r ay( $di vi si on_name, $est abl i shed_on, $st af f _no,
$di vi si on_head, $di vi si on_head_name, $emai l , $di vi si on_i d, $submi t ) ) ;
}
}
application/layouts/layout.phtml
<! DOCTYPE ht ml PUBLI C " - / / W3C/ / DTD XHTML 1. 0 Tr ansi t i onal / / EN"
" ht t p: / / www. w3. or g/ TR/ xht ml 1/ DTD/ xht ml 1- t r ansi t i onal . dt d" >
<ht ml xml ns=" ht t p: / / www. w3. or g/ 1999/ xht ml " xml : l ang=" en" l ang=" en" >
<head>
<met a ht t p- equi v=" Cont ent - Type" cont ent =" t ext / ht ml ; char set =ut f - 8" / >
<t i t l e><?php echo $t hi s- >escape( $t hi s- >t i t l e) ; ?></ t i t l e>
<l i nk r el =" st yl esheet " t ype=" t ext / css" medi a=" scr een" hr ef =" <?php echo $t hi s-
>baseUr l ( ) ; ?>/ css/ si t e. css" / >
</ head>

<body>

<di v i d=" header " >
<h1>Depar t ment of I nf or mat i on Technol ogy</ h1>
<h2>Mi ni st r y of I nf or mat i on and Communi cat i on, RGoB</ h2>
</ di v>

<di v i d=" cont ent " cl ass=" cont ent " >
<st r ong><?php echo $t hi s- >escape( $t hi s- >t i t l e) ; ?></ st r ong> &nbsp; | &nbsp; <a
hr ef =" <?php echo $t hi s- >ur l ( ar r ay( ' cont r ol l er ' =>' i ndex' ,
' act i on' =>' i ndex' ) ) ; ?>" >Home</ a>
<hr st yl e=" wi dt h: 400px; " >
<?php echo $t hi s- >l ayout ( ) - >cont ent ; ?>
</ di v>

<br >

<di v i d=" f oot er " al i gn=" cent er " >
&copy Copyr i ght 2008, 2009 Er . Vi shant a Rayamaj hi &nbsp; | &nbsp; cont act devel oper
<a hr ef =" mai l t o: vi shant a. r ayamaj hi @gmai l . com" >vi shant a. r ayamaj hi @gmai l . com</ a>
</ di v>

</ body>
</ ht ml >
application/views/helpers/BaseUrl.php
Localization of E-Governance Project (DIT, UNDP) [email protected] 8 | P age

<?php

cl ass Zend_Vi ew_Hel per _BaseUr l
{
f unct i on baseUr l ( )
{
$f c = Zend_Cont r ol l er _Fr ont : : get I nst ance( ) ;
r et ur n $f c- >get BaseUr l ( ) ;
}
}
application/views/scripts/index/index.phtml
<br >

<di v cl ass=" di v_bl ock" >
&r aquo;
<a hr ef =" <?php echo $t hi s- >ur l ( ar r ay( ' cont r ol l er ' =>' i ndex' , ' act i on' =>' add' ) ) ; ?>" >
Add New Di vi si on
</ a>
</ di v>

<br >

<t abl e>
<capt i on>Di vi si ons of DI T</ capt i on>
<t r >
<t h>SN</ t h>
<t h>Di vi si on Name</ t h>
<t h>Dat e of Est abl i shment </ t h>
<t h>No of St af f s</ t h>
<t h>Head of Di vi si on</ t h>
<t h>Di vi si on Head Name</ t h>
<t h>Emai l Addr ess</ t h>
<t h>&nbsp; Edi t &nbsp; </ t h>
<t h>Del et e</ t h>
</ t r >

<?php f or each( $t hi s- >di vi si ons as $di vi si on) : ?>
<t r >
<t d><?php echo $t hi s- >escape( $di vi si on- >di vi si on_i d) ; ?></ t d>
<t d><?php echo $t hi s- >escape( $di vi si on- >di vi si on_name) ; ?></ t d>
<t d al i gn=" cent er " ><?php echo $t hi s- >escape( $di vi si on- >est abl i shed_on) ; ?></ t d>
<t d al i gn=" cent er " ><?php echo $t hi s- >escape( $di vi si on- >st af f _no) ; ?></ t d>
<t d al i gn=" cent er " ><?php echo ( $t hi s- >escape( $di vi si on- >di vi si on_head) == ' Y' ) ?
' Yes' : ' No' ; ?></ t d>
<t d><?php echo $t hi s- >escape( $di vi si on- >di vi si on_head_name) ; ?></ t d>
<t d><?php echo $t hi s- >escape( $di vi si on- >emai l ) ; ?></ t d>
<t d al i gn=" cent er " >
<a hr ef =" <?php echo $t hi s- >ur l ( ar r ay( ' cont r ol l er ' =>' i ndex' , ' act i on' =>' edi t ' ,
' i d' =>$di vi si on- >di vi si on_i d) ) ; ?>" >Edi t </ a>
</ t d>
<t d al i gn=" cent er " >
<a hr ef =" <?php echo $t hi s- >ur l ( ar r ay( ' cont r ol l er ' =>' i ndex' ,
' act i on' =>' del et e' , ' i d' =>$di vi si on- >di vi si on_i d) ) ; ?>" >Del et e</ a>
</ t d>
Localization of E-Governance Project (DIT, UNDP) [email protected] 9 | P age

</ t r >
<?php endf or each; ?>

</ t abl e>
application/views/scripts/index/add.phtml
<?php echo $t hi s- >f or m; ?>
application/views/scripts/index/edit.phtml
<?php echo $t hi s- >f or m; ?>
application/views/scripts/index/delete.phtml
<?php i f ( $t hi s- >di vi si on) : ?>
<p>Ar e you sur e t hat you want t o del et e
[ <st r ong><?php echo $t hi s- >escape( $t hi s- >di vi si on- >di vi si on_name) ; ?></ st r ong>]
est abl i shed at
[ <?php echo $t hi s- >escape( $t hi s- >di vi si on- >est abl i shed_on) ; ?>] ?
</ p>
<f or mact i on=" <?php echo $t hi s- >ur l ( ar r ay( ' act i on' =>' del et e' ) ) ; ?>" met hod=" post " >
<di v>
<i nput t ype=" hi dden" name=" di vi si on_i d" val ue=" <?php echo $t hi s- >di vi si on-
>di vi si on_i d; ?>" / >
<i nput t ype=" submi t " name=" del " val ue=" Yes" / >
<i nput t ype=" submi t " name=" del " val ue=" No" / >
</ di v>
</ f or m>
<?php el se: ?>
<p>Cannot f i nd di vi si on. </ p>
<?php endi f ; ?>

You might also like