Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:在forEach中,当达到某个条件时停止执行,但稍后继续_Php_Execution_Continue - Fatal编程技术网

PHP:在forEach中,当达到某个条件时停止执行,但稍后继续

PHP:在forEach中,当达到某个条件时停止执行,但稍后继续,php,execution,continue,Php,Execution,Continue,假设我有这样的东西: $count = 0; foreach($CourseDetails as $course_line) { print "<tr><td>{$course_line['class_code']}</td> <td>{$course_line['class_name']}</td> <td>{$course_line['class_unit']}&

假设我有这样的东西:

$count = 0; 
foreach($CourseDetails as $course_line) {
    print "<tr><td>{$course_line['class_code']}</td>
            <td>{$course_line['class_name']}</td>
            <td>{$course_line['class_unit']}</td>
            <td>{$course_line['class_description']}</td>
            <td>{$course_line['class_instructors']}</td>";
    $count = $count + 1
    if ($count = 10);
        $count = 0;
        // code that stops script and makes a "Continue?" button pop up on the webpage under the already outputed details
        // if the client presses the "Continue?" button, allow the script to continue (jump back into the foreach loop)
}
$count=0;
foreach($CourseDetails作为$course\u行){
打印“{$course\u line['class\u code']}
{$course_line['class_name']}
{$course\u line['class\u unit']}
{$course_line['class_description']}
{$course_line['class_instructors']};
$count=$count+1
如果($count=10);
$count=0;
//停止脚本并在已输出的详细信息下的网页上弹出“继续”按钮的代码
//如果客户端按下“Continue”(继续)按钮,则允许脚本继续(跳回foreach循环)
}
我正在试图找到一种方法来停止脚本,然后按一个按钮说“继续?”。如果客户机按下按钮,我希望脚本继续它停止的地方。PHP有任何函数允许这样做吗

简短回答:不


这更像是一个工作流设计问题。我建议创建一个要执行的项目列表。然后尝试执行它们(当它们被成功处理时,将它们标记为完成),如果遇到错误,则停止执行并让用户知道。如果用户希望继续,请重新加载页面,并继续执行初始列表中尚未完成的项目。

一旦将页面加载到浏览器中,则无法停止脚本。只能在脚本内部创建中断条件。页面之间的“内存”有两种方式,一种是传递URL变量,例如start=20,另一种是使用$\u会话变量来记住最后一个中断点。最好通过URL传递变量,这样页面就可以单独访问

然后,在您的循环中,只需评估条目是否具有高于最后一个中断的数字。下面假定您的课程位于一个带有数字索引的数组中,在这种情况下,$计数是多余的。如果要从数据库中提取它们,请采用完全不同的方法

$start = !empty($_GET['start']) ? $_GET['start'] : 0;
$offset = 10;

foreach($CourseDetails as $course_num => $course_line) {

    if ($course_num < $start) continue; // These were already seen.

    if ($course_num >= $start + $offset) {
        // Interrupt and make a button that links to ?start=$start+$offset.
        break;
    }

    // Here display stuff.
}
$start=!空($\u GET['start'])$_获取['start']:0;
$offset=10;
foreach($CourseDetails作为$course\u num=>$course\u行){
如果($course_num<$start)继续;//这些已被看到。
如果($course\u num>=$start+$offset){
//中断并制作一个链接到?start=$start+$offset的按钮。
打破
}
//这里展示东西。
}

用Javascript怎么办?Javascript仅在服务器端PHP完成执行并将页面传递到浏览器后,在浏览器端执行。要将用户输入返回到PHP,需要重新加载页面。或者,进一步修改您的设计,加载PHP页面,然后执行一系列从Javascript返回PHP的AJAX调用,并逐项执行列表。此时,您可以停止在Javascript中执行循环并请求用户输入。数据是否来自数据库?如果我理解正确,您正在尝试实现某种“分页”函数