Dynamically create jQuery Tabs Markup from PHP Array - php

What I am trying to achieve is create a jQuery Tabs markup from PHP Array. Basic intention is to add tabs and content dynamically. Both existing StackExchange answers and Google Search left me empty handed here. Finding it pretty dificult with my level of PHP knowledge.
Here are one of the many code that I have tried:
My Array:
$args = array();
$args[] = array(
'name' => 'Shortcode Name',
'tabname' => 'Shortcode Tab Name', //Same As Tab 4
'action' => 'shortcodeaction',
'icon' => 'codeicon',
'image' => 'codeimage',
);
$args[] = array(
'name' => 'Shortcode2 Name',
'tabname' => 'Shortcode2 Tab Name',
'action' => 'shortcodeaction2',
'icon' => 'codeicon2',
'image' => 'codeimage2',
);
$args[] = array(
'name' => 'Shortcode3 Name',
'tabname' => 'Shortcode3 Tab Name',
'action' => 'shortcodeaction3',
'icon' => 'codeicon3',
'image' => 'codeimage3',
);
$args[] = array(
'name' => 'Shortcode4 Name',
'tabname' => 'Shortcode Tab Name', //Same As Tab 1
'action' => 'shortcodeaction4',
'icon' => 'codeicon4',
'image' => 'codeimage4',
);
$args[] = array(
'name' => 'Shortcode5 Name',
'tabname' => 'Shortcode5 Tab Name',
'action' => 'shortcodeaction5',
'icon' => 'codeicon5',
'image' => 'codeimage5',
);
The PHP Code:
echo '<ul>';
foreach ( $args as $arg ) {
echo '<li>'.$arg['tabname'].'</li>';
}
echo </ul>
foreach ( $args as $arg ) {
echo '<div id="' . preg_replace("/[^a-z0-9]+/", "", $arg['tabname'] ) . '"></div>';
}
The tabname key value for Tab 1 and Tab 4 are same, so they should go under same tab name and tab content. This is exactly where I am getting lost. I know I can do a foreach loop to create tabs, but those two are not going under same tab instead they are creating new tab with same name.
Used this to get the count for array with unique value for tabname:
$lists = wp_list_pluck( $args, 'tabname' );
$counts = count( array_unique( $lists ));
// 4
Now a foreach loop will create 3 tabs, but not sure how to pass the $args inside the loops.
I am not asking you to do this for me, but a little head start would be great.
Thanks

Related

Check value from Gravity Forms API

I am creating a custom Gravity Forms add-on and it appears to work so far. The settings are showing and saving as expected.
Here's what I have:
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'Animal Types', 'animaltypes' ),
'fields' => array(
array(
'name' => 'gravity_forms_animal_types',
'type' => 'checkbox',
'label' => esc_html__( 'Animal Types', 'animaltypes' ),
'choices' => array(
array(
'label' => esc_html__( 'Cat', 'animaltypes' ),
'name' => 'option_cat',
'default_value' => 0,
),
array(
'label' => esc_html__( 'Dogs', 'animaltypes' ),
'name' => 'option_dog',
'default_value' => 0,
)
)
),
)
)
);
}
But what I can't figure out is how to check, for example, if option_cat has been set so that I can then run a custom function if it is.
So essentially (and I know the below code is not correct) something like this:
if(option_cat == true) {
my_cat_function();
}
In gravity forms when you create a new addon you provide a slug for that addon. Gravity forms save that settings with the help of that slug.
So if you want to get that settings you can use below code.
$settings = get_option('gravityformsaddon_slugname__settings');
$option = rgar( $settings, 'gravity_forms_animal_types' );
In options you can get selection of your settings, and if you want to one selection at a time you must use radio button instead of checkbox.
It was quite simple after all.
$options = get_option('gravityformsaddon_animaltypes_settings');
$cat = $options['option_cat'];
if($cat) {
my_cat_function();
}

How to append a comma to an array var within a foreach loop?

