对于一个项目,我试图用PHP做一些事情。它需要for
循环并且还包含一个循环。初始循环内的for
循环按预期运行。它遍历变量并在$i
等于count($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();
};
};
答案 0 :(得分:4)
您在两个for循环中使用$ i变量。这会在第一个循环中覆盖$ i。
for ($i = 0; $i < $yourVar1; i++){
//some code
for ($a =0; $a < $yourVar2; a++){
//some code
}
//some code
}
我刚刚将内循环中的$ i更改为$ a,以便它不会再覆盖它。 希望能帮到你
答案 1 :(得分:1)
// 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
。在那里使用不同的变量。
对于这个答案,我使用了$z
。