create dynamic multidimensional array using PHP - php

I want to create dynamic multidimensional array using PHP
I need to generate array like this ...
$fetchMenu = array('page-1' => array(
'name' => 'first_page',
'label' => 'First page 2',
'route' => 'product_index',
'pages' => array(
array(
'name' => 'xxx',
'label' => 'xxx',
'route' => 'product_index',
), array(
'id' => 'permissions',
'label' => 'Permissions',
'title' => 'Permissions',
'route' => 'product_add',
'menu_tree_path' => 'default|system|roles_and_permission|permissions',
'display_in_menu' => true,
)
),
),
'page-2' => array(
'name' => 'second_page',
'label' => 'Second page 2',
'route' => 'product_index',
'pages' => array(),
),);
How can I do it ?

something like this?
$dynMenu = array();
$bookLength = 5; //function getBookLength() ??
for($i=0;$i<$bookLength;$i++){
$pageNumber = 'page'.$i;
$page = $i; // function getPages() ??
$dynMenu[$pageNumber] = array('name' => 'foo', 'label' => 'bar');
for($j=0;$j<$page;$j++){
$dynMenu[$pageNumber][$j] = array('name' => 'foo-ish', 'label' => 'bar-ish');
}
}

Related

How to get value of element in the same array when initialize

I Think it's a strange question :
i dont need answer with loop or push to array i'm just wondering about the idea
I'm trying ti initialize an array ,, but i need to read the previous element like this,
return $recourd_list = array(
'clients'=> array(
'route' => 'clients.index',
'title' => 'fullname',
'list' => Clients::orderBy('id', 'desc')->take(5)->get(),
'count' => Clients::count(),
'class' => 'bgm-lightgreen',
'diff' => 5 - $recourd_list['clients']['count'],
),
'sliders'=> array(
'route' => 'sliders',
'title' => 'title',
'list' => Sliders::orderBy('id', 'desc')->take(5)->get(),
'count' => Sliders::count(),
'class' => 'bgm-rightBlue Oil-color',
'diff' => 5 - $recourd_list['sliders']['count'],
),
'sponsors'=> array(
'route' => 'sponsors.index',
'title' => 'title',
'list' => Sponsors::orderBy('id', 'desc')->take(5)->get(),
'count' => Sponsors::count(),
'class' => 'bgm-nave dark-green',
'diff' => 5 - $recourd_list['sponsors']['count'],
),
'packages'=> array(
'route' => 'packages',
'title' => 'title',
'list' => Packages::orderBy('id', 'desc')->take(5)->get(),
'count' => Packages::count(),
'class' => 'bgm-bluegray',
'diff' => 5 - $recourd_list['packages']['count'],
),
'event_schedule'=> array(
'route' => 'event_schedule.index',
'title' => 'title',
'list' => EventSchedule::orderBy('id', 'desc')->take(5)->get(),
'count' => EventSchedule::count(),
'class' => 'bgm-orange',
'diff' => 5 - $recourd_list['event_schedule']['count'],
),
);
I need to read the count element in diff element .. is that way to do that when initialize array
Thanks All
No, this isn't possible as the array technically won't be defined at that point.
One way to achieve this would be to use something like array_map.
http://php.net/manual/en/function.array-map.php
$recourd_list = array(
'clients'=> array(
'route' => 'clients.index',
'title' => 'fullname',
'list' => Clients::orderBy('id', 'desc')->take(5)->get(),
'count' => Clients::count(),
'class' => 'bgm-lightgreen',
),
'sliders'=> array(
'route' => 'sliders',
'title' => 'title',
'list' => Sliders::orderBy('id', 'desc')->take(5)->get(),
'count' => Sliders::count(),
'class' => 'bgm-rightBlue Oil-color',
),
'sponsors'=> array(
'route' => 'sponsors.index',
'title' => 'title',
'list' => Sponsors::orderBy('id', 'desc')->take(5)->get(),
'count' => Sponsors::count(),
'class' => 'bgm-nave dark-green',
),
'packages'=> array(
'route' => 'packages',
'title' => 'title',
'list' => Packages::orderBy('id', 'desc')->take(5)->get(),
'count' => Packages::count(),
'class' => 'bgm-bluegray',
),
'event_schedule'=> array(
'route' => 'event_schedule.index',
'title' => 'title',
'list' => EventSchedule::orderBy('id', 'desc')->take(5)->get(),
'count' => EventSchedule::count(),
'class' => 'bgm-orange',
),
);
then
return array_map(function ($item) {
$item['diff'] = $item['count'] - 5;
return $item;
}, $recourd_list);
Alternatively, you could use map with Collections
https://laravel.com/docs/5.3/collections#method-map
return collect($recourd_list)->map(function ($item) {
$item['diff'] = $item['count'] - 5;
return $item;
})->toArray();
Hope this helps!
short answer:
no.
But since you are, at init time, defining the Count, why not do this:
'diff' => 5 - Clients::count(),
so with the entire example:
return $recourd_list = array(
'clients' => array(
'route' => 'clients.index',
'title' => 'fullname',
'list' => Clients::orderBy('id', 'desc')->take(5)->get(),
'count' => Clients::count(),
'class' => 'bgm-lightgreen',
'diff' => 5 - Clients::count(),
),
'sliders' => array(
'route' => 'sliders',
'title' => 'title',
'list' => Sliders::orderBy('id', 'desc')->take(5)->get(),
'count' => Sliders::count(),
'class' => 'bgm-rightBlue Oil-color',
'diff' => 5 - Sliders::count(),
),
'sponsors' => array(
'route' => 'sponsors.index',
'title' => 'title',
'list' => Sponsors::orderBy('id', 'desc')->take(5)->get(),
'count' => Sponsors::count(),
'class' => 'bgm-nave dark-green',
'diff' => 5 - Sponsors::count(),
),
'packages' => array(
'route' => 'packages',
'title' => 'title',
'list' => Packages::orderBy('id', 'desc')->take(5)->get(),
'count' => Packages::count(),
'class' => 'bgm-bluegray',
'diff' => 5 - Packages::count(),
),
'event_schedule' => array(
'route' => 'event_schedule.index',
'title' => 'title',
'list' => EventSchedule::orderBy('id', 'desc')->take(5)->get(),
'count' => EventSchedule::count(),
'class' => 'bgm-orange',
'diff' => 5 - EventSchedule::count(),
),
);

