duanchensou8685 2015-08-21 23:47
浏览 144
已采纳

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 23: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条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