Tag Archives: databases

Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway2

 Here you have your form fold, the file stuction should look like this:

crud4

here is the code for userForm.php

<?php
namespace CsnUser\Form;

use Zend\Form\Form;

class UserForm extends Form
{
public function __construct($name = null)
{
parent::__construct(‘user’);
$this->setAttribute(‘method’, ‘post’);

$this->add(array(
‘name’ =>’usr_name’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Username’,
),
));

$this->add(array(
‘name’ =>’usr_password’,
‘attributes’=> array(
‘type’=>’password’,
),
‘options’=>array(
‘label’=>’Password’,
),
));

$this->add(array(
‘name’ =>’usr_email’,
‘attributes’=> array(
‘type’=>’email’,
),
‘options’=>array(
‘label’=>’E-mail’,
),
));

$this->add(array(
‘name’ =>’usrl_id’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Role’,
‘value_options’=>array(
‘1’=>’Public’,
‘2’=>’Member’,
‘3’=>’Admin’,
),
),
));

$this->add(array(
‘name’ =>’lng_id’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Language’,
‘value_options’=>array(
‘1’=>’English’,
‘2’=>’French’,
‘3’=>’German’,
),
),
));

$this->add(array(
‘name’ =>’usr_active’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Active’,
‘value_options’=>array(
‘0’=>’No’,
‘1’=>’Yes’,

),
),
));

$this->add(array(
‘name’ =>’usr_question’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Question’,
),
));

$this->add(array(
‘name’ =>’usr_answer’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Answer’,
),
));

$this->add(array(
‘name’ =>’usr_picture’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Picture URL’,
),
));

$this->add(array(
‘name’ =>’usr_picture’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Picture URL’,
),
));

$this->add(array(
‘name’ => ‘usr_password_salt’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Password Salt’,
),
));

$this->add(array(
‘name’ => ‘usr_registration_date’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Registration Date’,
),
));

$this->add(array(
‘name’ => ‘usr_registration_token’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Registration Token’,
),
));

$this->add(array(
‘name’ => ‘usr_email_confirmed’,
‘type’ => ‘Zend\Form\Element\Select’,
‘options’ => array(
‘label’ => ‘E-mail was confirmed?’,
‘value_options’ => array(
‘0’ => ‘No’,
‘1’ => ‘Yes’,
),
),
));

$this->add(array(
‘name’ => ‘submit’,
‘attributes’ => array(
‘type’  => ‘submit’,
‘value’ => ‘Go’,
‘id’ => ‘submitbutton’,
),
));
}
}

here is the code for userFilter.php(you could use javascript like backbone to validate the form instead will more faster)

<?php
namespace CsnUser\Form;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;

class UserFilter extends InputFilter
{
public function __construct() {
$this->add(array(
‘name’=>’usr_name’,
‘required’=>true,
‘filters’=>array(
array(‘name’=>’StripTags’),
array(‘name’=>’StringTrim’),
),
‘validators’=>array(
array(
‘name’    => ‘StringLength’,
‘options’ => array(
‘encoding’ => ‘UTF-8’,
‘min’      => 1,
‘max’      => 100,
),
),
),
));

$this->add(array(
‘name’       => ‘usr_email’,
‘required’   => true,
‘validators’ => array(
array(
‘name’ => ‘EmailAddress’
),
),
));

$this->add(array(
‘name’     => ‘usr_password’,
‘required’ => true,
‘filters’  => array(
array(‘name’ => ‘StripTags’),
array(‘name’ => ‘StringTrim’),
),
‘validators’ => array(
array(
‘name’    => ‘StringLength’,
‘options’ => array(
‘encoding’ => ‘UTF-8’,
‘min’      => 6,
‘max’      => 12,
),
),
),
));

$this->add(array(
‘name’     => ‘usr_active’,
‘required’ => true,
‘filters’  => array(
array(‘name’ => ‘Int’),
),
‘validators’ => array(
array(
‘name’    => ‘Digits’,
),
),
));

}

}

the index.phtml code:for retrieve data from your database, here is your presentation page, on you browser type:http://zf2.local/csn-user/user/index

should look like this:

crud3

 

<h1>Index = Retrieve</h1>
<a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘create’)); ?>”>Create</a>
<table>
<tr>
<th>usr_id</th>
<th>usr_name</th>
<th>usr_email</th>
</tr>
<?php
foreach ($rowset as $row) {
?>
<tr>
<td><?php  echo $row->usr_id; // The row is an ArrayObject $row->usr_id and $row[‘usr_id’] with both work?></td>
<td><?php  echo $row[‘usr_name’]; ?></td>
<td><?php  echo $row[‘usr_email’]; ?></td>
<td><a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘update’, ‘id’ => $row[‘usr_id’])); ?>”>Edit</a></td>
<td><a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘delete’, ‘id’ => $row[‘usr_id’]));?>” onclick=”return confirm(‘Do you rely want to delete the record?’)”>Delete</a></td>
</tr>
<?php
}
?>
</table>

your update.phtml code:

