Drupal 6 PHP:Foreach 不断在同一行上重复操作

发布于 2024-11-19 23:35:04 字数 1349 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-11-26 23:35:04

看起来您正在嵌套的 foreach 循环中重复使用 $key 。尝试在每个后续循环中使用 $key1,然后使用 $key2 等。

foreach ($vars['view']->result as $key1 => $row) { // 1st forloop
foreach ($row->node_data_field_pref_position_field_pref_position_value as $key2 => $position) { // 2nd forloop
foreach ($row->node_data_field_position_field_position_value as $key3 => $position) { // 3rd forloop

It looks like you're re-using $key within nested foreach loops. Try using $key1, then $key2, etc for each subsequent loop.

foreach ($vars['view']->result as $key1 => $row) { // 1st forloop
foreach ($row->node_data_field_pref_position_field_pref_position_value as $key2 => $position) { // 2nd forloop
foreach ($row->node_data_field_position_field_position_value as $key3 => $position) { // 3rd forloop
季末如歌 2024-11-26 23:35:04

根据你的问题描述。您很可能在循环中使用相同的引用,因此要纠正它,您必须将每一行分配给它自己的唯一变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文