我有一个用户id数组,我需要在视图中遍历a user: id
字段,为匹配和不匹配的结果添加一些特定的HTML。
<?php if (//Field content matches an array value): ?>
<span class="friend"><?php print $output; ?></span>
<?php endif; ?>
<?php if (//Field content doesn't match an array value): ?>
<span class="not-friend"><?php print $output; ?></span>
<?php endif; ?>
谁能帮我把空白填上?:) 假设$ output将只是一个表示uid的整数(而不是HTML标记),您可以这样做:
<span class="<?php if(!in_array($output, $your_array)): ?>not-<?php endif; ?>friend">
<?php print $output; ?>
</span>
参见PHP in_array()
但是,$output可能是HTML。如果是这种情况,您应该使用$row而不是$output。为了查看$row包含什么,我喜欢在模板文件中这样做:
<!-- <?php echo print_r($row,true); ?> -->
(然后在浏览器中查看源代码)
此外,我建议不要在模板文件中这样做,因为它将逻辑与主题联系在一起…看看Views的Customfield——它会让你在一个自定义字段中做PHP…如果将其放在UID下,并从显示中排除UID,则可以访问UID并执行上面在自定义字段中得到的代码,使用$data对象而不是$row或$output。