简体   繁体   中英

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. So value of $sub_cat depends on the page that loads. 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. 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 .

<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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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