PHP for循环在第一次迭代后停止
对于一个项目,我正在尝试用PHP做一些事情。它需要一个PHP for循环在第一次迭代后停止,php,loops,Php,Loops,对于一个项目,我正在尝试用PHP做一些事情。它需要一个for循环,并包含一个循环。初始循环中的for循环按预期运行。它通过变量循环并停止一次$i等于计数($tasks) 但是,当我复制并粘贴完全相同的循环时,现在使用$tasklists只需一次迭代,循环就会停止。请注意,当我测试count($tasklists)时,它会返回示例3,当我回显$I时,它会在循环中第一次回显0 下面是带有一堆注释的代码 // First let's break up all the tasklists the pro
for
循环,并包含一个循环。初始循环中的for
循环按预期运行。它通过变量循环并停止一次$i
等于计数($tasks)
但是,当我复制并粘贴完全相同的循环时,现在使用$tasklists
只需一次迭代,循环就会停止。请注意,当我测试count($tasklists)
时,它会返回示例3,当我回显$I
时,它会在循环中第一次回显0
下面是带有一堆注释的代码
// First let's break up all the tasklists the project has
$tasklists = explode('&', $data['proj_tasklists']);
// Now lets get all the tasks for each tasklist
for($i = 0; $i < count($tasklists); $i++) {
// Get list_tasks from the tasklist
$sql_get = "SELECT * FROM tasklists WHERE list_id='".$tasklists[$i]."'";
$result_get = mysqli_query($con, $sql_get);
if($result_get) {
// Now load in the variable
while($results = mysqli_fetch_array($result_get)) {
$data['list_tasks'] = $results['list_tasks'];
};
// Now let's break that up
$tasks = explode('&',$data['list_tasks']);
// Now reset list_tasks
$data['list_tasks'] = '';
// Do something for every task
for($i = 0; $i < count($tasks); $i++) {
// And get the info for the set task
$sql_get = "SELECT * FROM tasks WHERE task_id='".$tasks[$i]."'";
$result_get = mysqli_query($con, $sql_get);
if($result_get) {
// Now load it's task_user in a variable
while($results = mysqli_fetch_array($result_get)) {
$data['task_user'] = $results['task_user'];
};
// Check if that is the same as that of the user whom was deleted
if($data['task_user'] == $data['user_id']) {
// If the Id is the same update it to ''
$sql_update = "UPDATE tasks SET task_user='' WHERE task_id='".$tasks[$i]."'";
if (mysqli_query($con, $sql_update)) {
// If that worked then add this to the list of addjusted IDs
// First check if the variable is empty or not
if($data['adjusted'] == '') {
// Add the ID plainly
$data['adjusted'] = $tasks[$i];
} else {
// Otherwise preceed the ID with an &
$data['adjusted'] = $data['adjusted'].'&'.$tasks[$i];
};
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>3
));
// Exit the php before it returns an succes state
exit();
};
};
// Now reset task_user
$data['task_user'] = '';
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>2
));
// Exit the php before it returns an succes state
exit();
};
};
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>1
));
// Exit the php before it returns an succes state
exit();
};
};
//首先,让我们分解项目中的所有任务列表
$tasklists=分解('&',$data['proj_tasklists']);
//现在让我们获取每个任务列表的所有任务
对于($i=0;$i“未成功”,
'其中'=>3
));
//在返回成功状态之前退出php
退出();
};
};
//现在重置任务\用户
$data['task_user']='';
}否则{
//返回错误
echo json_编码(数组(
“状态”=>“未成功”,
'其中'=>2
));
//在返回成功状态之前退出php
退出();
};
};
}否则{
//返回错误
echo json_编码(数组(
“状态”=>“未成功”,
'其中'=>1
));
//在返回成功状态之前退出php
退出();
};
};
在两个for循环中都使用$i变量。这将覆盖第一个循环中的$i
for ($i = 0; $i < $yourVar1; i++){
//some code
for ($a =0; $a < $yourVar2; a++){
//some code
}
//some code
}
for($i=0;$i<$yourVar1;i++){
//一些代码
对于($a=0;$a<$yourVar2;a++){
//一些代码
}
//一些代码
}
我只是将内部循环中的$I更改为$a,这样它就不会再覆盖它了。
希望这对您有所帮助您在这两个for循环中都使用了$i变量。这将覆盖第一个循环中的$i
for ($i = 0; $i < $yourVar1; i++){
//some code
for ($a =0; $a < $yourVar2; a++){
//some code
}
//some code
}
// First let's break up all the tasklists the project has
$tasklists = explode('&', $data['proj_tasklists']);
// Now lets get all the tasks for each tasklist
for($i = 0; $i < count($tasklists); $i++) {
// Get list_tasks from the tasklist
$sql_get = "SELECT * FROM tasklists WHERE list_id='".$tasklists[$i]."'";
$result_get = mysqli_query($con, $sql_get);
if($result_get) {
// Now load in the variable
while($results = mysqli_fetch_array($result_get)) {
$data['list_tasks'] = $results['list_tasks'];
};
// Now let's break that up
$tasks = explode('&',$data['list_tasks']);
// Now reset list_tasks
$data['list_tasks'] = '';
// Do something for every task
for($z = 0; $z < count($tasks); $z++) {
// And get the info for the set task
$sql_get = "SELECT * FROM tasks WHERE task_id='".$tasks[$i]."'";
$result_get = mysqli_query($con, $sql_get);
if($result_get) {
// Now load it's task_user in a variable
while($results = mysqli_fetch_array($result_get)) {
$data['task_user'] = $results['task_user'];
};
// Check if that is the same as that of the user whom was deleted
if($data['task_user'] == $data['user_id']) {
// If the Id is the same update it to ''
$sql_update = "UPDATE tasks SET task_user='' WHERE task_id='".$tasks[$i]."'";
if (mysqli_query($con, $sql_update)) {
// If that worked then add this to the list of addjusted IDs
// First check if the variable is empty or not
if($data['adjusted'] == '') {
// Add the ID plainly
$data['adjusted'] = $tasks[$i];
} else {
// Otherwise preceed the ID with an &
$data['adjusted'] = $data['adjusted'].'&'.$tasks[$i];
};
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>3
));
// Exit the php before it returns an succes state
exit();
};
};
// Now reset task_user
$data['task_user'] = '';
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>2
));
// Exit the php before it returns an succes state
exit();
};
};
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>1
));
// Exit the php before it returns an succes state
exit();
};
};
for($i=0;$i<$yourVar1;i++){
//一些代码
对于($a=0;$a<$yourVar2;a++){
//一些代码
}
//一些代码
}
我只是将内部循环中的$I更改为$a,这样它就不会再覆盖它了。
希望这对您有所帮助//首先,让我们分解项目中的所有任务列表
// First let's break up all the tasklists the project has
$tasklists = explode('&', $data['proj_tasklists']);
// Now lets get all the tasks for each tasklist
for($i = 0; $i < count($tasklists); $i++) {
// Get list_tasks from the tasklist
$sql_get = "SELECT * FROM tasklists WHERE list_id='".$tasklists[$i]."'";
$result_get = mysqli_query($con, $sql_get);
if($result_get) {
// Now load in the variable
while($results = mysqli_fetch_array($result_get)) {
$data['list_tasks'] = $results['list_tasks'];
};
// Now let's break that up
$tasks = explode('&',$data['list_tasks']);
// Now reset list_tasks
$data['list_tasks'] = '';
// Do something for every task
for($z = 0; $z < count($tasks); $z++) {
// And get the info for the set task
$sql_get = "SELECT * FROM tasks WHERE task_id='".$tasks[$i]."'";
$result_get = mysqli_query($con, $sql_get);
if($result_get) {
// Now load it's task_user in a variable
while($results = mysqli_fetch_array($result_get)) {
$data['task_user'] = $results['task_user'];
};
// Check if that is the same as that of the user whom was deleted
if($data['task_user'] == $data['user_id']) {
// If the Id is the same update it to ''
$sql_update = "UPDATE tasks SET task_user='' WHERE task_id='".$tasks[$i]."'";
if (mysqli_query($con, $sql_update)) {
// If that worked then add this to the list of addjusted IDs
// First check if the variable is empty or not
if($data['adjusted'] == '') {
// Add the ID plainly
$data['adjusted'] = $tasks[$i];
} else {
// Otherwise preceed the ID with an &
$data['adjusted'] = $data['adjusted'].'&'.$tasks[$i];
};
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>3
));
// Exit the php before it returns an succes state
exit();
};
};
// Now reset task_user
$data['task_user'] = '';
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>2
));
// Exit the php before it returns an succes state
exit();
};
};
} else {
// Return an error
echo json_encode(array(
'status'=>'unsuccesful',
'where'=>1
));
// Exit the php before it returns an succes state
exit();
};
};
$tasklists=分解('&',$data['proj_tasklists']);
//现在让我们获取每个任务列表的所有任务
对于($i=0;$i