简体   繁体   English

PHP While循环设置计数ID

[英]PHP While Loop setting count ID

I'm a little stuck with a piece of PHP I am working on, I need to set the href of a link to the same ID for the DIV below it, but its not setting them the same. 我对正在使用的PHP有点迷恋,我需要为链接下面的DIV设置指向相同ID的链接的href,但不能将它们设置为相同。

$x=1; 
     while ( $loop->have_posts() ) : $loop->the_post(); 
     if($color!='')
        {
            $out.='<'.$heading.' class="heading_accordion newHeadingColor">'.get_the_title().'</'.$heading.'>';
        }
     else
     {
                $out.='<dd class="accordion-navigation"><a href="#panel'.$x++.'">'.get_the_title().'</a>';
     }
                $out.= '<div id="panel'.$x++.'" class="content">'.get_the_content().'</div></dd>';

                endwhile; // end of the loop.

its doing 它在做

<a href="#panel1">link</a>
<div id="panel2">content</div>
<a href="#panel3">link</a>
<div id="panel4">content</div>
<a href="#panel5">link</a>
<div id="panel6">content</div>

and need something like 并且需要类似的东西

<a href="#panel1">link</a>
<div id="panel1">content</div>
<a href="#panel2">link</a>
<div id="panel2">content</div>
<a href="#panel3">link</a>
<div id="panel3">content</div>

It's because you are incrementing $x twice : Once when you output the href, once when you output the id. 这是因为您要增加$x两次:一次在输出href时,一次在输出id时。

You could just remove the incrementation on the first output : 您可以只删除第一个输出的增量:

$out.='<dd class="accordion-navigation"><a href="#panel'.$x.'">'.get_the_title().'</a>';

And keep it in the second one : 并将其保留在第二个中:

$out.= '<div id="panel'.$x++.'" class="content">'.get_the_content().'</div></dd>';

try this 尝试这个

while ( $loop->have_posts() ) : $loop->the_post(); 
$x++;
     if($color!='')
        {
            $out.='<'.$heading.' class="heading_accordion newHeadingColor">'.get_the_title().'</'.$heading.'>';
        }
     else
     {
                $out.='<dd class="accordion-navigation"><a href="#panel'.$x.'">'.get_the_title().'</a>';
     }
                $out.= '<div id="panel'.$x.'" class="content">'.get_the_content().'</div></dd>';

            endwhile; // end of the loop.

$x++ $ X ++

is equal to 等于

$x = $x + 1 $ x = $ x + 1

so you increment this 2 times in 1 iteration. 因此您可以在1次迭代中将其递增2次。 Try to remove increment from one of this two segments. 尝试从这两个段之一中删除增量。

<?php
$x=1; 
while ( $loop->have_posts() ) : $loop->the_post();

if($color!='') {
    $out.='<'.$heading.' class="heading_accordion newHeadingColor">'.get_the_title().'</'.$heading.'>';
} else {
    $out.='<dd class="accordion-navigation"><a href="#panel'.$x.'">'.get_the_title().'</a>';
}
$out.= '<div id="panel'.$x.'" class="content">'.get_the_content().'</div></dd>';

$x++;
endwhile; // end of the loop.
?>

You are doing the $x++ to early. 您正在尽早使用$ x ++。

Just use $x to output the id and do $x++ after the if-else statement 只需使用$ x输出id并在if-else语句之后执行$ x ++

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM