donglian1953 2015-08-31 13:21
浏览 26
已采纳

PHP在循环中设置一次值

I have a php script that checks every Sunday of the week and displays data on that day.

Users can change their value for that day to "Yes" or "No'.

I want to add a possibility that the users can't change their choice after every Thursday at 23:59 before that sunday.

I only want the first Sunday not to be editable. Since this all is in a loop that shows every Sunday until 11 Sundays in the future. I need to find a way to only do this for the first Sunday.

$datum_gisteren = new datetime('yesterday');
echo '<tr>';
echo '<td bgcolor="#3c741a" style="color:#FFFFFF;">&nbsp;'.$current_user->first_name.''.' '.''.$current_user->last_name.'</td>
<form action="" method="post">';
for ($i=0; $i<=11; $i++){
    $date_modified = $datum_gisteren->modify('next sunday')->format('d-m-y');
    $datum = get_the_author_meta( $date_modified, $current_user->ID );
    $time = '23:59'; 
    if(date('l') === 'Thursday' && date('H:i') >= $time && $date_modified === '06-09-15') {
        echo '<td>
        <select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
        '.(($datum == '1') ? '<option value="1" selected disabled="disabled">Nee</option>' : '').'
        '.(($datum == '2') ? '<option value="2" selected disabled="disabled">Ja</option>' : '').' 
        </select></td>';
    }else{
        echo '<td>
        <select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
        <option value="1" '.(($datum == '1') ? 'selected' : '').' >Nee</option>
        <option value="2" '.(($datum == '2') ? 'selected' : '').' >Ja</option>
        </select></td>';                        
    }
}   

Most of the work is done (I believe) but as you can see. The $date_modified in the first if-statement is "hardcoded" (06-09-15).

I want this value to be the first next sunday. But since this is in the loop it showed every Sunday for 11 times.

How can I set this Sunday once?

Hope it makes sense!!!

  • 写回答

1条回答 默认 最新

  • doutangguali32556 2015-08-31 14:08
    关注

    You can keep additional variable for it:

    $j = 0;
    
    for ($i=0; $i<=11; $i++){
        $date_modified = $datum_gisteren->modify('next sunday')->format('d-m-y');
        $datum = get_the_author_meta( $date_modified, $current_user->ID );
        $time = '23:59'; 
        if(date('l') === 'Thursday' && date('H:i') >= $time && $j === 0) {
            $j++;
            echo '<td>
            <select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
            '.(($datum == '1') ? '<option value="1" selected disabled="disabled">Nee</option>' : '').'
            '.(($datum == '2') ? '<option value="2" selected disabled="disabled">Ja</option>' : '').' 
            </select></td>';
        }else{
            echo '<td>
            <select class="form-field" id="'.$date_modified.'" name="'.$date_modified.'">
            <option value="1" '.(($datum == '1') ? 'selected' : '').' >Nee</option>
            <option value="2" '.(($datum == '2') ? 'selected' : '').' >Ja</option>
            </select></td>';                        
        }
    } 
    

    but will be good to segregate php logic from front.

    Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退