How to add array values in CHtml::activeDropDownList in Yii - php

I want to add $cluster1 values to CHtml::activeDropDownList
$cluster_name_models = new ClusterForm();
$records = $cluster_name_models->get_list('grade');
$cluster_details=$records['0']['grade'];
$cluster1=explode(';',$cluster_details);
$cluster1=Array ( [0] => JMC [1] => MMC [2] => SMC );

Assuming that grade is the name of the related model's attribute
$cluster_name_models = new ClusterForm();
$records = $cluster_name_models->get_list('grade');
$cluster_details=$records['0']['grade'];
$cluster1=explode(';',$cluster_details);
echo CHtml::activeDropDownList($model,
'grade',
$cluster1,
array('empty'=>'Select Option'));

Related

pass correct multidimensional array values to function (without duplicates)

I am trying to get away from doing things manually and repetitively by correctly utilizing loops and functions (methods) in oop programming; but I have hit a major stumbling block as it regards to multidimensional array groups, in passing the correct values to the necessary abstracted function (method) responsible for a database action.
Any help at all is very much welcomed and will enable me to move on from this stumbling block that I have been trying to push away for days upon days but without progress and it is out of true frustration and much agony that I am here begging for help.
Below is the code that for simplicity I have shortened as much as possible (can be easily tested locally by copying and pasting):
// array with table properties and form values - start
$form_fields_arr = [
'group' => [
'anime' => [ // genre
'table_prop' => [ // for update query - table properties
'table_name' => 'anime_tbl',
'account_id' => 2,
'visible' => 'yes'
],
'form_data' => [ // for update query - form values
'2' => 'Attack on Titan',
'4' => 'RWBY',
'6' => 'Rurouni Kenshin',
'8' => 'A Silent Voice'
]
],
'movie' => [ // genre
'table_prop' => [ // for update query - table properties
'table_name' => 'movie_tbl',
'account_id' => 4,
'visible' => 'yes'
],
'form_data' => [ // for update query - form values
'1' => 'Queen of Katwe',
'3' => 'Forest Gump',
'5' => 'War Horse',
'7' => 'The Fault in our Stars'
]
]
]
]; // ... end
// loop through multidimensional array and pass values to function - start
foreach ($form_fields_arr['group'] as $frm_key_1 => $frm_val_1) { // 2d array
foreach ($frm_val_1 as $frm_key_2 => $frm_val_2) { // 1d array
if (strcasecmp($frm_key_1, $frm_key_1) === 0) { // group by genre
foreach ($frm_val_2 as $frm_key_3 => $frm_val_3) { // 1d array
if (strcasecmp($frm_key_2, 'form_data') === 0) {
$title = $form_fields_arr['group'][$frm_key_1]['form_data'][$frm_key_3]; // anime/movie title
}
if (isset($frm_val_2['table_name']) &&
isset($frm_val_2['account_id']) &&
isset($frm_val_2['visible']) &&
isset($title)
) {
dbUpdate(
$frm_val_2['table_name'],
$frm_val_2['account_id'],
$frm_val_2['visible'],
$title
);
}
} // 1d array
} // if block
} // 1d array
} // 2d array
// ... end
// function that receives passed values - start
function dbUpdate($table_name, $account_id, $title_col, $form_value) {
$test_val_arr = [$table_name, $account_id, $title_col, $form_value];
return print_r($test_val_arr);
} // ... end
The above code outputs:
// array values passed to and returned from function
Array (
[0] = movie_tbl
[1] = 4
[2] = yes
[3] = A Silent Voice
)
Array (
[0] = movie_tbl
[1] = 4
[2] = yes
[3] = A Silent Voice
)
Array (
[0] = movie_tbl
[1] = 4
[2] = yes
[3] = A Silent Voice
)
Array (
[0] = movie_tbl
[1] = 4
[2] = yes
[3] = A Silent Voice
)
But the desired result that I am trying to achieve is:
// for anime genre - array values passed to and returned from function
Array (
[0] = anime_tbl
[1] = 2
[2] = yes
[3] = Attack on Titan
)
Array (
[0] = anime_tbl
[1] = 2
[2] = yes
[3] = RWBY
)
Array (
[0] = anime_tbl
[1] = 2
[2] = yes
[3] = Rurouni Kenshin
)
Array (
[0] = anime_tbl
[1] = 2
[2] = yes
[3] = A Silent Voice
)
// for movie genre - array values passed to and returned from function
Array (
[0] = movies_tbl
[1] = 4
[2] = yes
[3] = Queen of Katwe
)
Array (
[0] = movies_tbl
[1] = 4
[2] = yes
[3] = Forest Gump
)
Array (
[0] = movies_tbl
[1] = 4
[2] = yes
[3] = War Horse
)
Array (
[0] = movies_tbl
[1] = 4
[2] = yes
[3] = The Fault in our Stars
)
so upon everything royally failing with me spending literally about a week trying to fix this, telling myself that it is very simple and I really shouldn't be stuck here, out of desperation I decided to go back to my repetitive ways and tried the following:
// new array without table properties - start
$new_array = [];
$new_array['group']['anime'] = $form_fields_arr['group']['anime']['form_data'];
$new_array['group']['movie'] = $form_fields_arr['group']['movie']['form_data']; // ... end
// loop through multidimensional array and pass values to function - start
foreach ($new_array['group'] as $key_1 => $val_1) { // 2d array
foreach ($val_1 as $key_2 => $val_2) { // 1d array
if (strcasecmp($key_1, $key_1) === 0) {
dbUpdate('anime_tbl', 2, 'yes', $val_2);
dbUpdate('movie_tbl', 4, 'yes', $val_2);
} // if block
} // 1d array
} // 2d array
// ... end
But the results are still very much undesirable. Everything was working fine until I started using multidimensional arrays, simply because I realized that utilizing multidimensional arrays help me to shorten my code in other areas considerably. But I am stuck here and will have to go back further up and undo quite a lot of changes if I can't get this to work. I am pleading for help from any good soul out there. Please help me someone! Anyone!
I am being optimistic here and assuming that if by any chance I do get some help in fixing the above problem, could someone please also teach me how to loop through an array structure like the one below while yet getting the desired results without duplicates (I have truly tried but have truly failed):
// array with table properties and form values - start
$form_fields_arr = [
'table_prop' => [ // table properties group
'anime' => [ // for update query - table properties
'table_name' => 'anime_tbl',
'account_id' => 2,
'visible' => 'yes'
],
'movie' => [ // for update query - table properties
'table_name' => 'movie_tbl',
'account_id' => 4,
'visible' => 'yes'
]
],
'form_data' => [ // for update query - form values
'anime' => [ // genre
'2' => 'Attack on Titan',
'4' => 'RWBY',
'6' => 'Rurouni Kenshin',
'8' => 'A Silent Voice'
],
'movie' => [ // genre
'1' => 'Queen of Katwe',
'3' => 'Forest Gump',
'5' => 'War Horse',
'7' => 'The Fault in our Stars'
]
]
]; // ... end
You got a logic mistake in your for loops. First of all your variable namings are not very intuitive. $frm_key_1, $frm_key_2, etc. look alike and force the reader to have the array structure in mind all the time to understand the variables meaning. This led to a mistake like this one: if( strcasecmp($frm_key_1, $frm_key_1) === 0 ). This is always true.
Then you had two exclusive conditions:
if (strcasecmp($frm_key_2, 'form_data') === 0)
And:
if (isset($frm_val_2['table_name']) && /* ... */) {
If $frm_key_2 is 'form_data' you are in the second child of the genre array, yet the fields 'table_name', etc. are defined only in the first one (witht the key 'table_prop'). So both conditions can never be true at the same time.
Your condition to trigger the dbUpdate() function was, that all fields of the 'table_prop' array were present (which you iterated through at the same time), and a $title was set aswell. This was only true after your third for-loop iterated for the second time. During that iterations the $title variable got overwritten constantly, but no sbUpdate() was triggered, because $frm_val_2 had the values from 'form_data' instead of 'table_prop'. So after the 3rd for loop finished the 2nd time $title was 'A Silent Voice', which is simply the last child of the first 'form_data' array. Afterwards your 2nd for loop iterated the 2nd 'table_prop' array again, which means that now the 'dbUpdate()' condition was true, so it postet 4 times (number of childs in the 'table_prop' array) the parameters with $title = 'A Silent Voice'.
You tried to make everything as generic as possible, making everything over complicated. The best solution that works here is one that respects the specific structure.
This works:
<?php
// array with table properties and form values - start
$form_fields_arr = [
'group' => [
'anime' => [ // genre
'table_prop' => [ // for update query - table properties
'table_name' => 'anime_tbl',
'account_id' => 2,
'visible' => 'yes'
],
'form_data' => [ // for update query - form values
'2' => 'Attack on Titan',
'4' => 'RWBY',
'6' => 'Rurouni Kenshin',
'8' => 'A Silent Voice'
]
],
'movie' => [ // genre
'table_prop' => [ // for update query - table properties
'table_name' => 'movie_tbl',
'account_id' => 4,
'visible' => 'yes'
],
'form_data' => [ // for update query - form values
'1' => 'Queen of Katwe',
'3' => 'Forest Gump',
'5' => 'War Horse',
'7' => 'The Fault in our Stars'
]
]
]
];
// loop through multidimensional array and pass values to function - start
foreach ($form_fields_arr['group'] as $genreData) {
$tableProperties = $genreData['table_prop'];
if (!isset($tableProperties['table_name'])
|| !isset($tableProperties['account_id'])
|| !isset($tableProperties['visible'])) {
continue;
}
$data = $genreData['form_data'];
foreach ($data as $title) {
dbUpdate(
$tableProperties['table_name'],
$tableProperties['account_id'],
$tableProperties['visible'],
$title
);
}
}
// function that receives passed values - start
function dbUpdate($table_name, $account_id, $title_col, $form_value) {
$test_val_arr = [$table_name, $account_id, $title_col, $form_value];
return print_r($test_val_arr);
} // ... end
For the last part of the question that wasn't answered, thanks to Philipp Maurer's answer, after playing around with the code I got it to work. I am just placing the answer here for anyone who might have a similar problem and would like to better understand how to group and fetch values from a multidimensional array using a foreach loop without duplicates or incorrect results. See below code:
// array with table properties and form values - start
$form_fields_arr = [
'table_prop' => [ // table properties group
'anime' => [ // for update query - table properties
'table_name' => 'anime_tbl',
'account_id' => 2,
'visible' => 'yes'
],
'movie' => [ // for update query - table properties
'table_name' => 'movie_tbl',
'account_id' => 4,
'visible' => 'yes'
]
],
'form_data' => [ // for update query - form values
'anime' => [ // genre
'2' => 'Attack on Titan',
'4' => 'RWBY',
'6' => 'Rurouni Kenshin',
'8' => 'A Silent Voice'
],
'movie' => [ // genre
'1' => 'Queen of Katwe',
'3' => 'Forest Gump',
'5' => 'War Horse',
'7' => 'The Fault in our Stars'
]
]
]; // ... end
// loop through multidimensional array and pass values to function - start
foreach ($form_fields_arr as $index => $group_array) {
foreach ($group_array as $genre_key => $genre_val) {
if (!isset($group_array[$genre_key]['table_name']) ||
!isset($group_array[$genre_key]['account_id']) ||
!isset($group_array[$genre_key]['visible'])
) {
continue;
}
foreach ($form_fields_arr['form_data'][$genre_key] as $data_key => $data_title) {
dbUpdate(
$group_array[$genre_key]['table_name'],
$group_array[$genre_key]['account_id'],
$group_array[$genre_key]['visible'],
$data_title
);
}
}
}
// ... end
// function that receives passed values - start
function dbUpdate($table_name, $account_id, $title_col, $form_value) {
$test_val_arr = [$table_name, $account_id, $title_col, $form_value];
return print_r($test_val_arr);
} // ... end

PHP / Laravel Foreach Loop - Show only Items that belongs to same headline

I have a problem to view some data in the way I want to have it.
Here is a example of the array:
$items =
0 => [
'name' => 'foo'
'description' => 'bar'
'url' => 'http://foobar.com'
'headline' => 'Headline 1'
],
1 => [
'name' => 'uni'
'description' => 'corn'
'url' => 'http://unicorn.com'
'headline' => 'Headline 1'
],
2 => [
'name' => 'awe'
'description' => 'some'
'url' => 'http://awesome.com'
'headline' => 'Headline 2'
],
And know I want to loop through the items array and want to show the headline at first and all items that have the same headline. If a item has another headline, I want to print out the other headline and the items that belongs to it.
Should look like that:
Headline 1 : <--- Items that do have this headline
name = foo
description = bar
url = http://foobar.com
name = uni
description = corn
url = http://unicorn.com
Headline 2 <----- items with a new headline
name = awe
description = some
url = http://awesome.com
I wansn't able to do that. Can someone help me there?
I've tried something like a for loop that checks the current headline with the next headline.
#for ($i = 0; $i <= count($items); $i++)
<span>{{ $items[$i]['headline'] }}</span>
#if($items[$i]['headline'] == $items[$i+1]['headline'])
.....
# else .....
#endfor
But this haven't worked well
Thanks for your help and sorry because of my bad english!
Use laravel collections with groupby() method
$collection = collect($items);
$items= $collection->groupBy('headline');
$items->toArray();
The array will be splited by headline
From Laravel Docs https://laravel.com/docs/5.4/collections#method-groupby
If your array was a collection before converting to an array, you could use groupBy() collection method:
$collection->groupBy('headline');
Maybe this could help. i am writing this code in core PHP
$arr = array();
foreach($items as $item) {
$arr[$item['headline']] = $item;
}
it will return you an array of something like
Array
(
[Headline 1] => Array
(
[name] => uni
[description] => corn
[url] => http://unicorn.com
[headline] => Headline 1
)
[Headline 2] => Array
(
[name] => awe
[description] => some
[url] => http://awesome.com
[headline] => Headline 2
)
)

Combine arrays with a common value into a new array

I am working on a project and I am stuck on this my question is I have one array which is like below
$arr1 = array(
array
(
'id' => '1',
'city' => 'A.bad',
),
array
(
'id' => '2',
'city' => 'Pune',
),
array
(
'id' => '1',
'city' => 'Mumbai',
)
);
and I have to compare the this by id and I want the output like below.
$result = array(
array(
'id'='1',
'city'='A.bad','Mumbai'
),
array(
'id'='2',
'city'='Pune'
)
);
if we have same id as in the first one is A.bad so it will take it and in the third one it has id 1 and city as mumbai so it will combine as id 1 and city as a.bad,mumbai and other records are filtered in same manner.
Loop through the array and generate a new array depending on the id.You can try this -
$new = array();
foreach($arr1 as $array) {
$new[$array['id']]['id']= $array['id'];
// check if the city value set for that id
// if set the concatenate else set with the city name
$new[$array['id']]['city']= (isset( $new[$array['id']]['city'])) ? ($new[$array['id']]['city'] . ',' . $array['city']) : $array['city'];
}
Fiddle
If you are getting that data from database the you can group them in the query also.

Get all the belongsTo relations plus some HABTM at CakePHP

I'm trying to get only one HABTM relationship when using paginate using CakePHP 2.3.
Right now I'm getting this result:
Array
(
[0] => Array
(
[Ticket] => Array ( .....)
[User] => Array ( .....)
[Priority] => Array ( .....)
[Status] => Array(....)
[Attachment] => Array(....)
....
)
[1] => ....
[2] => ....
)
By using:
$this->Ticket->recursive = 0;
$this->paginate = array(
'limit' => 20
);
I am using $this->Ticket->recursive = 0; because I'm only interested in the belongsTo relationships with other tables, but at the same time, I would like to get only one of the HABTM relationships.
I know I could achieve it adding each of the single relationships in the contain array, like so:
$this->Ticket->recursive = 0;
$this->paginate = array(
'limit' => 20,
'contain' => array(
'Ticket`, `User`, `Priority`, `Status`, `Attachment`, `Tags`...
)
);
But I have plenty of tables and I was wondering if there's any way to do it without having to name every single table in the contain array.
I've also tried to use the recursive array inside the contains one like so:
//getting the filtered tickets
$this->paginate = array(
'limit' => 20,
'contain' => array(
'Ticket' => array('recursive' => 1),
'Tag' //this is the hasMany relationship
)
);
But it didn't work. Is there any way to achieve this without having to name all the tables to make it more simple to maintain?
Ok, as it seems there's no solution for it, I managed to do it by getting the associated models of the type belongsTo and then adding the model name of the HABTM relationship:
$associations = $this->Ticket->getAssociated('belongsTo');
$associations[] = 'Tag';
//getting the filtered tickets
$this->paginate = array(
'limit' => 20,
'contain' => $associations
);

How to insert product details in drupal_commerce product table

I have created a E-commerce site by using Drupal commerce which is a clone of a shopping site created by Ektron. Now my client requirement is that if he insert a product in that site(which is created in Ektron) it will also incorporated in my site when we run import.
So I need to know how drupal_commerce insert their product details in the table.Any help regarding this wold be appreciable . If anyone need any further clarification please let me know .
Also I have some entity fields as well . Like author name , least price etc how to insert those values as well
Thanks in advance
Try this one ..
$values = array (
'price' => 9271.00 ,
'currency_code' => 'USD' ,
);
$cp = commerce_product_new('product');
$cp->is_new = TRUE;
$cp->revision_id = NULL;
$cp->uid = 1;
$cp->status = 1;
$cp->created = $cp->changed = time();
$cp->sku = '#12sku';
$cp->title = 'New Title';
$cp->language = 'und';
$cp->commerce_price = array ( LANGUAGE_NONE => array ( 0 => array (
'amount' => $values[ 'price' ] ,
'currency_code' => $values[ 'currency_code' ],
) ) );
$cp->field_list_price = array ( LANGUAGE_NONE => array ( 0 => array (
'amount' => '300.00' ,
'currency_code' => $values[ 'currency_code' ],
) ) );
$cp->field_isbn = array ( LANGUAGE_NONE => array ( 0 => array (
'value' => '#isbnn' ,
) ) );

Categories