Inside a foreach loop I am assigning an array to a variable.
Because this array is within a loop it will output more than once.
Because it will output more than once I need the end of the array to have a comma so it doesn't break the array for each time it returns and instance of the array.
Is there a way to do this?
- I found ways online but they only showed how to do this with strings in foreach loops, to either add or remove the comma at the end of the last foreach loop.
My code is below to explain.
// ----------------------------------------------------------------------------------------------------
// Start our framework config arrays
// ----------------------------------------------------------------------------------------------------
$options = array();
// ----------------------------------------------------------------------
// MENU - Layout Settings
// ----------------------------------------------------------------------
$options[] =
array(
'title' => 'Layout Settings',
'name' => 'layout-settings',
'icon' => 'fa fa-cog',
'fields' =>
array(
// ----------------------------------------------------------------------
// TAB - Layout Settings
// ----------------------------------------------------------------------
array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => array(
return_post_type_layout_settings()
))
),
);
// Our return array function
function return_post_type_layout_settings() {
$public_post_type = get_post_types(
array(
'_builtin' => TRUE,
'public' => TRUE
)
);
sort($public_post_type, SORT_NATURAL);
foreach($public_post_type as $post_type) {
$layout_options =
array('title' => ucwords($post_type) . ' Layout', 'fields' => array(
array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(
// ----------------------------------------------------------------------
// FIELD - Header Settings Panel
// ----------------------------------------------------------------------
array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
array('id' => $post_type . '_enable_header', 'type' => 'switcher'),
))
)) // << I need to comma to post at the end of this array
// because in my array above this will output more than once
;
return $layout_options;
}
}
Multiple issues with the code. And there is nothing to do with actual commas. But rather the data structure of function output.
Code Receiving the Function output
In this piece of code:
<?php
$options[] =
array(
'title' => 'Layout Settings',
'name' => 'layout-settings',
'icon' => 'fa fa-cog',
'fields' =>
array(
// ----------------------------------------------------------------------
// TAB - Layout Settings
// ----------------------------------------------------------------------
array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => array(
return_post_type_layout_settings()
))
),
);
The tabs is declared only as an array with 1 value. There is no way you can put multiple value into tabs like this. It should be modified like this:
<?php
$options[] =
array(
'title' => 'Layout Settings',
'name' => 'layout-settings',
'icon' => 'fa fa-cog',
'fields' =>
array(
// ----------------------------------------------------------------------
// TAB - Layout Settings
// ----------------------------------------------------------------------
array('type' => 'tabbed', 'id' => 'layout_settings', 'tabs' => return_post_type_layout_settings())
),
);
The Function
Then you need to modify your function to return the correct array format:
function return_post_type_layout_settings() {
$public_post_type = get_post_types(
array(
'_builtin' => TRUE,
'public' => TRUE
)
);
sort($public_post_type, SORT_NATURAL);
$layout_options = array(); // initialize $layout_options as array
foreach($public_post_type as $post_type) {
// append each options into $layout_options
$layout_options[] =
array('title' => ucwords($post_type) . ' Layout', 'fields' => array(
array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(
// ----------------------------------------------------------------------
// FIELD - Header Settings Panel
// ----------------------------------------------------------------------
array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
array('id' => $post_type . '_enable_header', 'type' => 'switcher'),
))
));
}
// return after $layout_options is finished
return $layout_options;
}
Is the goal to have $layout_options be an array of multiple ["title"=>"My Layout", "fields"=>[..]] objects? If so, I'd do something like this:
function return_post_type_layout_settings() {
$public_post_type = get_post_types(
array(
'_builtin' => TRUE,
'public' => TRUE
)
);
sort($public_post_type, SORT_NATURAL);
$layout_options = array();
foreach($public_post_type as $post_type) {
$layout_options[] =
array('title' => ucwords($post_type) . ' Layout', 'fields' => array(
array('id' => $post_type, 'type' => 'grid', 'span' => '6-12', 'fields' => array(
// ----------------------------------------------------------------------
// FIELD - Header Settings Panel
// ----------------------------------------------------------------------
array('message' => 'Enable ' . ucwords($post_type). ' Header Settings?', 'video' => 'QAEjuDpIaE4', 'type' => 'title_with_help'),
array('id' => $post_type . '_enable_header', 'type' => 'switcher'),
))
))
;
}
return $layout_options;
}

CMB2 Multicheck Display Selected Options

I am trying to display the selected options from my functioning CMB2 multicheck metabox. Currently this is returned on the front-end:
check1check2
I am trying to return:
Open Ceiling Drop Ceiling
What am I missing? I could do this in a different way, but it's become a bit of a vendetta for me. Any help is greatly appreciated!
My metabox is here and appears in the appropriate page edit area:
$zf_ind_boxes->add_field( array(
'name' => esc_html__( 'Fan Types', 'cmb2' ),
'desc' => esc_html__( 'For Wassup content. Check all that apply.', 'cmb2' ),
'id' => $prefix . 'ind_fan_types',
'type' => 'multicheck',
'label_cb' => 'get_the_labels',
'options' => array(
'check1' => 'Open Ceiling',
'check2' => 'Drop Ceiling',
'check3' => 'Spot Cooling',
)
) );
In my template I have:
$array = get_post_meta($metafield_id, 'zf_ind_fan_types', true);
if($array) {
foreach ($array as $key => $value){
echo $value;
}
}
If you want display checked values at front-end just replace your section with this one below.
'options' => array(
'Open Ceiling' => 'Open Ceiling', //key => value
'Drop Ceiling' => 'Drop Ceiling', //key => value
'Spot Cooling' => 'Spot Cooling', //key => value
)
Value is hardcoded - so it isn't saved in database, in database is saved only key

