如何使用foreach为每个用户名进行循环执行许多请求mysql以下

时间:2019-06-20 08:22:04

标签: php mysql

下面附上我的php代码。我想为整个过程做一个循环。就像一个foreach。 foreach适用于我以$username = $_POST['username'], //['user1,user2,user3']

之类的数组发布的每个用户

在下面的代码中完成所有过程

第一个变量:

此处的变量用于初始化日历中验证弹出窗口所要求的一切

    <?php
include('config.php');
header('Content-Type:text/json;charset=utf-8');
ini_set("error_reporting","E_ALL & ~E_NOTICE");

// $check receive value 1,2 or for
// 1 = reschedule (reschedule time / date / coach / place)
// 2 = delete (delete the class into contract)
// 3 = cancel

//Init Value when you get the value from the calendar
$init_element = $_POST['init_element'];
$init_date = $_POST['init_date'];
$init_starttime = $_POST['init_starttime'];
$init_endtime = $_POST['init_endtime'];
$init_coachname = $_POST['init_coachname'];
$init_place = $_POST['init_place'];
//Value to process the data after selected from popup

$element = $_POST['element']; // POST 合同,快课预定了,快课为预定
$check = $_POST['check']; // POST 1 for reschedule , 2 for delete , 3 for cancel
$id = $_POST['id']; // id of the contract, fastclass pay or fastclass
$contract_id = $_POST['contract_id']; // contract_id get from contract_buffer table for contract
$companyname = $_POST['companyname'];
$date = $_POST['date']; // the date to modify
$start_time = $_POST['start_time']; // the start time to modify
$end_time = $_POST['end_time']; // end time to modify
$time = $starttime.'-'.$endtime;
$place = $_POST['place']; // place to modify
$coachname = $_POST['coachname']; // coachname to modify
$username = $_POST['username']; // username to grab if need to change
$repeat = $_POST['repeat']; // POST 1 when repeat is ticking , 0 if it's empty
$returnValue = array();

过程

