doudong7256 2017-08-15 22:42
浏览 51
已采纳

PHP for循环在第一次迭代后停止

For a project I am trying to do something in PHP. It requires a for loop and houses one as well. The for loop inside the initial loop functions as expected. It loops through the variables and stops once $i is equal to count($tasks)

However, when I copy paste the exact same loop on top of it, now working with $tasklists the loop stops after just one iteration. Mind you that when I tested count($tasklists) does return for example 3 and in the loop when I echo $i it does echo 0 the first time.

Here is the code with a bunch of comments.

// 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();
    };
};
  • 写回答

2条回答 默认 最新

  • drh78568 2017-08-15 22:56
    关注

    You are using the $i variable in both for loops. This overwrites the $i in the first loop.

    for ($i = 0; $i < $yourVar1; i++){
    //some code    
        for ($a =0; $a < $yourVar2; a++){
        //some code
        }
    //some code
    }
    

    I just changed the $i in the inner loop to a $a so that it won't override it anymore. Hope that helps you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。