7

I have the following for-loop. It uses the values 0-6 form monday-sunday respectively.

Is there a more condensed way to do this? As opposed to listing out the if ($i=="day")

// $i = 0 is monday... $i = 6 is Sunday
for($i=0;$i<7;$i++){

    if ($i==0)
        echo ' <input name="repeat_on_week[]" type="checkbox" value="0"  /> Monday';
    if ($i==1)
        echo ' <input name="repeat_on_week[]" type="checkbox" value="1" /> Tuesday';
    if ($i==2)
        echo ' <input name="repeat_on_week[]" type="checkbox" value="2" /> Wednesday';
    if ($i==3)
       echo ' <input name="repeat_on_week[]" type="checkbox" value="3" /> Thursday';
    if ($i==4)
       echo ' <input name="repeat_on_week[]" type="checkbox" value="4" /> Friday';
    if ($i==5)
        echo ' <input name="repeat_on_week[]" type="checkbox" value="5" /> Saturday';
    if ($i==6)
        echo ' <input name="repeat_on_week[]" type="checkbox" value="6" /> Sunday';

}
Jeff
  • 21,744
  • 6
  • 51
  • 55
kylex
  • 14,178
  • 33
  • 114
  • 175
  • Though not applicable to this exact question, I would like to gently inject an expansion to the above code; after the first 'if', the rest could(should) be 'else if' (or 'elif', I'm not a php afficianado) – KevinDTimm Jun 10 '10 at 18:44

7 Answers7

16

How about:

$days = array('Monday', 
              'Tuesday', 
              'Wednesday', 
              'Thursday', 
              'Friday', 
              'Saturday', 
              'Sunday'
        );

for($i = 0; $i < 7; $i++) {
   echo '<input name = "repeat_on_week[]" type = "checkbox" value = "' . $i . '" />' . $days[$i];
}

Or use a foreach; it's easier on the eyes and you don't have to figure out the length of the array:

for($days as $i => $day) {
   echo "<input name = \"repeat_on_week[]\" type = \"checkbox\" value = \"$i\" /> $day";
}

It's a good sign that you thought "there has to be a better way to do this!". It means that you're moving in the right direction*. But I would also suggest brushing up on the concepts of arrays and when it is good to use them.

*A good programmer always thinks his or her code sucks, which is another way of saying that a good programmer is always trying to improve himself or herself, which is also another way of saying that a good programmer is humble.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
4
$days = array(
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday",
    "Sunday");

for($i=0; $i<7; $i++){
    echo ' <input name="repeat_on_week[]" type="checkbox" value="' . $i . '"  /> ' . $days[$i];
}
Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
3

First way:

$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");

for ($i=0; $i < 7; $i++) {
    echo ' <input name="repeat_on_week[]" type="checkbox" value="'.$i.'" /> '.$days[$i];
}

The second way would be to use the "date" function to get the weekday names from system, but I'm too lazy to figure out that calculation... you'd give it timestamps matching known weekdays, and then do

date("l", $timestamp);
Jaanus
  • 17,688
  • 15
  • 65
  • 110
1

Well... 4 years later, but still I decided to share a solution as well :) Please, see bellow.

for ($i = 0; $i < 7; $i++) {
    echo '<input name = "repeat_on_week[]" type = "checkbox" value = "' . $i . '" />' . date('l', strtotime('Monday +' . $i . 'days'));
}

Hope it helps someone.

Vlad
  • 513
  • 5
  • 13
1

Ah, everone got this quickly, but I would prefer a foreach loop:

$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
foreach ($days as $i => $day) {
       echo " <input name=\"repeat_on_week[]\" type=\"checkbox\" value=\"$i\" /> $day";
}
Nathan
  • 3,842
  • 1
  • 26
  • 31
0

Either use a switch statement or an array with the strings directly.

Eiko
  • 25,601
  • 15
  • 56
  • 71
0

Your code is merely going to print out each line once, in order. It doesn't seem to me that you need a loop at all.

Pops
  • 30,199
  • 37
  • 136
  • 151
  • Well, there's other code in that loop that requires it (for comparison purposes, i left it out to keep the code easier to read). – kylex Jun 10 '10 at 18:15
  • @kylex, ah, very well then. Any of the answers using an array work, then. – Pops Jun 10 '10 at 18:23