Conditionally adding item to multidimensional array

I have been looking how to do this and am a bit stumped.
My array is as follows:
$returndata->setup_array = array(
'General' => array(
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
However I have 1 item that on a certain condition(triggered by the users session) that needs to be added to the listin the general array. The section being 'View Details => setup/viewdetails'. I have tried array push (probably incorrectly) but this adds the item as another array at the end under the main array.
I want/need it to work like this:
$returndata->setup_array = array(
'General' => array(
$viewdets
'Main Details' => 'setup/maindets',
'Directories' => 'directories',
'Extension Allocation' => 'xtnallo',
'List Holidays' => 'setup/holidays',
'List Group VM' => 'groupvm',
'Conference Rooms' => 'confroom'
),
'Offices' => array(
'List Offices' => 'iptoffices'
),
'Users' => array(
'List Users' => 'iptusers'
),
'Phones' => array(
'List Phones' => 'iptphones'
),
);
$viewdets = "'View Details' => 'setup/viewdetails'";
and still be interpreted as a functioning array for use as a menu.
$returndata->setup_array['General']['View Details'] = 'setup/viewdetails'
Cheers Rick!
You can use ArrayObject to have the array as a reference:
$a = new ArrayObject();
$b = array(
"a" => $a
);
$a[] = "foo";
print_r($b);
What did you try calling array_push() on? Have you tried
array_push($returndata->setup_array['General'], $viewdets);
You would need to add the variable to the specific depth of the array you wanted it to be present. check out array_push here, there's also a short language syntax that avoids the function call:
$returndata->setup_array['General'][] = $viewdets;

Adding to Array where Key equals Variable

I always have trouble wrapping my head around these foreach array things, and SO has been an incredibly value resource in getting me there, so I hope you guys can help with this one.
public function progress_bar()
{
$items = array(
array(
'step-name' => 'Setup',
'url' => '/projects/new/setup/',
),
array(
'step-name' => 'Artwork',
'url' => '/projects/new/artwork/',
),
array(
'step-name' => 'Review',
'url' => '/projects/new/review/',
),
array(
'step-name' => 'Shipping',
'url' => '/projects/new/shipping-info/',
),
array(
'step-name' => 'Billing',
'url' => '/projects/new/billing/',
),
array(
'step-name' => 'Receipt',
'url' => '/projects/new/receipt/',
),
);
// Status can be active, editing, or complete.
foreach ($this->progress as $step => $status)
{
foreach ($items as $item)
{
$item['step-name'] == ucfirst($step) ? $item['status'] = $status : '';
}
}
return $items;
}
$this->progress contains an array of statuses ('setup' => 'active', 'artwork' => 'editing')
I want to add to the $items array the status of each matching item in $this->progress
$items = array(
array(
'step-name' => 'Setup',
'url' => '/projects/new/setup',
'status' => 'active',
),
etc...
);
If I understand your question correctly, the problem is that you are trying to add an array element to $items, but what you're actually doing is adding the element to a temporary variable ($item), which does not reference the original $items variable.
I'd suggest approaching it like this:
foreach ($this->progress as $step => $status)
{
// Having the $key here allows us to reference the
// original $items variable.
foreach ($items as $key => $item)
{
if ($item['step-name'] == ucfirst($step) )
{
$items[$key]['status'] = $status;
}
}
}
return $items;
Are you locked into using an array to store $items? If so, you're going to be stuck doing a nested loop ("For each element in $this->progress, check each element in $items. If match, update $items" or something like that). If you have some flexibility, I would use a hash for $items (associative array in php-speak), where the index is the step name. So $items['Setup'] would contain 'url' => ... and 'status' => ... etc. Does that make sense? Then your algorithm breaks down to "For each element in $this->progress, get the element in $items by name ($items[$step_name]) and update it's info."
I would change how you have the $items array keyed and do it like this. Stops you from having the nested loops.
public function progress_bar()
{
$items = array(
'Setup' => array(
'url' => '/projects/new/setup/',
),
'Artwork' => array(
'url' => '/projects/new/artwork/',
),
'Review' => array(
'url' => '/projects/new/review/',
),
'Shipping' => array(
'url' => '/projects/new/shipping-info/',
),
'Billing' => array(
'url' => '/projects/new/billing/',
),
'Receipt' => array(
'url' => '/projects/new/receipt/',
)
);
// Status can be active, editing, or complete.
foreach ($this->progress as $step => $status)
{
$item[ucfirst($step)]['status'] = $status;
}
return $items;
}

Categories