duanchensou8685 2015-08-21 15:47
浏览 145
已采纳

PHP循环(每周一天和每小时)[关闭]

I want to create a loop (the result will be inserted into javascript)

Syntax:

  • The first number is the day (Monday - Friday/1 - 5)
  • The second number is the hour (1 - 11)

So this is finally what the loop should return:

subject_1_1: $("#subject_1_1").val(),
subject_2_1: $("#subject_2_1").val(),
subject_3_1: $("#subject_3_1").val(),
subject_4_1: $("#subject_4_1").val(),
subject_5_1: $("#subject_5_1").val(),
subject_1_2: $("#subject_1_2").val(),
subject_2_2: $("#subject_2_2").val(),
subject_3_2: $("#subject_3_2").val(),
subject_4_2: $("#subject_4_2").val(),
subject_5_2: $("#subject_5_2").val(),
subject_1_3: $("#subject_1_3").val(),

until

subject_5_11: $("#subject_5_11").val()

The last element shouldn't have a comma.

  • 写回答

2条回答 默认 最新

  • dongqiang2024 2015-08-21 15:52
    关注
    // first sets x = 1, then, while x isn't equal to 5 the code
    // inside `{ ... }` is executed and x is added 1
    for($x=1;$x<=5;$x++){
        // again, but this time with y, from 1 to 11.
        for($y=1;$y<=11;$y++){
            // So we end up here and we now this code is going to execute
            // elevent times for each x value, and a total of 5 x values.
            // thats (x = from 1 to 5) * (y = from 1 to 11).
            // This string is printed (`echo`) every time for each iteration.
            echo "subject_1_3: $(\"#subject_".$x."_".$y."\").val()".(!($x==5&&$y==11)?",":"")."
    ";
        }
    }
    

    Edited 1: now removes last comma.

    Edited 2: added comments, but read:

    In the string being printed, I notice this code:

    !($x==5&&$y==11)?",":""
    

    That does the following:

    if this is true ? this is executed : and if it is not, then this is executed
    

    So, in pseudo:

    (if not (x=5 and y = 11)) then (print comma) else (don't)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?