Interactive
Interactive

Reputation: 1550

PHP Set a value once in a loop

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!!!

Upvotes: 0

Views: 214

Answers (1)

Taron Saribekyan
Taron Saribekyan

Reputation: 1390

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.

Upvotes: 1

Related Questions