简体   繁体   中英

Form break in foreach loop in codeigniter

Just consider I have 30 questions in my database. I'm doing Foreach to display all questions, so it's displaying all questions in one form page Example:-

    1 Question...
      answer(textarea)
    2 Question...
      answer(textarea)
    .
    .
    .
   30 Question...
      answer(textarea)

Now I want to show 10 questions per page with NEXT button at the END of 30th Question will have SUBMIT Button. So that I can insert post values into database.

How can I make this ? Is there any jquery plugin or I do manually ? Please Suggested I appreciate your answer

Well, the idea here is loop everything to the page in one go, though wrap every 10 questions in a div with an class, making sure to add style:hidden to the all but the first.

Then you can add buttons below to just show then hide the divs one after the other.

So something like this?

    <form class="" action="index.php" method="post">
            <?php $i = 0; ?>
            <?php foreach ($questions as $q): ?>
                <?php if (($i % 10) == 0): //Multipal of 10 ?>
                    <?php if ($i == 0): ?>
                        <div class="page">
                    <?php else: ?>
                        <div class="page" style="display:none;">
                    <?php endif; ?>

                <?php endif; ?>

                <label for="<?=$q?>">Label: <?=$q?></label><input type="text" name="<?=$q?>"><br/>

                <?php $i++; ?>
                <?php if (($i % 10) == 0): ?>
                    <?php if (count($questions) != $i): ?>
                        <button type="button" class="next-page">Next</button>
                    <?php else: ?>
                        <input type="submit" name="name" value="Submit">
                    <?php endif; ?>
                    </div>
                <?php else: ?>
                <?php endif; ?>

            <?php endforeach; ?>


    </form>
    <script type="text/javascript">
        $('.next-page').on('click', function(){
            var currentPage = $('.page');
            currentPage.first().hide();
            currentPage.first().next().show();
        });
    </script>

You can use a page number with you request.

1 st page -> you send the pagenum as 1. so you limit the records by LIMIT (pagenum * 10) 10 - which is LIMIT 10 10

2nd page -> you only change the page num to 2. so you limit the records by LIMIT (pagenum * 10) 10 - which is LIMIT 20 10

This is just an idea which you could implement only with php. there could be several other solutions.

The best solution for this please follows below link

Step-by-Step form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM