1

如何始终打印,但是数据库中的一个名称(值)与应打印的相同名称(值)附有文本,或者值加粗?对不起我的英语不好;D

这是值

$a = '9.00';
$b = '10.00';
$c = '11.00';
$d = '12.00';
$e = '13.00';
$f = '14.00';
$g = '15.00';
$h = '16.00';
$i = '17.00';
$j = '18.00';

$date = '5/21/2012';
$q = mysql_query("SELECT * FROM times WHERE date='$date'");
while ($row = mysql_fetch_assoc($q)){
$time = $row['time'];
....

}

怎么做?用数组?或 foreach... 帮助,请 ;D

结果将是:

9.00 - AVAIBLE->这个不在数据库 10.00 - NOT AVAIBLE中->这个在数据库中 11.00 - NOT AVAIBLE->这个在数据库中 12.00 - NOT AVAIBLE->这个在数据库中.....

4

2 回答 2

3

如果我理解,您想一直打印,$a..$j但如果它们出现在您的查询结果中,则将它们加粗或识别它们。

// Store all times in an array rather than variables...
$times = array('9.00','10.00','11.00','12.00','13.00','14.00','15.00','16.00','17.00','18.00');

// Array to hold times from the database query
$dbtimes = array();

date = '5/21/2012';
$q = mysql_query("SELECT * FROM times WHERE date='$date'");
while ($row = mysql_fetch_assoc($q)){

  // Place database times onto the array
  $dbtimes[] = $row['time'];
}

// Iterate over all the times and if the same time occurs in the databse
// array list, output it with special marks as needed.
foreach ($times as $t) {
  if (in_array($t, dbtimes)) {
     // Print bolded time $t
     echo '<strong>$t</strong>';
  }
  else {
     // Print regular time $t
     echo $t;
  }
}

请注意,这不是5/21/2012MySQL日期时间类型 ( is)的有效日期格式。希望您的列是正确的日期时间类型,而不是像. 如果不是,我建议将其更改为适当的日期时间。2012-05-12date5/21/2012

于 2012-04-20T13:45:02.687 回答
0
// store all the times in an array
$mytimes = array('9.00', '10.00', ..., '18.00');

$date = '5/21/2012';
$q = mysql_query("SELECT * FROM times WHERE date='$date'");

while($row = mysql_retch_assoc($q){
    $time = $row['time'];

    // loop through the array, if the time matches the value in the database, 
    // make it bold
    foreach($mytimes as $timecheck){
        if($time == $timecheck){
            // make bold
            echo '<b>'.$timecheck.'</b><br>';
        }
        else{
            echo $timecheck.'<br>';
        }
    }
}
于 2012-04-20T13:45:33.310 回答