<h1>Update</h1>
<?php
$form = $this->form;
$form->prepare();
$form->setAttribute(‘action’,$this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘update’, ‘id’ => $id)));
$form->setAttribute(‘method’,’post’);

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

your create.phtml code:

<h1>Create an new user</h1>
<?php
$form = $this->form;
$form->prepare();
// Assuming the “contact/process” route exists…
$form->setAttribute(‘action’, $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘create’))); //’contact/process’));
// Set the method attribute for the form
$form->setAttribute(‘method’, ‘post’);

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

And you don’t need a delete view

 

Advertisements

Zend Framework2: Tutorial Part3 use TableGateway add references to the database connection

Just go to modify “config/autoload/global.php”

return array(
‘db’ => array(
‘driver’ => ‘Pdo’,
‘dsn’ => ‘mysql:dbname=test;host=localhost’,
‘username’ => ‘db_user’,
‘password’ => ”,
‘driver_options’ => array(
PDO::MYSQL_ATTR_INIT_COMMAND => ‘SET NAMES \’UTF8\”
),
),
‘service_manager’ => array(
‘factories’ => array(
‘Zend\Db\Adapter\Adapter’
=> ‘Zend\Db\Adapter\AdapterServiceFactory’,
),
),
);

a paypal shopping cart with php here is the source code for you catalog–continue

first, create your catalog and product database, here is the source code:

— phpMyAdmin SQL Dump
— version 2.10.3
http://www.phpmyadmin.net

— Host: localhost
— Generation Time: May 24, 2013 at 02:43 PM
— Server version: 5.0.51
— PHP Version: 5.2.6

SET SQL_MODE=”NO_AUTO_VALUE_ON_ZERO”;


— Database: `bazaar`

CREATE DATABASE `bazaar` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
USE `bazaar`;

— ——————————————————–


— Table structure for table `categories`

CREATE TABLE `categories` (
`CatID` int(11) NOT NULL auto_increment,
`Name` varchar(50) NOT NULL,
PRIMARY KEY  (`CatID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;


— Dumping data for table `categories`

INSERT INTO `categories` VALUES (1, ‘Bazaar Ceramics Catalogue’);

— ——————————————————–


— Table structure for table `products`

CREATE TABLE `products` (
`ProdID` int(11) NOT NULL auto_increment,
`CartID` int(11) NOT NULL,
`Name` varchar(50) NOT NULL,
`Cost` decimal(8,2) NOT NULL,
`Image` varchar(50) NOT NULL default ‘NoImage.jpg’,
`Description` varchar(250) default NULL,
PRIMARY KEY  (`ProdID`),
KEY `CartID` (`CartID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=11 ;


— Dumping data for table `products`

INSERT INTO `products` VALUES (1, 1, ‘Copper Red Dish 001’, 450.00, ‘bcpot002_smaller.jpg’, ‘Shallow Copper Red dish form showing distinctive qualities of this traditional reduction fired glaze.  Fired to 1300 degrees’);
INSERT INTO `products` VALUES (2, 1, ‘Copper Red Bottle 001’, 550.00, ‘bcpot003_smaller.jpg’, ‘Copper Red bottle form.  Reduction fired to 1300 degrees’);
INSERT INTO `products` VALUES (3, 1, ‘Chun bowl 001’, 350.00, ‘bcpot006_smaller.jpg’, ‘Blue Chun bowl with tea stain rim over terracotta.  Fired to 1300 degrees’);
INSERT INTO `products` VALUES (4, 1, ‘Moonscape bowl 001’, 320.00, ‘bcpot009_smaller.jpg’, ‘High Calcium bowl with white glaze over blue slip.  Fired to 1280 degrees’);
INSERT INTO `products` VALUES (5, 1, ‘Crystalline bowl 001’, 250.00, ‘bcpot013_smaller.jpg’, ‘Blue Green Crystalline glaze.  Narrow-foot bowl with clear glaze outside.  Fired to 1300 degrees’);
INSERT INTO `products` VALUES (6, 1, ‘Carved vase form 001’, 450.00, ‘bcpot014_smaller.jpg’, ‘Carved Iron stoneware vase form.  Oxidation lustre on rim.  Fired to 1280 degrees’);
INSERT INTO `products` VALUES (7, 1, ‘Carved vase form 002’, 450.00, ‘bcpot016_smaller.jpg’, ‘Carved dry matt calcium vase form.  Fired to 1280 degrees’);
INSERT INTO `products` VALUES (8, 1, ‘Carved Bottle form 001’, 600.00, ‘bcpot018_smaller.jpg’, ‘Carved dry matt calcium bottle form.  Lustre rim.  Fired to 1280 degrees’);
INSERT INTO `products` VALUES (9, 1, ‘Dry earth vase form 001’, 500.00, ‘bcpot012_smaller.jpg’, ‘Vase form.  White slip over earthenware clay. Fired to 1000 degrees’);
INSERT INTO `products` VALUES (10, 1, ‘Carnivale cup and saucer 001’, 150.00, ‘bcpot008_smaller.jpg’, ‘ Cup and Saucer with Carnivale design.  Coloured slips under clear glaze.  Earthenware’);