根据X的数量打印一个数字

时间:2015-01-17 11:36:00

标签: php

下面,我希望$slideNumber由幻灯片数决定。我试图在“幻灯片”部分创建一个变量,并为每个新幻灯片增加1。然后我试图将其插入:

<input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">

但没有输出。它只输出slide的单词id。我该怎么做才能解决这个问题?

澄清一下,如果有五张幻灯片,那么应该有五张inputs打印:

<input type="radio" name="slider" id="slide1">
<input type="radio" name="slider" id="slide2">
<input type="radio" name="slider" id="slide3">
<input type="radio" name="slider" id="slide4">
<input type="radio" name="slider" id="slide5">

我该怎么做才能解决这个问题?

<!-- Slider Setup -->
<?php if( have_rows('slides') ):
    while ( have_rows('slides') ) : the_row();

?>

    <input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">

<?php endwhile;endif; ?>

<!-- Slide -->
<?php if( have_rows('slides') ): ?>

    <div id="slides">
        <div id="overflow">
            <div class="inner">

                <?php if( have_rows('slides') ): 

                $slideNumber = 1;
                while ( have_rows('slides') ) : the_row(); 

                    $slideNumber++;
                    $slideImage = get_sub_field('slide_image');

                ?>

                    <article>
                        <img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>">
                    </article>

                <?php endwhile;endif; ?>

            </div><!-- #inner -->
        </div><!-- #overflow -->
    </div><!-- #slides -->

<?php endif; ?>

1 个答案:

答案 0 :(得分:1)

您似乎在尝试使用$slideNumber变量时,它甚至没有定义,在这里:

<!-- Slider Setup -->
<?php if( have_rows('slides') ):
    while ( have_rows('slides') ) : the_row();

?>

    <input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">
    <!-- You're using $slideNumber here - but it's not defined or incremented -->

<?php endwhile;endif; ?>

相反,您可以在此处定义和增加此属性:

        <?php if( have_rows('slides') ): 

        $slideNumber = 1; // you define it here
        while ( have_rows('slides') ) : the_row(); 

            $slideNumber++; // you increment it for every slide
            $slideImage = get_sub_field('slide_image');

        ?>

            <article> <!-- but you actually don't use it -->
                <img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>">
            </article>

        <?php endwhile;endif; ?>

请尝试使用此代码:

<!-- Slider Setup -->
<?php if( have_rows('slides') ):
    $slideNumber = 1; // defined
    while ( have_rows('slides') ) : the_row();

?>
    <!-- Has to output something for id attribute, at least "slide1" -->
    <input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">

<?php
$slideNumber++; // incremented
endwhile;
endif;
?>

<!-- Slide -->
<?php if( have_rows('slides') ): ?>

    <div id="slides">
        <div id="overflow">
            <div class="inner">

                <?php if( have_rows('slides') ): 
                while ( have_rows('slides') ) : the_row(); 
                    $slideImage = get_sub_field('slide_image');

                ?>

                    <article>
                        <img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>">
                    </article>

                <?php endwhile;endif; ?>

            </div><!-- #inner -->
        </div><!-- #overflow -->
    </div><!-- #slides -->

<?php endif; ?>