views:

144

answers:

6

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';

}
A: 

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

Eiko
What's an "arty"?
Nathan
Sorry, array... Auto correction fooled me :-)
Eiko
A: 

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.

Lord Torgamus
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
Wow that is some sweet code.
Nathan
@kylex, ah, very well then. Any of the answers using an array work, then.
Lord Torgamus
+15  A: 

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
+1 for your comment at the end. Well said.
Cirrostratus
+3  A: 

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
+4  A: 
$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
+1  A: 

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
Is there a technical reason to use foreach over for? (is it faster?)
kylex
It's just syntactic sugar and it's simpler.
Vivin Paliath
Yeah, I don't think there's any performance issue. Don't worry so much about "faster", worry about "easier to read after 6 months away from it".
Nathan