Drupal 6 PHP:Foreach 不断在同一行上重复操作
我在 Drupal 6 中建立了一个网站,并尝试使用不同的类创建一些跨度以在我的 .tpl (html) 中打印。
在示例中,我在自己的行中列出了两个人。它们都有两个具有不同值的数组,称为“位置”和“首选位置”。
我的问题是,我已经创建了一个 foreach,它打印了跨度,但它每次都在同一行中重复跨度(其中只有两行)。我似乎无法在我的代码中找到错误。请看一下,任何帮助,或者指出正确的方向,我们将不胜感激。
foreach ($vars['view']->result as $key => $row) {
// preferred positions on pitch
$outputspanpref = "";
foreach ($row->node_data_field_pref_position_field_pref_position_value as $key =>
$position) {
$keyclass = 'pref-position-' . $key;
$positionclass = strtolower(str_replace(" ","-",$position['value']));
$outputspanpref .= '<span class="' . $keyclass . ' ' . "pref-position" .
'' . $positionclass . '"></span>';
$vars['prefposition'] = $outputspanpref;
}
// secondary positions on pitch
$outputspan = "";
foreach ($row->node_data_field_position_field_position_value as $key =>
$position) {
$keyclass = 'position-' . $key;
$positionclass = strtolower(str_replace(" ","-",$position['value']));
$outputspan .= '<span class="' . $keyclass . ' ' . $positionclass.'
</span>';
$vars['position'] = $outputspan;
}
}
}
I've don a site in Drupal 6, and am trying to make some spans with different classes to print in my .tpl (html).
In the example I have two people listed in their own row. They both have two arrays with different values called 'positions' and 'preferred positions'.
My problem is that I've made a foreach, and it prints the spans, but it keeps repeating the spans each time for the ame row (in this there is only two rows). I can't seem to find the error in my code. Please have a look, any help, or a finger pointed in the right direction would be appreciated.
foreach ($vars['view']->result as $key => $row) {
// preferred positions on pitch
$outputspanpref = "";
foreach ($row->node_data_field_pref_position_field_pref_position_value as $key =>
$position) {
$keyclass = 'pref-position-' . $key;
$positionclass = strtolower(str_replace(" ","-",$position['value']));
$outputspanpref .= '<span class="' . $keyclass . ' ' . "pref-position" .
'' . $positionclass . '"></span>';
$vars['prefposition'] = $outputspanpref;
}
// secondary positions on pitch
$outputspan = "";
foreach ($row->node_data_field_position_field_position_value as $key =>
$position) {
$keyclass = 'position-' . $key;
$positionclass = strtolower(str_replace(" ","-",$position['value']));
$outputspan .= '<span class="' . $keyclass . ' ' . $positionclass.'
</span>';
$vars['position'] = $outputspan;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正在嵌套的
foreach
循环中重复使用$key
。尝试在每个后续循环中使用$key1
,然后使用$key2
等。It looks like you're re-using
$key
within nestedforeach
loops. Try using$key1
, then$key2
, etc for each subsequent loop.根据你的问题描述。您很可能在循环中使用相同的引用,因此要纠正它,您必须将每一行分配给它自己的唯一变量。
Based on your problem description. It is likely that you are using the same reference in your loop and thus to rectify it ,you must assign each row to it's own unique variable.