简体   繁体   English

在 codeigniter 中的 foreach 循环中形成中断

[英]Form break in foreach loop in codeigniter

Just consider I have 30 questions in my database.想想我的数据库中有 30 个问题。 I'm doing Foreach to display all questions, so it's displaying all questions in one form page Example:-我正在使用 Foreach 来显示所有问题,因此它在一个表单页面中显示所有问题示例:-

    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.现在我想每页显示 10 个问题,在第 30 个问题结束时使用NEXT按钮将有提交按钮。 So that I can insert post values into database.这样我就可以将帖子值插入到数据库中。

How can I make this ?我怎样才能做到这一点? Is there any jquery plugin or I do manually ?有没有jquery插件或者我手动做? 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.好吧,这里的想法是一次性将所有内容循环到页面上,尽管将每 10 个问题包装在一个带有类的 div 中,确保将 style:hidden 添加到除第一个之外的所有问题。

Then you can add buttons below to just show then hide the divs one after the other.然后你可以在下面添加按钮来显示然后一个接一个地隐藏 div。

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第 1 页 -> 您将 pagenum 发送为 1。因此您将记录限制为LIMIT (pagenum * 10) 10 - 即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第 2 页 -> 您只将页码更改为 2。因此您将记录限制为LIMIT (pagenum * 10) 10 - 即LIMIT 20 10

This is just an idea which you could implement only with php.这只是一个你只能用 php 实现的想法。 there could be several other solutions.可能还有其他几种解决方案。

The best solution for this please follows below link对此的最佳解决方案请遵循以下链接

Step-by-Step form分步表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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