Jump to content

Foreach multistep buttons


Irji

Recommended Posts

I have a foreach multistep with buttons :

<?php

$layout =  array('one', 'too', 'three', 'four');

    $keys= array_keys($layout);

    $total=count($keys);

    ?>

    <div class="steps">

        <?php

        $i=0;$step_name='';

        foreach ($layout as $value):

            $i++;

            switch ($value) {

                case 'one':

                    $step_name=esc_html__('One');

                    break;

                case 'too':

                    $step_name=esc_html__('Too');

                    break;

                case 'three':

                    $step_name=esc_html__('Three');

                    break;

                case 'four':

                    $step_name=esc_html__('Four');

                    break;

            }

            ?>

        <?php endforeach;?>

    </div>

    <form action="<?php echo esc_url($action); ?>" method="post" id="submit_form" class="manager-form"

          enctype="multipart/form-data">

        foreach ($layout as $value) {

            $index = array_search($value,$keys);

            $prev_key = $next_key= '';

            if($index>0)

            {

                $prev_key = $keys[$index-1];

            }

            if($index<$total-1){

                $next_key = $keys[$index+1];

            }

            ?>

            <fieldset tabindex="-1" id="step-<?php echo esc_attr($value); ?>">

                <div class="ere-step-nav">

                <?php

                if($prev_key!='1'):?>

                    <button class="btn-prev" aria-controls="step-<?php echo esc_attr($prev_key); ?>"

                        type="button" title="<?php esc_attr_e('Previous') ?>"><i class="fa fa-angle-left"></i><span><?php esc_html_e('Previous') ?></span></button>

                <?php endif; ?>

                    <button class="btn-all" type="button" title="<?php esc_attr_e('All Fields') ?>"><?php esc_html_e('All') ?></button>

                <?php if($next_key!=''):?>

                    <button class="btn-next" aria-controls="step-<?php echo esc_attr($next_key); ?>"

                        type="button" title="<?php esc_attr_e('Next') ?>"><span><?php esc_html_e('Next') ?></span><i class="fa fa-angle-right"></i></button>

                <?php else:?>

                    <input type="submit" name="submit_button" class="btn-submit"

                           value="Submit>"/>

                <?php endif;?>

                </div>

            </fieldset>

    </form>

Everything is working.

But the Prev button appears on the first step (and should be from the second step).

The Submit button should be on the last step instead of the Next button, but it's not there.
I'm just starting to learn php. Please tell me how can I fix this. What am I doing wrong.

Any help would be appreciated.
Thanks!

Link to comment
Share on other sites

I don't think you've posted your code accurately: you said everything is working, but on my screen it doesn't make sense. I presume you want four fieldlists labeled "One," "Too," etc. You can't accomplish this with the code you posted: you print the labels first all at once, the fieldlists second all at once; thus, all of the labels are clustered together on top of the fieldlists.

You've made this much, much harder than it needs to be.

$keys= array_keys($layout);
$total=count($keys);

You don't need this. You can get the same result with 

$total = count($layout);

Your first foreach loop echos values but on the screen they're not attached to anything. They're just sorta hangin' out there. Lose it. You don't need it.

You don't need prev_key and next_key. If what you display is entirely dependent upon the index at the time, you just need to know that index. For that you can do this in your second foreach loop:

foreach ($layout as $k => $v) {

$k is your index. 

if($prev_key!='1'): 
...
if($next_key!=''):

If your goal is to put buttons in a certain order, and that order is determined arbitrarily using the layout array (i.e. if idx = 1 do buttonx, if idx = 2 do buttony), you don't need these variables or checks. 

Your entire code can be written as an array definition and single foreach loop, like so: 

$layout =  array('One', 'Too', 'Three', 'Four');
$total = count($layout);

foreach($layout as $k => $value) { // $k is your index
?>

<form action="myaction" method="post" id="submit_form" class="manager-form" enctype="multipart/form-data">

    <fieldset tabindex="-1" id="step-<?php echo esc_attr($value); ?>">

    <!-- fieldset is a block level element, so you don't need the div tag -->
    <!-- div class="ere-step-nav" -->

    <?php if($k > 0): ?>
        <!-- only show previous when we're not on the zero-th element -->
        <button class="btn-prev" aria-controls="step-<?php echo esc_attr($k); ?>"

        type="button" title="<?php esc_attr_e('Previous') ?>"><i class="fa fa-angle-left"></i><span><?php esc_html_e('Previous') ?></span></button>

    <?php endif; ?>

    <!-- this button shows all the time -->
    <button class="btn-all" type="button" title="<?php esc_attr_e('All Fields') ?>"><?php esc_html_e('All') ?></button>

    <?php if ($k < ($total - 1)): ?>
        <!-- only show the next button when we're not on the last element -->
        <button class="btn-next" aria-controls="step-<?php echo esc_attr($k); ?>"

        type="button" title="<?php esc_attr_e('Next') ?>"><span><?php esc_html_e('Next') ?></span><i class="fa fa-angle-right"></i></button>

    <?php endif; ?>

        <!-- the submit button shows all the time -->
        <input type="submit" name="submit_button" class="btn-submit" value="Submit>"/>

        <!-- /div -->

    </fieldset>

</form>
        <?php
}

Questions?

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.