0

下面代码的目的是检查日期和时间 AM 或 PM 的 2 个参数。我的代码有效,但我正在努力提高效率并需要帮助。我有 2 个 if 语句,例如这个 if 语句 sun pm 和 sun am,然后我有 mon tues, wed, thur...... 所以这很长。有人告诉我有一种方法可以缩短它,请帮忙。

if ($day == 'Sun' && $AMorPM == 'pm')
{
echo"
<fieldset>
<Legend><b>CHECK-OUT</b></Legend>
Date out:<input name='DateOut' type='text' value=$dateout disabled='disabled'><br>
Time out:<input name='TimeOut' type='text' value=$timeout disabled='disabled'> 
<input type='radio' name='AM'disabled='disabled'>AM 
<input type='radio' name='PM' checked disabled='disabled'>PM<br>
Day of week: <input type='radio' name='S'checked disabled='disabled'>S 
<input type='radio' name='Mon' disabled='disabled'>M 
<input type='radio' name='Tue'disabled='disabled'>T 
<input type='radio' name='Wed'disabled='disabled'>W 
<input type='radio' name='Thu'disabled='disabled'>R 
<input type='radio' name='Fri'disabled='disabled'>F 
<input type='radio' name='Sat'disabled='disabled'>S
</fieldset>";
}
4

2 回答 2

0

您可以将其设置为函数-

//create array of days/short day codes
$day_array = array('Sun'=>'S', 'Mon'=>'M', 'Tue'=>'T', 'Wed'=>'W', 'Thu'=>'R', 'Fri'=>'F', 'Sat'=>'S');

//creates clock function
function clock($today, $ampm, $date, $time, $dayarray){

if ($ampm == 'am') {$punch_type =  "CHECK-IN";
                   $inout = 'in'; 
                   $inout_up = ucfirst($inout);
                   $checked_am = 'checked = "checked"';
} 
else               {$punch_type = "CHECK-OUT";
                   $inout = 'out'; 
                   $inout_up = ucfirst($inout);
                   $checked_pm = 'checked = "checked"';
}
//sets day of the week array vars, based of today
$dayof_week = $dayarray[$today];

echo"
<fieldset>
<Legend><b>$punch_type</b></Legend>
Date $inout:<input name='Date$inout_up' type='text' value=$date disabled='disabled'><br>
Time $inout:<input name='Time$inout_up' type='text' value=$time disabled='disabled'> 
<input type='radio' name='AM' $checked_am disabled='disabled'>AM <input type='radio' name='PM' $checked_pm disabled='disabled'>PM<br>
Day of week: ";

// loop through day array to create radios for every day of the week
foreach($dayarray as $key=>$value){
    // sets which day is checked
    $chkd = ($today == $key) ? ' checked = "checked"' : ''; 
    // creates each radio
    echo "<input type='radio' name='$key' $chkd disabled='disabled'>$value";}

echo"</fieldset>";
}

然后你可以通过执行调用该函数 -

clock("Sun", "am", "8/28/12", "10:30", $day_array);

这样,您就不需要每天重写脚本。如果您只是在函数中动态设置 4 个变量,它将动态构建/显示您的时钟,即。

clock($day, $AMorPM, $date, $time, $day_array);
于 2012-08-29T04:31:02.430 回答
0

无论一周中的哪一天,你对上午和下午做同样的事情吗?无论一天中的什么时间,您是否对一周中的每一天都做同样的事情?如果是这样,那您为什么不先处理 AM/PM 位,然后分别处理星期几?

echo "<fieldset>
    <Legend><b>CHECK-OUT</b></Legend>
    Date out:<input name='DateOut' type='text' value=$dateout disabled='disabled'><br>
    Time out:<input name='TimeOut' type='text' value=$timeout disabled='disabled'> 
    <input type='radio' name='AM'";

if ($AMorPM == "am")
   echo " checked ";

echo "disabled='disabled'>AM <input type='radio' name='PM'";

if ($AMorPM == "pm")
    echo " checked ";

echo " disabled='disabled'>PM<br>";

无论选择一周中的哪一天,该代码都会在一天中的某个时间执行您想要的操作。现在对一周中的每一天都做同样的事情。

于 2012-08-29T03:39:35.373 回答