I am trying to build a cart system, after successfully entering the item to the cart. how can I display items according shop. I was a bit constrained here.
Controllers
function to_cart () {
$data = array(
array(
'id' => '1',
'qty' => 1,
'price' => 2.400,
'name' => 'Asus Eeepc',
'store' => 'My_store 1'
),
array(
'id' => '2',
'qty' => 1,
'price' => 1.500,
'name' => 'Accer AspireOne',
'store' => 'My_store 1'
),
array(
'id' => '3',
'qty' => 1,
'price' => 4.000,
'name' => 'Toshiba Satelite',
'store' => 'My_store 2'
),
array(
'id' => '4',
'qty' => 1,
'price' => 2.700,
'name' => 'Lenova ThinkCare',
'store' => 'My_store 1'
)
);
$this->cart->insert($data);
redirect($this->agent->referrer());
}
Views
<?php foreach ($this->cart->contents() as $item) {
echo '<td>'.$item['no'].'</td>';
echo '<td>'.$item['name'].'</td>';
echo '<td>'.$item['price'].'</td>';
echo '<td>'.$item['qty'].'</td>';
echo '<td>'.$item['store'].'</td>';
?>
Related
I have 200 input field in my page. All are radio button of group of 5. How can I get the data of input field. Names of all input field are created dynamically from database.
My code is as follows
$attributes = array('class' => 'result', 'id' => 'result', 'name' => 'result');
echo form_open('exam/result',$attributes);
$a = array(
'name' => 'aptitude-'.$value->question_id,
'id' => 'answer1',
'value' => 'a',
'checked' => FALSE,
);
$b = array(
'name' => 'aptitude-'.$value->question_id,
'id' => 'answer2',
'value' => 'a',
'checked' => FALSE,
);
$c = array(
'name' => 'aptitude-'.$value->question_id,
'id' => 'answer3',
'value' => 'a',
'checked' => FALSE,
);
$d = array(
'name' => 'aptitude-'.$value->question_id,
'id' => 'answer4',
'value' => 'a',
'checked' => FALSE,
);
$e = array(
'name' => 'aptitude-'.$value->question_id,
'id' => 'answer5',
'value' => 'a',
'checked' => FALSE,
);
$f = array(
'name' => 'aptitude-'.$value->question_id,
'id' => 'answer6',
'value' => 'a',
'checked' => TRUE,
'style' => 'display:none'
);
echo form_radio($a);
echo form_label($value->a, 'a');
echo "<br>";
echo form_radio($b);
echo form_label($value->b, 'b');
echo "<br>";
echo form_radio($c);
echo form_label($value->c, 'c');
echo "<br>";
echo form_radio($d);
echo form_label($value->d, 'd');
echo "<br>";
echo form_radio($e);
echo form_label($value->e, 'e');
echo "<br>";
echo form_radio($f);
echo form_close();
This code will run for 200 times
Use array name aptitude[]:
$a = array(
'name' => 'aptitude['.$value->question_id.']',
'id' => 'answer1',
'value' => 'a',
'checked' => FALSE,
);
$b = array(
'name' => 'aptitude['.$value->question_id.']',
'id' => 'answer2',
'value' => 'b',
'checked' => FALSE,
);
$c = array(
'name' => 'aptitude['.$value->question_id.']',
'id' => 'answer3',
'value' => 'c',
'checked' => FALSE,
);
$d = array(
'name' => 'aptitude['.$value->question_id.']',
'id' => 'answer4',
'value' => 'd',
'checked' => FALSE,
);
$e = array(
'name' => 'aptitude['.$value->question_id.']',
'id' => 'answer5',
'value' => 'e',
'checked' => FALSE,
);
$f = array(
'name' => 'aptitude['.$value->question_id.']',
'id' => 'answer6',
'value' => 'f',
'checked' => TRUE,
'style' => 'display:none'
);
Get POST value:
$aptitudes = $this->input->post('aptitude');
print_r($aptitudes);
// example output
[7] => a
[5] => c
[3] => f
// where [7] [5] [3] are the $value->question_id
Get specific POST
$aptitudes[5]; // output => c
I have a function to reset a password.
At the point where I have to compare the password and the password_confirm field, I get a strange behavior:
My form
echo $this->Form->hidden('tkn', array('value' => $tkn));
echo $this->Form->hidden('uid', array('value' => $uid));
echo $this->Form->input('password',array('type' => 'password', 'name' => 'data[Appuser][password]'));
echo $this->Form->input('password_confirm',array('type' => 'password', 'name' => 'data[Appuser][password_confirm]'));
My model validation:
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => array('equaltofield','password'),
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($val1, $val2){
return $this->data[$this->alias][key($val1)] == $this->data[$this->alias][$val2];
}
My Controller:
if($this->Appuser->save($this->data)){
$this->Session->setFlash(__('Password has been updated'));
}else{
debug($this->Appuser->invalidFields());
}
Now:
When I submit an empty form I get the following returned from the invalidFields()
array(
'password' => '*****',
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
Question 1: Why do I not get the message, that the password has not its minlength?
Question 2: Why do I get the second message twice for comparing the password?
When typing in 2 different password with min length, I get this again:
array(
'password_confirm' => array(
(int) 0 => '{password_confirm} not equal!',
(int) 1 => '{password_confirm} not equal!'
)
)
When debug($this->data) I also get this (if that helps somehow)
array(
'Api' => array(
'tkn' => '6837d241bf1076c3c55a95abbcfafa04dc19a33c',
'uid' => '1'
),
'Appuser' => array(
'password' => '*****',
'password_confirm' => 'asdfgh'
)
)
Any ideas regarding my two questions above?
Thanks in advance!!
Try this
var $validate = array(
'password' => array(
'rule' => array('minLength', '6'),
'message' => '{password} minLength 6!'
),
'password_confirm' => array(
'rule' => 'equaltofield',
'message' => '{password_confirm} not equal!'
),
);
function equaltofield($data){
return $this->data[$this->alias]['password'] == $this->data[$this->alias]['password_confirm'];
}
Unfortunately i don`t have the exact example here, but, is similar to this:
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender'
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
)
));
How can I set a default value to this element? I tried putting this, but didn't work:
'attributes' => array(
'value' => '0'
)
Thank's! And, sorry for my poor english! I need to improve it!
You should provide the value underoption, not attribute.
LIke this :
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender'
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
'value' => 10, // here
)
));
See this link:here
Radio is just an extension of MultiCheckbox
You will need to set the checked_value within the options array
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender', // <-- missing comma here also
'options' => array(
'label' => 'What is your gender ?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
'checked_value' => 10,
)
));
You can see this in the setOptions() method of Zend\Form\Element\Checkbox which the Radio element extends.
Your syntax is correct. value key in attributes is used to set the default checked value.
You just forgot to add a comma after 'name' => 'gender'
The following code will work for you
$form->add(array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'gender',
'attributes' => array(
'value' => '0',
),
'options' => array(
'label' => 'What is your gender?',
'value_options' => array(
'0' => 'Female',
'1' => 'Male',
),
),
));
I've got this table in my database that outputs this:
array(
(int) 0 => array(
'Price' => array(
'id' => '1',
'amount' => '20',
'price' => '180.00',
'type_id' => '1',
'active' => 'a'
)
),
(int) 1 => array(
'Price' => array(
'id' => '2',
'amount' => '30',
'price' => '232.50',
'type_id' => '1',
'active' => 'a'
)
),
...And so on.
I need a drop down in my form that displays the amount and price together (ie. "20 # 180.00"), but when selected, gets the "id" field.
I reworked a new array called $prices so it outputs like so...
array(
(int) 0 => array(
'id' => '1',
'amount' => '20',
'price' => '180.00',
'type_id' => '1',
'active' => 'a',
'display' => '20 # 180.00'
),
(int) 1 => array(
'id' => '2',
'amount' => '30',
'price' => '232.50',
'type_id' => '1',
'active' => 'a',
'display' => '30 # 232.50'
However, I'm not sure if that array is necessary.
But the main problem is that I don't know what to put in the Form options to make it select the "display" field.
echo $this->Form->input('Project.quantity', array(
'options' => $prices[?????]['display']
));
Simply adding the
'options' => $prices
displays a lot of stuff in the drop down (http://f.cl.ly/items/1e0X0m0D1f1c2o3K1n3h/Screen%20Shot%202013-05-08%20at%201.13.48%20PM.png).
Is there a better way of doing this?
You can use virtual fields.
In your model:
public $virtualFields = array(
'display' => 'CONCAT(amount, " # ", price)'
);
In the controller:
$prices = $this->Price->find('list', array(
'fields' => array('id', 'display')
));
Two ways to do this.
You said you reworked you array to $prices. Then, change that rework so the $prices array looks like this
array('1' => '4 # 6',
/*id*/ => /*price*/
/*etc*/);
and then pass it to the form
echo $this->Form->input('Project.quantity', array(
'options' => $prices
));
For a simple dropdown, retrieve the data with a find('list'). That will give you an array like the one you need to make a dropdown. To change the display field, create a virtual field like this in the model
public $virtualFields = array("display_price"=>"CONCAT(amount, ' # ' ,price)");
public $displayField = 'display_price';
And that way you don't have to rework your array.
If you have other drowpdowns of the same model in other forms, note that those will also change. That's the advantage of doing that in the model... Or disadvantage, if you only want to do it in one part... Like almost everything, it depends on what your need are :)
i have this form:
$this->addElement (
'multiCheckbox', 'servers2',
array (
'checkedValue' => '0',
'multiOptions' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
));
the problem is that the checkedValue doesn't work for this setup, it does for a simple checkbox. I've also tried 'checkedValues' => array('1','0'), singular or plural,
but no end in sight.
any ideas?
THanks
To mark certain checkboxes as checked, try this:
$multiCheckElement->setValue(array('11.com', '3.com'));
// or
$this->addElement (
'multiCheckbox', 'servers2',
array (
'value' => array('11.com', '3.com'), // select these 2 values
'multiOptions' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
)
);
See also Zend_Form_Element_MultiCheckbox
ZF2 will require you to use value_options;
$form->add(
array(
'name' => 'servers2',
'type' => \Zend\Form\Element\MultiCheckbox::class,
'attributes' => array(
'id' => 'servers2',
'class' => 'form-control',
),
'options' => array(
'label' => 'Servers 2',
'column-size' => 'sm-10',
'label_attributes' => array('class' => 'col-sm-2'),
'twb-layout' => 'horizontal',
'value_options' => array(
'11.com' => '.com',
'12.com' => '12.com',
'16.com' => '16.com',
'3.com' => '17.com'
)
),
)
);
To specify the checked options, as seen at
use the 'selected' => true attribute:
$options = array(
array(
'value' => '0',
'label' => 'Apple',
'selected' => false,
'disabled' => false,
'attributes' => array(
'id' => 'apple_option',
'data-fruit' => 'apple',
),
'label_attributes' => array(
'id' => 'apple_label',
),
),
array(
'value' => '1',
'label' => 'Orange',
'selected' => true,
),
array(
'value' => '2',
'label' => 'Lemon',
),
);