Welcome to WebmasterWorld Guest from 54.145.39.186

Forum Moderators: open

Message Too Old, No Replies

looping through a comma separated string?

     

eatspinach

11:11 am on Jul 7, 2009 (gmt 0)

5+ Year Member



hi i have a string stored in a cookie variable that looks like this 825, 123, 654 etc

these numbers are the ids of the buttons already pressed which i want to keep hidden.. i have some javascript to disable one at a time like for example,

$hideMe = 825;

<script type='text/javascript'>
element = document.getElementById("<?php echo $hideMe; ?>");
element.style.display="none";
</script>

but i need this to work when $hideMe is a big long list of numbers seperated by a comma. has anyone any ideas i am hugely confused as usual ha-ha

thanks

eatspinach

11:25 am on Jul 7, 2009 (gmt 0)

5+ Year Member



I seemed to have solved my own problem, here is my solution..

<script type='text/javascript'>

var questions="<?php echo $hideMe; ?>";

var arry = questions.split(', ');

for(var i=0; i<arry.length; i++){
element = document.getElementById(arry[i]);
element.style.display="none";
alert(arry[i]);
}

</script>

DrDoc

5:23 pm on Jul 7, 2009 (gmt 0)

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



That's actually better handled by PHP itself.

<style type="text/css">
<?=$hideMe?> { display: none; }
</style>

Now, whether

$hideMe
is only a single element or contains a comma separated list of elements, they will all be hidden.

eatspinach

8:11 am on Jul 8, 2009 (gmt 0)

5+ Year Member



You are right my way turned out to be a little buggy.. Thanks a million :)

whoisgregg

7:39 pm on Jul 8, 2009 (gmt 0)

WebmasterWorld Senior Member whoisgregg is a WebmasterWorld Top Contributor of All Time 10+ Year Member



DrDoc's solution is far more reliable. However, short tags and the shorthand echo (the <?=) are both deprecated and may not be supported in future versions of PHP. Your code will last longer if you go ahead and use the full echo statement:

<?php echo $hideMe; ?>
 

Featured Threads

Hot Threads This Week

Hot Threads This Month