使用php变量更改css类 [英] Change css class using php variable

查看:71
本文介绍了使用php变量更改css类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里需要你的帮助。正如标题所示,我正在尝试使用PHP变量更改css类。所以基本上我想创建一个回响一些代码的循环。但我希望第一个循环中的div类不同 - 它应该被隐藏起来。



下面是我为解决问题所做的简化代码。我不知道错误在哪里。请指教。

 <!DOCTYPE html PUBLIC -  // W3C // DTD XHTML 1.0 Transitional // ENhttp:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> 
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>无标题文档< / title>

< style>
.hidden {
visibility:hidden;
}

.visible {
visibility:visible;
}

.firstclass {
color:red;
}

< / style>

< / head>

< body>
<?php
$ case = 4;
$ page =;
$ class =;

if($ page == 0){
$ class =hidden;
}

else {
$ class =visible; ($ page = 0; $ page <$ case; $ page ++){
echo< div class =& quot; firstclass $ class& amp; quot;


} ;">这是页面$ page< br />< / div>;
}

?>
< / body>
< / html>


解决方案

您需要将if条件放入循环

 <?php 
for($ page = 0; $ page< $ case; $ page ++){
if($ page == 0)$ class =hidden;
else $ class =visible;

echo< div class ='firstclass $ class'>这是页面$ page< br />< / div>;
}
?>

因为当$ page == 0有条件的时候它会给一个隐藏的类,所以如果你只是把它放在循环之外,那么$ page永远不会获得一个int值,因为在条件的顶部有$ page =;


need your kind help here. So as the title suggest, I'm trying to change css classes using PHP variable. So basically I want to create a loop that echos some code. But I want the div class in the first loop to be different -- it should be hidden.

Here's the simplified code I made to make the problem clear. I don't know where the mistakes are. Please advise.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<style>
.hidden {
    visibility:hidden;
}

.visible {
    visibility:visible;
}

.firstclass {
    color:red;
}

</style>

</head>

<body>
<?php
    $case = 4;
    $page = "";
    $class = "";

    if ($page == 0) {
        $class = "hidden";
    }

    else {
        $class = "visible";
    }

    for ($page = 0; $page < $case; $page++) {
        echo "<div class = &quot; firstclass $class &quot; >This is page $page <br /></div>";
    }

?>
</body>
</html>

解决方案

You need to put the if condition in the looping

<?php
    for ( $page = 0; $page < $case; $page++ ) {
        if ($page == 0) $class = "hidden";
        else $class = "visible";

        echo "<div class='firstclass $class'>This is page $page <br /></div>";
    }
?>

Because there is a condition when $page == 0 then it will give a hidden class, so if you just put it outside the looping, then $page never get the an int value, because on the top of the condition there is $page = "";

这篇关于使用php变量更改css类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