Sunday, 15 February 2015

PHP for each loop that applies different CSS stlyes -



PHP for each loop that applies different CSS stlyes -

i create link bold if the url ends ending:

http://url.com/test#style1

http://url.com/test#style2

...and on #style15

would best php or javascript? create next loop runs through #style1 - #style15?

<?php $url = 'http://'.$_server['http_host'].$_server['php_self']; if ($url == "http://url.com/test#style1") { ?> <style>.a1 {font-weight:bold;} </style> <? } else if ($url == "http://url.com/test#style2") { ?> <style>.a2 {font-weight:bold;} </style> <? }?>

you cannot access hash in php i.e. here "#style1" url, have javascript.

here solution in javascript:

<script> if (window.location.hash) { var hash = window.location.hash.substring(1); if (hash.length != 0) { for(i=1; i<=15; i++){ if(("style"+i) == hash){ /* used jquery here */ $(".a"+i).css("font-weight", "bold"); } } } } </script>

php for-loop

No comments:

Post a Comment