谁能解决我的问题?在drupal7视图中,我不知道为什么if条件没有得到满足(if($mycount == 1))在下面的代码中。
foreach ($fields as $id => $field){
if($field->class == "counter")
{
$mycount = $field->content;
echo $mycount;
echo "<br>";
}
if($field->class == "field-logo")
{
var_dump($mycount); // output
echo "<br>";
if($mycount == 1) // but here 1 == 1 for the first time. see the output.
{
echo "worked"; // its not coming here...
}
}
}
输出:1
string(36) "1"
2
string(36) "2"
3
string(36) "3"
4
string(36) "4"
5
string(36) "5"
谢谢。
我怀疑问题是转储字符串大小- 字符串(36) -这表明$mycount
内容不是一个字符,而是36(数字"1"和35个垃圾字符)。你应该试试这个:
$mycount = trim($field->content);
echo '<pre>as string: ' . var_export($mycount, true)
. ', is_numeric: ' . var_export(is_numeric($mycount), true)
. ', as integer: ' . var_export((int) $mycount, true) . '</pre><br />';
您应该在输出中看到这样的内容:
as string: '1', is_numeric: true, as integer: 1
我相信Views在一些HTML中包装了您的字段值,因此您的实际输出可能是<span class="field-content">1</span>
,但在浏览器中您只看到"1"。
为了避免在HTML中包装结果,您需要将"查看结果计数器"字段样式设置更改为"自定义字段HTML",并将"- None -"设置为下拉值。
现在$field->content
将返回值没有任何HTML和$mycount == 1
在你的if语句将工作良好。