if (!empty($init_date)){
    $sql = "select * from contract_buffer where username = '$username' and coachname = '$init_coachname' and date = '$init_date' and start_time = '$init_starttime' and end_time = '$init_endtime'"; // get element times for substraction
    $res = mysqli_query($conn,$sql);
        while($row = mysqli_fetch_array($res)){
        $init_cid = $row['contract_id'];
        }

if ($element == 合同){
        if ($check == 1){
            if ($repeat == 1){
                if(!empty($init_cid)){
                    $sql2 = "UPDATE contract_buffer
                    SET  start_time = '$start_time' , end_time = '$end_time' ,  coachname = '$coachname' , place = '$place'
                    WHERE contract_id = '$init_cid' and date >= '$init_date'";
                    mysqli_query($conn, $sql2);
                    $returnValue['status'] = "200";
                    $returnValue["message"] = "Success! All your class have been rescheduled from the date you have mentioned";

                }else{
                    $returnValue['status'] = "400";
                    $returnValue["message"] = "Sorry a problem have been detected when you try repeat rescheduling. Error"." ".$init_cid;
                }

            }else{
                if(!empty($init_cid)){
                    $sql3 = "UPDATE contract_buffer
                    SET  date = '$date' , start_time = '$start_time' , end_time = '$end_time' ,  coachname = '$coachname' , place = '$place'
                    WHERE contract_id = '$init_cid' and date = '$init_date'";
                    mysqli_query($conn, $sql3);
                    $returnValue['status'] = "200";
                    $returnValue["message"] = "Success! The class for"." ".$username." ".$init_cid." "."have been rescheduled.";

                }else{
                    $returnValue['status'] = "400";
                    $returnValue["message"] = "username = ".$username." "."coachname = ".$init_coachname." "."date = ".$init_date." "."start time = ".$init_starttime." "."end time = ".$init_endtime;
                }
            }

        }elseif ($check == 2)  {
           if(!empty($init_cid)){
               $sql4 = "select * from contruct where str = '$init_cid'"; // take of one class to the times counts ok
               $res4 = mysqli_query($conn,$sql4);
               while($row = mysqli_fetch_array($res4)){
                   $times_up = $row["times"];
                   $times = $times_up - 1;
                   $condition = 1;
                // update number of time into contruct table
                   $sql5 = "UPDATE contruct
                   SET  times = '$times'
                   WHERE str = '$init_cid'";
                   mysqli_query($conn, $sql5);
               }

               if($condition == 1){
                   $sql6 = "DELETE FROM contract_buffer WHERE username = '$username' and coachname = '$init_coachname' and date = '$init_date' and start_time = '$init_starttime' and end_time = '$init_endtime'";
                   mysqli_query($conn, $sql6);
                   $returnValue['status'] = "200";
                   $returnValue['message'] = "Sucess your class have been deleted";
               }else{
                   $returnValue['status'] = "400";
                   $returnValue["message"] = "Sorry your class can be delete. Please check with administrator at ";
               }

           }else{
               $returnValue['status'] = "400";
               $returnValue["message"] = "Sorry server problem won't return value. Please check with administrator at ";
           }

        }else{
          if(!empty($init_cid)){
              $sql7 = "select * from contruct where str = '$init_cid'";
              $res7 = mysqli_query($conn,$sql7);
              while($row = mysqli_fetch_array($res7)){
                  $e_date = $row["end_date"];
                  $convert_date = strtotime($e_date);
                  $days = 7;
                  $final = ($days * 84600) + 84600;
                  $end_init = $convert_date + $final;
                  $end_date = gmdate("Y-m-d", $end_init);
                    $sql8 = "select MAX(date) as maxdate from contract_buffer where contract_id = '$init_cid'"; // get the max date to be store
                    $res8 = mysqli_query($conn,$sql8);
                    while($row = mysqli_fetch_array($res8)){
                        $mxdate = $row['maxdate'];
                        $mxdate_7 = date('Y-m-d', strtotime($mxdate. ' + 7 days')); 
                  }
              }

              if(!empty($init_cid)){
                  $sql9 = "UPDATE contract_buffer
                  SET  date = '$mxdate_7'
                  WHERE username = '$username' and coachname = '$init_coachname' and date = '$init_date' and start_time = '$init_starttime' and end_time = '$init_endtime'";
                  mysqli_query($conn, $sql9);
                  $returnValue['status'] = "200";
                  $returnValue["message"] = "Update 7 days have been worked.";
                  $control = 1;
              }else{
                  $returnValue['status'] = "400";
                  $returnValue["message"] = "Update of the max date doesn't work. Please check with administrator at ";
              }

              if($control == 1){
                  $sql10 = "UPDATE contruct
                  SET  end_date = '$end_date'
                  WHERE str = '$init_cid'";
                  mysqli_query($conn, $sql10);
                  $returnValue['status'] = "200";
                  $returnValue['message'] = "Class have been successfully rescheduled 7 days after the end date.";
                  $status = 0;
                  if(!empty($init_cid)){
                      $sql11 = "UPDATE contract_buffer
                      SET  status = '$status'
                      WHERE username = '$username' and coachname = '$init_coachname' and date = '$init_date' and start_time = '$init_starttime' and end_time = '$init_endtime'";
                      mysqli_query($conn, $sql11); // to check something here maybe shall more attention when tested
                      $returnValue['status'] = "200";
                      $returnValue["message"] = "Success! your class have been canceled";
                  }else{
                      $returnValue['status'] = "400";
                      $returnValue["message"] = "Your class can't be canceled. Please check with administrator at ";
                  }
              }else{
                  $returnValue['status'] = "400";
                  $returnValue["message"] = "haven't been canceled - Server problem. Please check with administrator at ";
              }

          }else{
              $returnValue['status'] = "400";
              $returnValue["message"] = "Sorry your class can be canceled. Please check with administrator at ";
          }
        }

}elseif ($element == 快课预定了) {
    if ($check == 1){
        if(!empty($id)){
            $sql12 = "UPDATE user_fast_class
            SET  date = '$date' , startime = '$start_time' , endtime = '$end_time' ,  coachname = '$coachname'
            WHERE id = '$id'";
            mysqli_query($conn, $sql12);
            $status = 200;


                if ($status = 200){
                    $sql20 = "select * from company_fast_class where coachname = '$init_coachname' and date = '$init_date' and start_time = '$init_starttime' and end_time = '$init_endtime' and place = '$init_place'";
                    $res20 = mysqli_query($conn,$sql20);
                    while($row = mysqli_fetch_array($res20)){
                        $f_price = $row['price'];
                        $f_type = $row['type'];
                        $f_numbers = $row['numbers'];
                        $f_publishtime = date("Y-m-d");
                    }
                    $sql21 = "insert into company_fast_class(company_name,coachname,date,start_time,end_time,price,type,publish_time,numbers,place)
                    values('$companyname','$coachname','$date','$start_time','$end_time','$f_price','$f_type','$f_publishtime','$f_numbers','$place')";
                    mysqli_query($conn, $sql21);


                    $sql22 = "select * from company_fast_class where coachname = '$coachname' and date = '$date' and start_time = '$start_time' and end_time = '$end_time' and place = '$place'";
                    $res22 = mysqli_query($conn,$sql22);
                    while($row = mysqli_fetch_array($res22)){
                        $f_id = $row['id'];
                    }

                    $sql23 = "UPDATE user_fast_class
                    SET  fastclass_id = '$f_id'
                    WHERE id = '$id'";
                    mysqli_query($conn, $sql23);
                    $returnValue['status'] = "200";
                }else{
                    $returnValue['status'] = "400";
                    $returnValue["message"] = "No value returned. Please check with administrator at ";
                }
            $returnValue['status'] = "200";
            $returnValue["message"] = "successfully operated"." ".$f_id;

        }else{
            $returnValue['status'] = "400";
            $returnValue["message"] = "Sorry the booking fastclass can't be rescheduled. Please check with administrator at ";
        }

    }elseif ($check == 2) {
        if(!empty($id)){
            $sql13 = "DELETE FROM user_fast_class WHERE id = '$id'";
            mysqli_query($conn, $sql13);
            $returnValue['status'] = "200";
            $returnValue["message"] = "The paid fastclass have been successfully deleted";
        }else{
            $returnValue['status'] = "400";
            $returnValue["message"] = "Sorry the booking fastclass can't be deleted. Please check with administrator at";
        }

    }else{
      if(!empty($id)){
          $sql9 = "DELETE FROM user_fast_class WHERE id = '$id'";
          mysqli_query($conn, $sql9);
          $returnValue['status'] = "200";
          $returnValue["message"] = "The paid fastclass have been successfully canceled";
      }else{
          $returnValue['status'] = "400";
          $returnValue["message"] = "Sorry the booking fastclass can't be canceled. Please check with administrator at ";
      }
    }

}else{
    if ($check == 1){
        if (!empty($id)){
            $sql14 = "UPDATE company_fast_class
            SET  date = '$date' , start_time = '$start_time' , end_time = '$end_time' ,  coachname = '$coachname' , place = '$place'
            WHERE id = '$id'";
            mysqli_query($conn, $sql14);
            $returnValue['status'] = "200";
            $returnValue["message"] = "Success your the fastclass have been rescheduled";
        }else{
            $returnValue['status'] = "400";
            $returnValue["message"] = "Sorry something wrong happened when rescheluded the fastclass. Please check with administrator at ";
        }

    }elseif ($check == 2) {
        if (!empty($id)){
        $sql15 = "DELETE FROM company_fast_class WHERE id = '$id'";
        mysqli_query($conn, $sql15);
            $returnValue['status'] = "200";
            $returnValue["message"] = "The fastclass have been successfully deleted";
        }else{
            $returnValue['status'] = "400";
            $returnValue["message"] = "Sorry something wrong happened when deleted the fastclass. Please check with administrator at ";
        }

    }else{
        if (!empty($id)){
            $sql16 = "DELETE FROM company_fast_class WHERE id = '$id'";
            mysqli_query($conn, $sql16);
            $returnValue['status'] = "200";
            $returnValue["message"] = "The fastclass have been successfully canceled";
        }else{
            $returnValue['status'] = "400";
            $returnValue["message"] = "Sorry something wrong happened when canceled the fastclass. Please check with administrator at";
        }
    }
}

}else{
    $returnValue['status'] = "400";
    $returnValue['message'] = "Please fill all the form";
}

echo json_encode($returnValue);
?>

我需要为整个用户名循环一个用户名。这将对我的用户有帮助。而不是一个选择一个用户。可以选择多个,然后加快速度

非常感谢您的帮助。

0 个答案:

没有答案