I am using zend Framework Form,
I am newbie in zend Framework and i want to display my check box form like this :-
*SK336
*CP
*PES
*JCP
*BGH
*SK996
*KO
*RTY
*HGR
*SK547
*GPK
*SK478
*JUP
Note where :- * is check-box here
what i am trying is here :-
public function init()
{
$parents = array();
$childs = array();
foreach ($this->Tagkey as $aResultDataValue) {
$parents [$aResultDataValue['parent']] = $aResultDataValue['parent'];
$childs [$aResultDataValue['parent']][] = $aResultDataValue['child'];
}
foreach ($parents as $parent){ // print_r ($parents); die();
$tags = new Zend_Form_SubForm();
$tags->addElements(array(
new Zend_Form_Element_MultiCheckbox('parent', array(
'multiOptions' => array($parent),
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
array($parent))
)
)),
));
foreach ($childs as $child){
$tags->addElements(array(
new Zend_Form_Element_MultiCheckbox('child', array(
'multiOptions' => array($child),
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
$child)
)
)),
));
}
$this->addSubForms(array(
'tags' => $tags,
)
);
}
I am able to create such type of structure in any .php page but not able to do that right now in zend framework form, I am using zend sub-form here.
Also I got an error right now when i am using this query
Warning: htmlspecialchars() expects parameter 1 to be string, array given in /var/www/dashboard_campaign/library/Zend/View/Abstract.php on line 905
More Information about my Question :-
(1) mysql qyery
select b.tagCode parent,a.tagCode child from tag a, tag b where a.tagParentId=b.tagId
(2) output of Zend_Debug::dump($this->Tagkey);
array(9) {
[0] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(2) "CP"
}
[1] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(3) "PES"
}
[2] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(3) "JCP"
}
[3] => array(2) {
["parent"] => string(5) "SK996"
["child"] => string(2) "KO"
}
[4] => array(2) {
["parent"] => string(5) "SK996"
["child"] => string(3) "RTY"
}
[5] => array(2) {
["parent"] => string(5) "SK996"
["child"] => string(3) "HGR"
}
[6] => array(2) {
["parent"] => string(5) "SK547"
["child"] => string(3) "GPK"
}
[7] => array(2) {
["parent"] => string(5) "SK478"
["child"] => string(3) "JUP"
}
[8] => array(2) {
["parent"] => string(5) "SK336"
["child"] => string(3) "BGH"
}
}
Now i can understand your problem. I think it is to hard for handle that think from sub form. Try to use zend view scripts as following way.
Your form.php
public function init()
{
foreach ($parents as $parent) {
$parent = new Zend_Form_Element_Hidden($parent);
$parent->setDecorators(array(
array(
'ViewScript',
array(
'viewScript' => 'customviewscripts/parent.phtml',
'parent' => $parent
)
)
);
$this->addElement($parent);
}
}
file at views/script/customviewscript/parent.phtml
<?php
$params = $this->element->getDecorator('ViewScript')->getOptions();
$parent = $parems['parent'];
$string = '<label>$parent['name']</label><input type="check" name="parent[]">';
foreach ($children as $child) {
$string .= <label>$child['name']</label>
. <input type="check" name=child[$parent[id]][]>' ;
}
print $string;
?>
This is not the real solution. I percent only example. I think you can customize that. Most of developer use view script to make complex forms.
Your looking like mentioned wrong syntax for multi options John, you should try this.
Remove array for $parent see example below.
new Zend_Form_Element_MultiCheckbox('parent', array(
'multiOptions' => $parent,
'filters' => array('StringTrim'),
'validators' => array(
array('InArray',
false,
array($parent))
)
From db results you have to produce following type of array for the $parent variable
$parent variable should like this for example copy this array and try your self without fetching from database,you will see
all options
$parent=Array ([1] => blah1 [2] => blah2 [3] => blah3 [4] => blah4 [5] => blah5);
Check this one also for multiple check boxes, in place of array you should try placing array variable, I havn't tried this just looked at internet but should work fine.
$category1 = new Zend_Form_Element_MultiCheckbox('categories',Array())
$category1->setLabel('Category 1');
$category2 = new Zend_Form_Element_MultiCheckbox('categories',Array())
$category2->setLabel('Category 2');
... later...
$this->addElement($category1)
->addElement($category2);
Related
I'm trying to echo the "name" variable from this array.
array(2) {
["error"] => bool(false)["response"] => array(8) {
["id"] => int(560277)["name"] => string(7)
"Jeff" ["avatar"] => string(55)
"https://etc.etc" ["joinDate"] => string(19)
"2015-09-21 16:47:53" ["steamID64"] => int(76521228272726998)
["groupName"] => string(6)
"Player" ["groupID"] => int(1)["permissions"] => array(2) {
["isGameAdmin"] => bool(false)["showDetailedOnWebMaps"] => bool(false)
}
}
}
I'm not sure how to access it, I've tried:
json->response->name;, but this won't work because this is an array not an object and json[0]["response"]["name"];
EDIT:
The array is coming from a json API, here is an example. https://api.truckersmp.com/v2/player/2
I am then converting that using
$json = (json_decode($tmpServer, true, JSON_BIGINT_AS_STRING));
i got it working on php fiddle like this
$arr = file_get_contents("https://api.truckersmp.com/v2/player/2");
$decoded = json_decode($arr,true);
echo $decoded["response"]["name"];
I am trying to add values in a multidimensional array. Given below is how its supposed to be
array(
'name' => 'Hotel',
'placeholder' => 'Select the hotel',
'id' => $prefix . 'confirmation_hotel',
'type' => 'select_advanced',
'multiple' => false,
'options' => array(
'5896' => 'Hotel 1',
'6005' => 'Hotel 2'
)
),
But I getting data of options from a custom function with a foreach loop, given below is the code.
global $bookyourtravel_accommodation_helper, $bookyourtravel_car_rental_helper;
$items = $order->get_items();
$names = array();
foreach ( $items as $item_id => $item ) {
$bookyourtravel_theme_woocommerce = BookYourTravel_Theme_WooCommerce::get_instance();
$bookyourtravel_theme_woocommerce->init();
$order_names = $bookyourtravel_theme_woocommerce->order_item_name_confirmation($item);
}
$order_names output:
array(2) {
["name"]=>
string(17) "Hotel 1"
["id"]=>
string(4) "5896"
}
array(2) {
["name"]=>
string(26) "Hotel 2"
["id"]=>
string(4) "6005"
}
Now I need to add this data in the array given above. I'm not sure how to achieve this, can someone help me.
In the loop, after $order_names assignment, add :
$originalArray['options'][$order_names['id']] = $order_names['name'];
I assume your first array at the top is called $a.
So you can append an array element to your 'options' sub array like this:
foreach ($order_names as $order_name) {
array_push($a['options'], array($order_name['id'] => $order_name['name']));
}
I have been working with the square-connect api, and I am having issues trying to create an order. I have followed the docs for creating an order. Below is the code in question.
$api = new \SquareConnect\Api\OrdersApi();
$location = $this->get_location();
if (!empty($items)) {
if (is_string($items)) {
$items = json_decode($items);
}
// print_r($items);
if (!is_array($items)) {
// echo "Items is not an array -- exiting now";
return false;
}
$order_data = [];
$order_data['idempotency_key'] = uniqid();
$order_data['line_items'] = [];
foreach ($items as $key => $li) {
$order_data['line_items'][] = [
'name' => $li->name,
'base_price_money' => ['amount' => ($li->price * 100)],
'quantity' => $li->qty
];
}
$order_data['taxes'] = [
'type' => 'ADDITIVE',
'name' => 'State Sales Tax',
'percentage' => '7'
];
print_r($order_data);
// exit;
$apiResponse = $api->createOrder($location, new \SquareConnect\Model\CreateOrderRequest($order_data));
$order = $apiResponse->getOrder();
print_r($apiResponse);
print_r($order);
}
When I make the request I get the following error
Message: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_ARRAY","detail":"Expected an array.","field":"taxes"}]}
I have checked that the taxes field is an array
["taxes"]=>
array(3) {
["type"]=>
string(8) "ADDITIVE"
["name"]=>
string(15) "State Sales Tax"
["percentage"]=>
string(1) "7"
}
Any assistance would be greatly appreciated
EDIT: print_r($items) output
Array
(
[0] => stdClass Object
(
[id] => 24
[square_id] => L7PCKMIEDQFDR34IZ3E3VIDO
[variation_id] => SUJCAPZJQLM7VGAILP66NMMM
[variation_name] => Regular
[qty] => 1
[price] => 3.99
[name] => Ham & Cheese
)
[1] => stdClass Object
(
[id] => 4
[square_id] => 37RNDFXRVJPLR4UT7UXS5QOO
[variation_id] => 27Z5H3V6ZRHW2X5LVNGXOARY
[variation_name] => Starbucks DOUBLESHOT Expresso
[qty] => 1
[price] => 2.5
[name] => Starbucks DOUBLESHOT Expresso
)
)
var_dump(order_data)...
array(3) {
["idempotency_key"]=>
string(13) "59c138b7a67e2"
["line_items"]=>
array(2) {
[0]=>
array(3) {
["name"]=>
string(12) "Ham & Cheese"
["base_price_money"]=>
array(1) {
["amount"]=>
float(399)
}
["quantity"]=>
string(1) "1"
}
[1]=>
array(3) {
["name"]=>
string(29) "Starbucks DOUBLESHOT Expresso"
["base_price_money"]=>
array(1) {
["amount"]=>
float(250)
}
["quantity"]=>
string(1) "1"
}
}
["taxes"]=>
array(3) {
["type"]=>
string(8) "ADDITIVE"
["name"]=>
string(15) "State Sales Tax"
["percentage"]=>
string(1) "7"
}
}
The issue here is that you are expected to give an array of tax objects, and it is confusing because you are using the implicit object creation capability of the SDK with PHP arrays. in JSON, your taxes would look like:
"taxes":[
{
"type":"additive"
...
}
]
(note that is is an array of objects), you are providing:
"taxes":{
"type":"additive"
...
}
So if you want to just make a quick change to your code, you should do something like:
$order_data['taxes'] =array(array(
'type' => 'ADDITIVE',
'name' => 'State Sales Tax',
'percentage' => '7'
));
If you wanted to be more verbose in your code you could do something like:
$order_data = new \SquareConnect\Model\CreateOrderRequest();
$taxes = \SquareConnect\Model\CreateOrderRequestTax();
$taxes->setType('ADDITIVE');
...
$order_data->setTaxes($taxes);
Clearly the implicit array way is a bit easier, but it can be confusing in PHP. Does that help?
Hi I am creating an array which will store sections in my site. Some of these sections will not be available for some users to view therefore I will need to check the permission of the user in question before putting into my array. However when I do this if statement I get an array within an array which I don't want and this causes the following error:
Method Illuminate\View\View::__toString() must not throw an exception
This is the code I am using:
$user = Auth::user();
if(($user->hasRole('Admin') || $user->hasRole('Admin') || $user->hasRole('Project Master') || $user->hasRole('Project Owner'))) {
$restrictsections = ['Create' => route('project.create'),
'Sort' => route('project.sort'),];
}
$this->sections = [
'Projects' => [
'View' => route('project.index'),
$restrictsections
]
];
The array is now structured as so:
array(1) {
["Projects"]=>
array(2) {
["Create"]=>
string(30) "http://projects.local/projects"
[0]=>
array(2) {
["Create"]=>
string(37) "http://projects.local/projects/create"
["Edit"]=>
string(35) "http://projects.local/projects/sort"
}
}
}
As opposed to:
$this->sections = [
'Project' => [
'View' => route('project.index'),
'Create' => route('project.create'),
'Sort' => route('project.sort'),
]
];
array(1) {
["Project"]=>
array(3) {
["View"]=>
string(30) "http://projects.local/project"
["Create"]=>
string(37) "http://projects.local/project/create"
["Sort"]=>
string(35) "http://projects.local/project/sort"
}
}
Any ideas how I can merge the two arrays together? but it should be structured as follows:
array(1) {
["Project"]=>
array(3) {
["View"]=>
string(30) "http://projects.local/project"
["Create"]=>
string(37) "http://projects.local/project/create"
["Sort"]=>
string(35) "http://projects.local/project/sort"
}
}
You can use the + operator to combine arrays.
For example:
php > print_r(['View' => '1'] + ['Create' => 'two', 'Sort' => '3']);
Array
(
[View] => 1
[Create] => two
[Sort] => 3
)
Applying to your code:
$user = Auth::user();
if(($user->hasRole('Admin') || $user->hasRole('Admin') || $user->hasRole('Project Master') || $user->hasRole('Project Owner'))) {
$restrictsections = ['Create' => route('project.create'),
'Sort' => route('project.sort'),];
}
$this->sections = [
'Projects' => [
'View' => route('project.index')
] + $restrictsections
];
edit: + is technically a union so if the second array has keys that are present in the first array they will be ignored.
Create it a little another
$this->sections = ['Projects' => $restrictsections];
$this->sections['Projects']['View'] = route('project.index');
Use array_merge() like this
$this->sections = [
'Projects' => array_merge(
['View' => route('project.index')],
$restrictsections
)
];
or use the + operator like this
$this->sections = [
'Projects' => ['View' => route('project.index')] + $restrictsections
];
I am trying to figure out how I can move an array element to another spot. Is this possible?
Here is my example the var_dump array:
array
'person' =>
array
'first_name' =>
array
'...'
'last_name' =>
array
'...'
'rank' =>
array
'...'
'score' =>
array
'...'
'item' =>
array
'...'
'work' =>
array
'company' =>
array
'...'
'phone' =>
array
'...'
And of course there are values in the '...', but just to simplify it. So I need to move "score" before "rank", so the output will show score first before rank, is that possible?
Now I know the array push/pop/shift/unshift but none of those would help me here I think.
Please note, I have no control of this array...I am receiving it as is...
basically it is coming from a Wordpress plugin and it has a filter for these fields so I am using this to catch it.
add_filters( 'work_rank_fields', 'custom_order');
function custom_order($fields) {
var_dump($fields); //what you see on top
}
Using a sample array like you gave us, you could try something like this.
$sample = array(
'person' => array(
'first_name' => array('first'),
'last_name' => array('last'),
'rank' => array('rank'),
'score' => array('score'),
'item' => array('item')
),
'work' => array(
'company' => array('company'),
'phone' => array('phone')
)
);
function reorder_person( $sample )
{
extract( $sample['person'] );
// the desired order below for the keys
$sample['person'] = compact('first_name','last_name','score','rank','item');
return $sample;
}
$sample = reorder_person( $sample );
Now your var_dump of $sample should display score before rank
array(2) {
'person' =>
array(5) {
'first_name' =>
array(1) {
[0] =>
string(5) "first"
}
'last_name' =>
array(1) {
[0] =>
string(4) "last"
}
'score' =>
array(1) {
[0] =>
string(5) "score"
}
'rank' =>
array(1) {
[0] =>
string(4) "rank"
}
'item' =>
array(1) {
[0] =>
string(4) "item"
}
}
'work' =>
array(2) {
'company' =>
array(1) {
[0] =>
string(7) "company"
}
'phone' =>
array(1) {
[0] =>
string(5) "phone"
}
}
}
A little clumsy but, your wordpress filter custom_order function then might look like:
function custom_order( $fields ) {
$a = array();
foreach( $fields['person'] as $key => $value )
{
if ( $key == 'rank' ) continue; // wait until we get score first
if ( $key == 'score' )
{
$a['score'] = $value; // add score first, then rank
$a['rank'] = $fields['person']['rank'];
continue;
}
$a[$key] = $value;
}
$fields['person'] = $a;
return $fields;
}
I'm not sure which is the order criteria but I guess one of this functions can help you. Take a look particularly to last three. You just have to create the appropriate comparison function