Irji Posted November 10, 2022 Share Posted November 10, 2022 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! Quote Link to comment https://forums.phpfreaks.com/topic/315516-foreach-multistep-buttons/ Share on other sites More sharing options...
BillInKCMO Posted November 11, 2022 Share Posted November 11, 2022 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? Quote Link to comment https://forums.phpfreaks.com/topic/315516-foreach-multistep-buttons/#findComment-1602483 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.