简体   繁体   English

HTML样式标签内的PHP代码不起作用

[英]Php code inside style tag of html not working

I'm trying to make my sub category looks different when it's selected, for that I've used sub_cat variable in my php. 我试图使子类别在选择时看起来有所不同,因为我在php中使用了sub_cat变量。 So value of $sub_cat depends on the page that loads. 因此$ sub_cat的值取决于加载的页面。 On the top I've this code: 在顶部,我有以下代码:

if(!empty($_GET['sub_cat'])){
        $sub_cat=$_GET['sub_cat'];
    }
    else{
        $sub_cat=0;
    }

Now what my code for displaying list. 现在我的代码用于显示列表。

<li <?php if($sub_cat==11) echo "style='color:#C00'"; ?> ><a href="vegetables.php?sub_cat=11">Popular</a></li>
<li <?php if($sub_cat==12) echo "style='color:#C00'"; ?> ><a href="vegetables.php?sub_cat=12">Exotic</a></li>
<li <?php if($sub_cat==13) echo "style='color:#C00'"; ?> ><a href="vegetables.php?sub_cat=13">Fibrous Veg</a></li>

But it doesn't seem to make any difference, value of $sub_cat is coming fine as in 11,12,13 but this code is not working. 但这似乎没有什么区别,$ sub_cat的值可以像11,12,13那样很好,但是此代码无法正常工作。 Can anyone spot the minor mistake or major mistake? 谁能发现小错误或大错误?

How about transferring the style attribute to the a element instead of placing it in the li . 如何将style属性转移到a元素而不是将其放置在li

<li><a href="vegetables.php?sub_cat=11" <?php if($sub_cat==11) echo "style='color:#C00'"; ?>>Popular</a></li>
<li><a href="vegetables.php?sub_cat=12" <?php if($sub_cat==12) echo "style='color:#C00'"; ?>>Exotic</a></li>
<li><a href="vegetables.php?sub_cat=13" <?php if($sub_cat==13) echo "style='color:#C00'"; ?>>Fibrous Veg</a></li>

Try this: 尝试这个:

if(isset($_GET['sub_cat'])){
        $sub_cat=$_GET['sub_cat'];
    }
    else{
        $sub_cat=0;
    }

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

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