Для проекта я пытаюсь сделать что-то на 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();
};
};
Вы используете переменную $ i в обоих циклах. Это перезаписывает $ i в первом цикле.
for ($i = 0; $i < $yourVar1; i++){
//some code
for ($a =0; $a < $yourVar2; a++){
//some code
}
//some code
}
Я просто изменил $ 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();
};
};
Перезаписывающая переменная $i
во внутреннем цикле. Используйте другую переменную там.
Для этого ответа я использовал $z
,