Zend2 element/select issue with array - php

Maybe someone knows how to fix this issue.
For example:
//id in database is 1 (so it should select first element)
$dataArray = [
1 => 'Test 1',
2 => 'Test 2',
3 => '1',
]
$type = new Element\Select('type');
$type->setValueOptions($dataArray);
It will create normal select box, but when data will be auto selected, options will marked as selected 1 and 3 (no matter that's not multi select).
This problem appears when array is loaded from database and value in array is equal for label.
Any help?

Problem solved.
When printing element, value was set like object.

Related

PHP Form Data with Multi-dimensional Array

Say I have a form containing an input field with
name="my_options[my_elements][1][name]"
and another with
name="my_options[my_elements][1][category]"
When I submit the form all is well and a new array containing name and category keys/values is added to the my_elements array in the my_options array in the DB.
I can also use name="my_options[my_elements][][name]"
to create a new array within my_options[my_elements]
However I obviously can't then use name="my_options[my_elements][][category]"
as this will put the category value into a further new array.
Is there a way of dynamically keeping track of the position of the new array created with the first [] so that values can be added at that particular level of the multidimensional array?
To clarify, I'm looking to dynamically add elements to the following array:
'my_options' =>
array (size ?)
0 =>
'my_elements' =>
array (size=2)
0 =>
array (size=2)
'name' => 'name 1'
'category' => 'cat 1'
1 =>
array (size=2)
'name' => 'name 2'
'category' => 'cat 2'
`
I wouldn't use array definitions in the html part of your application.
Stick with a input name, like name="category" and in your php file fetch the submitted data and organize it in an array the way you want it.
assuming you use POST as form method:
$category = $_POST['category'];
(don't forget to validate the input)

Updating and creating entries with PHP and DynamoDB

I have a string set on one of my DynamoDB tables.
This code adds a new item to the string set and works great
$update = $client->updateItem ( array (
'TableName' => 'myTable',
'Key' => array (
'rep_num' => array (
'S' => $id_num
)
),
'ExpressionAttributeValues' => array (
':transaction_ids' => array(
'SS' => array($transaction_id)
)
),
'UpdateExpression' => 'ADD transaction_ids :transaction_ids'
));
My problem is, it only works if the key transaction_ids already exists on an item.
How can I change this code to also create the key on an existing item if one doesn't exist?
If the item doesn't have the transaction_ids, it should create the attribute and add the value to it.
If the item has the attribute transaction_ids, it should add the value to it as long as the data type of new value matches with the existing SS attribute data type.
Refer this link
If the existing data type is a set, and if Value is also a set, then
Value is appended to the existing set. For example, if the attribute
value is the set [1,2], and the ADD action specified [3], then the
final attribute value is [1,2,3]. An error occurs if an ADD action is
specified for a set attribute and the attribute type specified does
not match the existing set type.
Both sets must have the same primitive data type. For example, if the
existing data type is a set of strings, Value must also be a set of
strings.
I have tested with JavaScript API. It works as expected. It should work in PHP as well.
Sample code:-
UpdateExpression : "ADD #transaction_ids :transaction_ids ",
ExpressionAttributeNames: {
'#transaction_ids' : 'transaction_ids',
},
ExpressionAttributeValues: {':transaction_ids' : docClient.createSet(["3"])},

How to update specific field in CodeIgniter update?

I'm using CodeIgniter framework for the easyappointments.org library. I'm trying to update the field data through the id of a row. Actually this is my code for the update:
return $this->db
->update('ea_appointments', $appointment)
->where('ea_appointments.id', $appointment_id);
The problem's that the update method need two parameter (table to update, associative array with the fields name column of db). In my above function I pass only the $appointment_id, so the variable $appointment is empty and doesn't contain any associative array. I want to know how to update only the field data to 1. And remain the other field in the same value condition.
This is the structure of the table:
id|book|start|end|notes|hash|unavailable|provider|data
Logic example:
previous condition row:
id => 0
book => example
start => 18/10/2015
end => 19/10/2015
notes => empty
hash => akdjasldja
unavailable => false
provider => 5
data => 0
I pass in the function $appointment_id with 0 value. I'm waiting this new result:
id => 0
book => example
start => 18/10/2015
end => 19/10/2015
notes => empty
hash => akdjasldja
unavailable => false
provider => 5
data => 1
So the main problem is retrieve first the all field value of the specific row and later update? Or something like this. Could someone help me?
In my above function I pass only the $appointment_id, so the variable
$appointment is empty and doesn't contain any associative array.
If you simply want to update the column data to 1 for appointment_id 0 then pass in an array with data for the key and 1 for the value.
$appointment_id = 0;
$appointment = array('data' => 1);
$this->db->where('id', $appointment_id);
$this->db->update('ea_appointments', $appointment);

Doctrine: Update using collection

The Problem/How
Pass angularJS the array result of a query that includes many joins.
Using angularJS to sort with ui-sortable reorders the dataset when sorting.
Pass data back to PHP and use synchronizeWithArray to save back (creates a collection).
Doctrine doesn't like receiving the data of the collection back in a different order than it outputs.
** If all I change are values - without reordering elements it saves with no problems.
Update: http://www.doctrine-project.org/jira/browse/DC-346
Noticed it was an old bug they never fixed, is there anything to still do what I want?
Details
$model = Doctrine_Core::getTable('TableName')->findOneById(...);
$model->synchronizeWithArray(array);
$model->save();
Doctrine (1.2) / mysql throws an Integrity error, duplicate primary key id = 2 - it is trying to change the id field.
When I reorder the elements with ui-sortable, it moves the arrays within 'Fields' around while also updating the 'position' value.
This is example data:
The problem would be array 0 and array 1 swap places - causing doctrine to cause primary key error as it tries to change the ids over.
array( // the root of the array is part of one table
id => 1001,
label => 'xxx',
Fields => array( // related table data
0 => array(
id => 1,
position => 0,
name => 'item1'
),
1 => array(
id => 2,
position => 1,
name => 'item2'
),
2 => array(
id => 3,
position => 2,
name => 'item3'
),
3 => array(
id => 4,
position => 3,
name => 'item4'
)
)
)
Well I guess we could either look on the Doctrine side and try to fix this bug, or make it work with the way Doctrine operates. The second option is probably easier.
Why don't you just save the order of the data, and put it back in that order before feeding it back to doctrine? One way to do that would be in angular. Where ever you get the array of data in angular, call saveOrder(), and before feeding it back, call reOrder():
var order = {};
function saveOrder(data)
{
for(var key in data)
{
if(data.hasOwnProperty(key))
{
order[data[key].id] = key;
}
}
}
function reOrder(data)
{
var ordered = [];
for(var key in data)
{
if(data.hasOwnProperty(key))
{
ordered[order[data[key].id]] = data[key];
}
}
return ordered;
}

Cakephp: Saving the array value not the integer

After filtering some data, i created a variable $customers. This variable is a simple array
which has the following values:
array(
(int) 1 => 'Customer1',
(int) 2 => 'Customer2',
(int) 3 => 'Customer3'
)
I passed this array from the controller to the view like this
$this->set('customers', $customers);
In the view i use this array in a form so that the user can select one
echo $this->Form->input('customer_id', array('options' => array($customers)));
The data which is displayed in the select form is this 'Customer1', 'Customer2', 'Customer3'
Eveyting works ok so far.
Now, after the user has submited the data, i want to do some further logic in the controller. I want to take the data which the user has selected and save it in a second table. So i do this:
$this->Invoice->set('secondtabel', $this->request->data['Invoice']['customer_id']);
The data is being saved in the second table, but the problem is saves the value '1', '2', '3' not the name of the customer. How can i save the name of the customer and not the identifier number from the array.
Please bear with me, i am new to cakephp and to php in general.
I assume this is actually a problem with your HTML, your select box likely looks something like this:
<select name="customer_id">
<option value="1">Customer1</option>
<option value="2">Customer2</option>
<option value="3">Customer3</option>
</select>
This is why your values are 1, 2 or 3 and not Customer1 etc, because $this->request->data['Invoice']['customer_id'] is equal to 1, 2 or 3 etc.
My suggestion would be to fix this at the root of the problem, and I think that by only passing the values into your select box, you should get HTML like this:
<option>Customer1</option>
... which will mean that $this->request->data['Invoice']['customer_id'] will equal Customer1 etc.
So, try this: (array_values will return an array only containing the values, essentially stripping the keys)
$this->set('customers', array_values($customers));
This should fix your problem. However, as far as structured data goes, the way you are currently doing it (storing 1, 2 or 3 etc) is actually the correct way to do this. This way, you simply join the customers table when you are retrieving this data, and you can grab the name that way... Something like this:
$invoices = $this->Invoice->find('all', array(
'conditions' => array(
// your custom find conditions
),
'joins' => array(
array(
'table' => 'customers',
'alias' => 'Customer',
'type' => 'LEFT',
'conditions' => array('Customer.id = Invoice.customer_id')
)
),
'fields' => array(
'Invoice.*', // retrieve regular invoice data
'Customer.name' // retrieve the joined customer name too
)
));
This way you still store a customer ID as an integer, and simply look up the name with SQL when you go to retrieve that data.
A possible reason why you might want to do it simply by storing the customer's name as text is that you want to store the customers name as it appears at the time, meaning if it changes in the future, previous invoices with that name attached won't change with it because that name is stored in text rather than a numeric reference to another table containing a name that has changed.
Hope this helps.
Docs
http://php.net/array_values

Categories