Tuesday, November 26, 2013

insert , update ,delete example using zend framework simple

Hello Guys we giving you simple add,edit ,delete example using zend framework you can learn step by step
first configure your database connection for zend application
------------------------------------------------------------------------
D:\xampp\htdocs\mysite\application\configs\application.ini
-------------------------------------------------------------------------
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = ""
resources.db.params.dbname = "zend"
resources.layout.layoutpath = APPLICATION_PATH "/layouts"
phpSettings.date.timezone = "UTC"
[staging : production]
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

---------------------------------------create user table ---------------------------------------------------------
CREATE TABLE IF NOT EXISTS `jos_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `username` varchar(150) DEFAULT '',
  `email` varchar(100) NOT NULL DEFAULT '',
  `password` varchar(100) NOT NULL DEFAULT '',
  `usertype` varchar(25) NOT NULL DEFAULT '',
  `block` tinyint(4) NOT NULL DEFAULT '0',
  `sendEmail` tinyint(4) DEFAULT '0',
  `gid` tinyint(3) unsigned NOT NULL DEFAULT '1',
  `registerDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `lastvisitDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `activation` varchar(100) NOT NULL DEFAULT '',
  `params` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `usertype` (`usertype`),
  KEY `idx_name` (`name`),
  KEY `gid_block` (`gid`,`block`),
  KEY `username` (`username`),
  KEY `email` (`email`)
) ENGINE=MyISAM AUTO_INCREMENT=122 DEFAULT CHARSET=utf8;
--------------------------------------------------------------------------------------------------------
Stp->1 create a form class user.php location : D:\xampp\htdocs\zend\application\forms\user.php
-------------------------------------paste below code-----------------------------------------------------------
<?php
class Form_User extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
$this->setName('album');
$id = new Zend_Form_Element_Hidden('id');
$name = new Zend_Form_Element_Text('name');
$name->setLabel('Name')
->setRequired(true)->addFilter('StripTags')->addFilter('StringTrim')->addValidator('NotEmpty');
$username = new Zend_Form_Element_Text('username');
$username->setLabel('Username')->setRequired(true)->addFilter('StripTags')->addFilter('StringTrim')->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('email');
$email->setLabel('Email')->setRequired(true)->addFilter('StripTags')->addFilter('StringTrim')->addValidator('NotEmpty')->addValidator('EmailAddress',  TRUE  );
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $name,$username, $email, $submit));
}
}
-----------------------------------------------------------------------------------------------------------------
Stp->2 create a controller class user.php location : D:\xampp\htdocs\mysite\application\controllers\BbcController.php
-----------------------------------------------------------------------------------------------------------------
<?php
class BbcController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    function indexAction()
{

$this->view->title = "Bbc by bikash";
$this->view->headTitle($this->view->title, 'PREPEND');
$user = new Model_DbTable_Bbc();

$this->view->bbc = $user->fetchAll();

}
function insertAction()
{
       $this->view->title = "insert new user";
$this->view->headTitle($this->view->title, 'PREPEND');
$form = new Form_User();
$form->submit->setLabel('Insert');
$this->view->form = $form;


if ($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData))
{
$name = $form->getValue('name');
$username=$form->getValue('username');

$email = $form->getValue('email');
$albums = new Model_DbTable_Bbc();
$albums->insertuser($name, $username,$email);
$this->_redirect('/bbc');
}
else
{
$form->populate($formData);
}
}
}
    function updateAction(){
   $this->view->title = "Upadate user";
$this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Form_User();
$form->submit->setLabel('update');
$this->view->form = $form;
if ($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData))
{
$id = (int)$form->getValue('id');
$name = $form->getValue('name');
$username=$form->getValue('username');
$email = $form->getValue('email');
$albums = new Model_DbTable_Bbc();
$albums->updateuser($id, $name,$username, $email);
$this->_redirect('/bbc');
}
else
{
$form->populate($formData);
}
}
else
{
$id = $this->_getParam('id', 0);
if ($id > 0)
{
$albums = new Model_DbTable_Bbc();
$form->populate($albums->getAlbum($id));
}
}

    }

public function deleteAction()
{
$this->view->title = "Delete album";
$this->view->headTitle($this->view->title, 'PREPEND');
if ($this->getRequest()->isPost())
{
$del = $this->getRequest()->getPost('del');
if ($del == 'Yes')
{
$id = $this->getRequest()->getPost('id');
$albums = new Model_DbTable_Bbc();
$albums->deleteUser($id);
}
$this->_redirect('/bbc');
}
else
{
$id = $this->_getParam('id', 0);
$albums = new Model_DbTable_Bbc();
$this->view->album = $albums->getAlbum($id);
}
}


}
------------------------------------------create view-----------------------------------------------------
Stp->1 create views location : D:\xampp\htdocs\mysite\application\views\scripts\bbc\index.phtml,insert.phtml,update.phtml,delete.phtml
--------------------------------------index.phtml -----------------------------------------------------

<p><a href="<?php echo $this->url(array('controller'=>'bbc',
'action'=>'insert'));?>">Add new album</a></p>
<table>
<tr>
<th>Name</th>
<th>Username</th>
<th>Email address</th>
<th>&nbsp;</th>
</tr>
<?php foreach($this->bbc as $album) : ?>
<tr>
<td><?php echo $this->escape($album->name);?></td>
<td><?php echo $this->escape($album->username);?></td>
<td><?php echo $this->escape($album->email);?></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'bbc',
'action'=>'update', 'id'=>$album->id));?>">Edit</a>
<a href="<?php echo $this->url(array('controller'=>'bbc',
'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
--------------------------------------insert.phtml -----------------------------------------------------
<?php echo $this->form ;?>
--------------------------------------update.phtml -----------------------------------------------------
<?php echo $this->form ;?>
--------------------------------------delete.phtml -----------------------------------------------------
<p>Are you sure that you want to delete
'<?php echo $this->escape($this->album['name']); ?>' by
'<?php echo $this->escape($this->album['email']); ?>'?
</p>
<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $this->album['id']; ?>" />
<input type="submit" name="del" value="Yes" />
<input type="submit" name="del" value="No" />
</div>

-------------------------hope it may help you--:)--:)-------------------------------------------:)

1 comment:

  1. Support

    I have to follow the instructions but failed
    You can send the project to mail me?
    I recently learned, it is imperative
    Thanks

    ReplyDelete