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