Split Mailalerts module recipients in prestashop

I need a specific list of recipients to receive stocks alerts only and another list of recipients to receive only the new order alert.
What is the best way to do it?
Thank you
Here is how I would deal with this question:
Create the override file /override/modules/mailalerts/mailalerts.php with this code:
<?php
class MailAlertsOverride extends MailAlerts
{
protected $merchant_mails_stock;
protected function init()
{
parent::init();
$this->merchant_mails_stock = str_replace(',', self::__MA_MAIL_DELIMITOR__, (string)Configuration::get('MA_MERCHANT_MAILS_STOCK'));
}
public function install($delete_params = true)
{
if (! parent::install($delete_params))
return false;
if ($delete_params)
{
Configuration::updateValue('MA_MERCHANT_MAILS_STOCK', Configuration::get('PS_SHOP_EMAIL'));
}
return true;
}
public function uninstall($delete_params = true)
{
if ($delete_params)
{
Configuration::deleteByName('MA_MERCHANT_MAILS_STOCK');
}
return parent::uninstall();
}
protected function postProcess()
{
$errors = array();
if (Tools::isSubmit('submitMAMerchant'))
{
$emails = (string)Tools::getValue('MA_MERCHANT_MAILS_STOCK');
if (!$emails || empty($emails))
$errors[] = $this->l('Please type one (or more) e-mail address');
else
{
$emails = str_replace(',', self::__MA_MAIL_DELIMITOR__, $emails);
$emails = explode(self::__MA_MAIL_DELIMITOR__, $emails);
foreach ($emails as $k => $email)
{
$email = trim($email);
if (!empty($email) && !Validate::isEmail($email))
{
$errors[] = $this->l('Invalid e-mail:').' '.Tools::safeOutput($email);
break;
}
elseif (!empty($email) && count($email) > 0)
$emails[$k] = $email;
else
unset($emails[$k]);
}
$emails = implode(self::__MA_MAIL_DELIMITOR__, $emails);
if (!Configuration::updateValue('MA_MERCHANT_MAILS_STOCK', (string)$emails))
$errors[] = $this->l('Cannot update settings');
}
}
if (count($errors) > 0)
{
$this->html .= $this->displayError(implode('<br />', $errors));
return $this->init();
}
parent::postProcess();
}
public function hookActionUpdateQuantity($params)
{
$this->merchant_mails = $this->merchant_mails_stock;
parent::hookActionUpdateQuantity($params);
}
public function renderForm()
{
$fields_form_1 = array(
'form' => array(
'legend' => array(
'title' => $this->l('Customer notifications'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'switch',
'is_bool' => true, //retro compat 1.5
'label' => $this->l('Product availability'),
'name' => 'MA_CUSTOMER_QTY',
'desc' => $this->l('Gives the customer the option of receiving a notification when an out-of-stock product is available again.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'switch',
'is_bool' => true, //retro compat 1.5
'label' => $this->l('Order edit'),
'name' => 'MA_ORDER_EDIT',
'desc' => $this->l('Send a notification to the customer when an order is edited.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right',
'name' => 'submitMailAlert',
)
),
);
$fields_form_2 = array(
'form' => array(
'legend' => array(
'title' => $this->l('Merchant notifications'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'switch',
'is_bool' => true, //retro compat 1.5
'label' => $this->l('New order'),
'name' => 'MA_MERCHANT_ORDER',
'desc' => $this->l('Receive a notification when an order is placed.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'switch',
'is_bool' => true, //retro compat 1.5
'label' => $this->l('Out of stock'),
'name' => 'MA_MERCHANT_OOS',
'desc' => $this->l('Receive a notification if the available quantity of a product is below the following threshold.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'text',
'label' => $this->l('Threshold'),
'name' => 'MA_LAST_QTIES',
'class' => 'fixed-width-xs',
'desc' => $this->l('Quantity for which a product is considered out of stock.'),
),
array(
'type' => 'switch',
'is_bool' => true, //retro compat 1.5
'label' => $this->l('Coverage warning'),
'name' => 'MA_MERCHANT_COVERAGE',
'desc' => $this->l('Receive a notification when a product has insufficient coverage.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'text',
'label' => $this->l('Coverage'),
'name' => 'MA_PRODUCT_COVERAGE',
'class' => 'fixed-width-xs',
'desc' => $this->l('Stock coverage, in days. Also, the stock coverage of a given product will be calculated based on this number.'),
),
array(
'type' => 'switch',
'is_bool' => true, //retro compat 1.5
'label' => $this->l('Returns'),
'name' => 'MA_RETURN_SLIP',
'desc' => $this->l('Receive a notification when a customer requests a merchandise return.'),
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'textarea',
'cols' => 36,
'rows' => 4,
'label' => $this->l('E-mail addresses'),
'name' => 'MA_MERCHANT_MAILS',
'desc' => $this->l('One e-mail address per line (e.g. bob#example.com).'),
),
array(
'type' => 'textarea',
'cols' => 36,
'rows' => 4,
'label' => $this->l('E-mail stock addresses'),
'name' => 'MA_MERCHANT_MAILS_STOCK',
'desc' => $this->l('One e-mail address per line (e.g. bob#example.com).'),
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right',
'name' => 'submitMAMerchant',
)
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->module = $this;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitMailAlertConfiguration';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name
.'&tab_module='.$this->tab
.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form_1, $fields_form_2));
}
public function getConfigFieldsValues()
{
$config = parent::getConfigFieldsValues();
$config['MA_MERCHANT_MAILS_STOCK'] = Tools::getValue('MA_MERCHANT_MAILS_STOCK', Configuration::get('MA_MERCHANT_MAILS_STOCK'));
return $config;
}
}

Codeigniter: Parsing array data in view in a loop

i want to multidimensional array to my view and then use this array to build form
This is what i've in my controller
function signin(){
$attributes = array(
'name' =>
array(
'name' => 'name',
'type' => 'text',
'placeholder' => '' ,
'value' => 'value'
),
'password' =>
array(
'name' => 'name',
'type' => 'password',
'placeholder' => '',
),
'gender' =>
array(
'name' => 'name',
'type' => 'select',
'value'=>
array(
'male','female'
),
),
'usertpye'=>array(
'type' => 'radio',
'seller' => 'seller',
'buyer' => 'buyer'
),
'upload'=>array(
'type' => 'file',
'name' => 'file'
),
'submit'=>array(
'type' => 'submit',
'name' => 'submit',
'value' => 'submit'
)
);
$this->load->view('login',$attributes);
}
in my view login i can access these items like $name or $password but i want to fetch in a loop.really have no idea how can i do it please help.
The load function receives an array, which keys then parses as variables in the view. Thus, you get variables like $name, $password etc.
Just add another layer before calling the load function like:
$data['attributes'] = $attributes;
And then, when loading the view do
$this->load->view('login',$data);
Here is the array a bit adjusted:
$attributes = array(
'name' =>
array(
'name' => 'name',
'type' => 'text',
'placeholder' => '' ,
'value' => 'value'
),
'password' =>
array(
'name' => 'name',
'type' => 'password',
'placeholder' => '',
),
'gender' =>
array(
'name' => 'name',
'type' => 'select',
'options' => array(
'male' => 'Male',
'female' => 'Female'
),
),
'usertpye'=>array(
'type' => 'radio',
'values' => array(
'seller' => 'seller',
'buyer' => 'buyer'
)
),
'upload'=>array(
'type' => 'file',
'name' => 'file'
),
'submit'=>array(
'type' => 'submit',
'name' => 'submit',
'value' => 'submit'
)
);
Here is how it would look like with the form helper of CI (this would go in the view, remember to first load the helper in the controller):
echo form_open('email/send');
foreach($attributes as $key=>$attribute) {
echo form_label($key).'<br/>';
if($attribute['type'] == 'select') {
echo form_dropdown($attribute['name'],$attribute['options']).'<br/>';
} elseif($attribute['type'] == 'radio') {
foreach ($attribute['values'] as $value) {
echo form_label($value);
echo form_radio(array('name' => $key, 'value' => $value)).'<br/>';
}
} else {
echo form_input($attribute).'<br/>';
}
}
Note I did some adjustments to your initial attributes array to make it work, but you'd still need to improve its structure, add unique names for all items etc.

Prestashop 1.6 Module error: Notice on line 719 in file prestashop16\tools\smarty\sysplugins\smarty_internal_templatebase.php

I'm developing a module and extended AdminFeaturesController.php to display my custom field Add/Edit Feature Value, but it is showing following error in popup:
Notice on line 719 in file
D:\xampp\htdocs\prestashop16\tools\smarty\sysplugins\smarty_internal_templatebase.php(157)
: eval()'d code [8] Undefined index: value
I think it is due to I override the function initFormFeatureValue() in my AdminFeaturesController.php file and added a new field. Here is the code for that:
public function initFormFeatureValue()
{
$this->setTypeValue();
$this->fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Feature value'),
'icon' => 'icon-info-sign'
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Feature'),
'name' => 'id_feature',
'options' => array(
'query' => Feature::getFeatures($this->context->language->id),
'id' => 'id_feature',
'name' => 'name'
),
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('Value'),
'name' => 'value',
'lang' => true,
'size' => 33,
'hint' => $this->l('Invalid characters:').' <>;=#{}',
'required' => true
),
array(
'type' => 'select',
'label' => $this->l('Parent Feature Value'),
'name' => 'parent_id_feature_value',
'options' => array(
'query' => FeatureValue::getFeatureValues((int)Tools::getValue('id_feature')),
'id' => 'id_feature_value',
'name' => 'value'
),
'required' => true
),
),
'submit' => array(
'title' => $this->l('Save'),
),
'buttons' => array(
'save-and-stay' => array(
'title' => $this->l('Save then add another value mamu'),
'name' => 'submitAdd'.$this->table.'AndStay',
'type' => 'submit',
'class' => 'btn btn-default pull-right',
'icon' => 'process-icon-save'
)
)
);
$this->fields_value['id_feature'] = (int)Tools::getValue('id_feature');
// Create Object FeatureValue
$feature_value = new FeatureValue(Tools::getValue('id_feature_value'));
$this->tpl_vars = array(
'feature_value' => $feature_value,
);
$this->getlanguages();
$helper = new HelperForm();
$helper->show_cancel_button = true;
$back = Tools::safeOutput(Tools::getValue('back', ''));
if (empty($back))
$back = self::$currentIndex.'&token='.$this->token;
if (!Validate::isCleanHtml($back))
die(Tools::displayError());
$helper->back_url = $back;
$helper->currentIndex = self::$currentIndex;
$helper->token = $this->token;
$helper->table = $this->table;
$helper->identifier = $this->identifier;
$helper->override_folder = 'feature_value/';
$helper->id = $feature_value->id;
$helper->toolbar_scroll = false;
$helper->tpl_vars = $this->tpl_vars;
$helper->languages = $this->_languages;
$helper->default_form_language = $this->default_form_language;
$helper->allow_employee_form_lang = $this->allow_employee_form_lang;
$helper->fields_value = $this->getFieldsValue($feature_value);
$helper->toolbar_btn = $this->toolbar_btn;
$helper->title = $this->l('Add a new feature value');
$this->content .= $helper->generateForm($this->fields_form);
}
Any idea why it is showing this error? Also, it is not populating my custom field.
OK, I fixed it. The problem was with options array in my new field array. Following is the correct one.
array(
'type' => 'select',
'label' => $this->l('Parent Feature Value'),
'name' => 'parent_id_feature_value',
'options' => array(
'query' => FeatureValue::getFeatureValuesWithLang($this->context->language->id, $parent_id),
'id' => 'id_feature_value',
'name' => 'value'
),
'required' => true
),
Here I had to override the FeatureValue class and add the getFeatureValuesWithLang() function.
public static function getFeatureValuesWithLang($id_lang, $id_feature, $custom = false)
{
return Db::getInstance()->executeS('
SELECT *
FROM `'._DB_PREFIX_.'feature_value` v
LEFT JOIN `'._DB_PREFIX_.'feature_value_lang` vl
ON (v.`id_feature_value` = vl.`id_feature_value` AND vl.`id_lang` = '.(int)$id_lang.')
WHERE v.`id_feature` = '.(int)$id_feature.'
'.(!$custom ? 'AND (v.`custom` IS NULL OR v.`custom` = 0)' : '').'
ORDER BY v.`position` ASC
', true, false);
}
What is actually does is that it was finding the 'value' field which was not present in the query result. So, I override FeatureValue class and add the above method and called that one. It solved the problem.

ZF2 tree routing

How to configure ZF2 tree pages routing?
I have multilevel pages. For example:
contacts
contacts/office
contacts/office/office1
products
products/category1
products/category1/product1
and more.
I use one controller for page and this config for routing:
'page' => array(
'type' => 'Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'Application\PageController'
)
),
'priority' => -1000,
'may_terminate' => false,
'child_routes' => array(
'name' => array(
'type' => 'Segment',
'options' => array(
'route' => ':sn1[/:sn2[/:sn3[/:sn4[/:sn5[/:sn6]]]]]',
'constraints' => array(
'sn1' => '[a-zA-Z][a-zA-Z0-9]*',
'sn2' => '[a-zA-Z][a-zA-Z0-9]*',
'sn3' => '[a-zA-Z][a-zA-Z0-9]*',
'sn4' => '[a-zA-Z][a-zA-Z0-9]*',
'sn5' => '[a-zA-Z][a-zA-Z0-9]*',
'sn6' => '[a-zA-Z][a-zA-Z0-9]*'
),
'defaults' => array(
'controller' => 'Application\PageController',
'action' => 'page'
)
)
)
)
)
Page action:
$params = array('sn1', 'sn2', 'sn3', 'sn4', 'sn5', 'sn6');
$names = array();
foreach ($params as $param) {
if($this->params($param, NULL) === NULL) {
break;
} else {
$names[] = $this->params($param, NULL);
}
}
var_dump($names);
$path = implode('/', $names);
var_dump($path);
Result for http://example.com/contacts/office/office1:
array (size=3)
0 => string 'contacts' (length=8)
1 => string 'office' (length=6)
2 => string 'office1' (length=7)
string 'contacts/office/office1' (length=23)
Are there any better way to implement?

Categories