0

我为开始时间选择和结束时间创建了一个下拉列表。我有 3 个数组,time_group、time_name 和 dotw。最终结果如下所示:

星期一 [dropdown] - [dropdown]
星期二 [dropdown] - [dropdown]
星期三 [dropdown] - [dropdown] 等等

我正在尝试使用实际页面上的预设变量检查 sql 行。

$char_avail_start_monday =  $row['char_avail_start_monday'];
$char_avail_stop_monday =  $row['char_avail_stop_monday'];

现在在下面的代码中,我每天循环并使用正确的值创建两个下拉列表中的每一个。但是我有一个 IF 语句,如果 db 单元格为空,请执行 X,如果 = 到 Y 执行某些操作等。看来我的编码工作不正常。我创建了 ${char_avail_start_}.$dotw[$d] (例如:$char_avail_start_monday),它没有指向我想要查看的预定义行。

    //this is using my previous variable
    echo "row has ".$char_avail_start_monday ." ";
    //this is using my attempt at generating it and seeing it it can pull from the row
    echo "row has " .${char_avail_start_}.$dotw[$d] ." <br/>";

我得到的输出是:

“行有 2000 行有星期一”

这似乎只是 $dotw[$d] 部分。我想做的事是不可能的吗?还是我只是做错了?

完整代码是:

<?php

//CREATE START TIMES ARRAY
$time_group = array('0000', '0030', '0100', '0130', '0200', '0230', '0300', '0330', '0400', '0430', '0500', '0530', '0600', '0630', '0700'
  , '0730', '0800', '0830', '0900', '0930', '1000', '1030', '1100', '1130', '1200', '1230', '1300', '1330', '1400', '1430', '1500', '1530'
  , '1600', '1630', '1700', '1730', '1800', '1830', '1900', '1930', '2000', '2030', '2100', '2130', '2200', '2230', '2300', '2330');

//CREATE FORMATED TIME ARRAY
$time_name = array('Midnight', '12:30 AM', '01:00 AM', '01:30 AM', '02:00 AM', '02:30 AM', '03:00 AM', '03:30 AM', '04:00 AM', '04:30 AM', '05:00 AM', '05:30 AM', '06:00 AM', '06:30 AM', '07:00 AM'
  , '07:30 AM', '08:00 AM', '08:30 AM', '09:00 AM', '09:30 AM', '10:00 AM', '10:30 AM', '11:00 AM', '11:30 AM', 'Noon', '12:30 PM', '01:00 PM', '01:30 PM', '02:00 PM', '02:30 PM', '03:00 PM', '03:30 PM'
  , '04:00 PM', '04:30 PM', '05:00 PM', '05:30 PM', '06:00 PM', '06:30 PM', '07:00 PM', '07:30 PM', '08:00 PM', '08:30 PM', '09:00 PM', '09:30 PM', '10:00 PM', '10:30 PM', '11:00 PM', '11:30 PM');

$dotw = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday');
?>

<table width="425" border="0">

<?      
//Loop through the days of the week *DOTW* array
$d = 0;
while ($d <= 6)  {
?>
    <tr>
      <?php
      //echo "test: " .${char_avail_start_}.$d;
          if (${char_avail_start_}.$d == ""){ 
              echo "<td width=\"190\" scope=\"col\"><span style=\"color: red\"><b>".ucfirst($dotw[$d])."</b></span></td>";
            } else {
              echo "<td width=\"190\" scope=\"col\">".ucfirst($dotw[$d])."</td>";
            }
      ?>

<td scope="col">
<?php

//here is my test code to see if its defined
if (${char_avail_start_}.$dotw[$d] == "2000"){ 
    echo "2000 FOUND\n";
  } else {

    //this is using my previous variable
    echo "row has ".$char_avail_start_monday ." ";
    //this is using my attempt at generating it and seeing it it can pull from the row
    echo "row has " .${char_avail_start_}.$dotw[$d] ." <br/>";
}

echo "<select name=\"char_avail_start_".$dotw[$d]."\" id=\"char_avail_start_".$dotw[$d]."\">\n";

//FIRST BLANK OPTION
if (${char_avail_start_}.$d == ""){
    echo "<option value=\"\" selected=\"selected\"></option>\n";
  } else {
    echo "<option value=\"\"></option>\n";
}

$i = 0;
while ($i <= 47)  {
    echo "<option value=\"".$time_group[$i]."\">".$time_name[$i]."</option>\n";
  $i++;
}
echo "</select>\n";

echo "-\n";

echo "<select name=\"char_avail_stop_".$dotw[$d]."\" id=\"char_avail_stop_".$dotw[$d]."\">\n";


//FIRST BLANK OPTION
if (${char_avail_stop_}.$d == ""){
    echo "<option value=\"\" selected=\"selected\"></option>\n";
  } else {
    echo "<option value=\"\"></option>\n";
}
$i = 0;
while ($i <= 47)  {
    echo "<option value=\"".$time_group[$i]."\">".$time_name[$i]."</option>\n";
  $i++;
}
echo "</select></td></tr>\n";

$d++;
}
?>
</table>
4

2 回答 2

0

如果我理解正确,最好创建一个包含可用值的天数组:

$char_avail_start = array();
$char_avail_start['monday'] = $row['char_avail_start_monday'];
$char_avail_start['tuesday'] = $row['char_avail_start_tuesday];
...

然后,您可以使用以下方式动态访问变量:

echo "row has " . $char_avail_start[$dotw[$d]] . "<br/>";
于 2013-04-03T13:31:46.530 回答
0

所以我做错了。更搞砸了,发现了一个错误。
if (${char_avail_start_}.$dotw[$d] == ""){ blah }

需要...
if (${char_avail_start_.$dotw[$d]} == ""){ blah }

我的尾随 } 在错误的位置。

然后它可以引用我之前定义的变量。
$char_avail_start['monday'] = $row['char_avail_start_monday'];
etc...

并毫无问题地从 sql server 拉回数据。

于 2013-04-03T17:59:49.977 回答