简体   繁体   English

如何在PHP for()循环中的简写if / else语句中使用变量?

[英]How do I use a variable inside of a shorthand if/else statement in a PHP for() loop?

I am trying to use a FOR loop to write dynamic CSS. 我正在尝试使用FOR循环编写动态CSS。 The CSS needs to change based on how many items are in a database table. CSS需要根据数据库表中的项目数量进行更改。 Here is my attempt, but it is not giving me the output I desire: 这是我的尝试,但没有给我我想要的输出:

    <?php
    $count_posts = wp_count_posts( 'portfolio' )->publish; // This is for a wordpress site.
    $slidecount = ceil( $count_posts / 3 ); // there are 3 database entries in each "slide".

    for ($c = 0 ; $c < $slidecount; $c++){ 
    echo '#slide' . ($c + 1) . ':checked ~ #controls label.c-label' . ($c < $slidecount ? (($c + 2) . ', ') : '1'); // This is the part I'm not sure on... How do I properly nest the ($c + 2) expression into a shorthand if/else statement ??
    }
    echo ' {' . "\n" . 'float: right;' . "\n" . 'display: block; }' . "\n";
    ?>

Output (when there are 5 items in the database table): 输出(当数据库表中有5个项目时):

            #slide1:checked ~ #controls label.c-label2, 
    #slide2:checked ~ #controls label.c-label3, 
    #slide3:checked ~ #controls label.c-label4, 
    #slide4:checked ~ #controls label.c-label5, 
    #slide5:checked ~ #controls label.c-label6 {
    float: right;
    display: block;
    }

Desired output: 所需的输出:

    #slide1:checked ~ #controls label.c-label2, 
    #slide2:checked ~ #controls label.c-label3, 
    #slide3:checked ~ #controls label.c-label4, 
    #slide4:checked ~ #controls label.c-label5, 
    #slide5:checked ~ #controls label.c-label1 {
    float: right;
    display: block;
    }

I want the last execution of the FOR loop to append a '1' to the end of 'label', so that the CSS selector points back to the first element "c-label1". 我希望FOR循环的最后一次执行将'1'附加到'label'的末尾,以便CSS选择器指向第一个元素“ c-label1”。 This is being used to build a pure CSS slider that changes the slides using radio buttons and needs to loop the slides back to the beginning once it gets to the end. 这用于构建纯CSS滑块,该滑块使用单选按钮更改幻灯片,并且一旦到达末尾就需要将幻灯片循环回到起始位置。 I need the code to automatically update whenever there is a new database entry saved. 每当保存新的数据库条目时,我都需要代码自动更新。

Can anyone help me clean up the code so it works? 谁能帮我清理代码,使其起作用?

Thank you SO much in advance. 非常感谢您。

In your for loop condition you have 在您的for循环条件下

$c < $slidecount

It means that the same condition in your loop body is also met, so your code to generate c-label1 will never be executed. 这意味着循环主体中的相同条件也得到了满足,因此将永远不会执行生成c-label1的代码。

Try changing 尝试改变

$c < $slidecount 

to

($c < ($slidecount-1))

This is happening because you are using $c < $slidecount in your statement but your $c value starts from 0 so it will never happen to match your slidecount and won't be false. 发生这种情况是因为您在语句中使用了$c < $slidecount ,但是$c值从0开始,所以它永远不会与您的slidecount匹配,并且不会为假。 You should also change your round bracket () 您还应该更改圆括号()

A quick solution could be to add 1 to your $c variable. 一种快速的解决方案是将$c变量加1。 This should work 这应该工作

(($c+1) < $slidecount ? ($c + 2) . ', ' : '1');

It is how you are using ( and ) . 这就是您使用() try this: 尝试这个:

echo '#slide' . ($c + 1) . ':checked ~ #controls label.c-label' . ($c < $slidecount ? $c + 2 . ', ': '1');

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

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