php, mysql 如果$a 在数据库中

标签 php mysql sql arrays foreach

如何打印所有时间,但那个在数据库中的同名(值) 应该打印的是附有文字,还是值是粗体? 抱歉我的英语不好;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 - 不可用 -> 这个在数据库中 11.00 - 不可用 -> 这个在数据库中 12.00 - 不可用 -> 这个在数据库中 .....

最佳答案

如果我理解,您想一直打印 $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/2012 不是 MySQL 日期时间类型的有效日期格式(2012-05-12 是)。希望您的 date 列是正确的日期时间类型,而不是像 5/21/2012 这样的自定义字符串。如果不是,我建议将其更改为合适的日期时间。

关于php, mysql 如果$a 在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10247434/

相关文章:

php - facebook 连接 api 静态 url

php - 解决同一网页上两个简单表单之间的冲突

mysql - 是否可以将此业务逻辑放在 SQL 查询中?

c# - EF6 LINQ 嵌套包括

MySQL:我应该为这些查询设置什么索引?

php - 如何在选中父复选框时选择/取消选择循环中生成的所有复选框

php - 如何在 Bigcommerce 中通过 sku id 更新产品的 "inventory_level"?

MySQL 和使子查询更高效

MySQL - 带有 GROUP BY 的条件 COUNT

mysql - MySQL 中的 COUNT 和 GROUP BY 问题