我正在寻找一些帮助来自动化这个过程。
这是我的整页代码。 http://www.privatepaste.com/65bbd73d3b 抱歉,我使用了 privatepaste,但代码太多,我无法通过在每行前面放置 4 行来很好地格式化(如果有快速的方法来做到这一点,请让我知道我的下一个 SO 问题!)
基本上,如果你看第 53 行,我有这个。
$checkdonors = mysql_query("SELECT userid FROM user WHERE membergroupids LIKE '%33'");
我正在努力让它检查用户 ID 的 membergroupids 字段是否包含 33,它检查正常。但是,我想将它添加到 if 条件中,如第 77 行所示。
<td <?php if ($tuserid==$checkdonors) { ?> style="background-color:#D0F2CD;" <?php } elseif ($tids=='1' AND $toonid==$tid) { ?> style="background-color:#FFD2F9; <?php } ?>">
因为,在下一个td,我有..
<td <?php if ($tuserid=='602' || $tuserid=='232' || $tuserid=='200' || $tuserid=='563' || $tuserid=='532') { ?>
这 5 个用户 ID (602,232,200,563,532) 是我在 membergroupids 33 中的用户,但我必须手动完成。
我想使该过程自动化,这样我就不必在每次将成员添加到 membergroupids '33' 时都添加新的 $tuserid=='userid'。
我该怎么做?
非常感谢。
最佳答案
如果我没理解错的话,你可以这样做:
// You could gather all of those ids first
$donors = array();
$checkdonors = mysql_query("SELECT userid FROM user WHERE membergroupids LIKE '%33'");
while($r = mysql_fetch_assoc($checkdonors)) {
$donors[] = $r['userid'];
}
所以当你在另一个循环中时。你可以使用 in_array()
:
if(in_array($tuserid, $donors)) {
// if that current id in the current loop is one of those donors
// do something
}
旁注:如果可能,切换到更好的 mysql 扩展,即 mysqli 或改用 PDO。
关于php - 如何在 if 条件下使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26226869/