You are invited to Log in or Register a free Frihost Account!

# Compose an algorithm to disable a button?

Denvis
I have a hangman game and I'm displaying each element in my array as a button.

 Code: \$letters = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');          foreach (\$letters as \$letter) echo '';

I want to disable the letter clicked once its compared to the letters in the word. I'm thinking maybe moving the letters clicked to a temp array and only echoing the letters in the current array and echoing the ones in the temp array as disabled (or maybe not echo them at all)

Is this approach plausible or am I going at it completely wrong? Can someone produce an algorithm in pseudocode or better yet show me how to do it in PHP?

Thanks
Nemesis234
jeez you sure are making hard work for a hangman game.

without knowing how you are counting what letters the user has chose and how you are storing them its quite hard to offer a complete solution.

in php there is a function, array_diff which will compare 2 arrays and tell you the differences, so if you stored abcd... etc in 1 array, and the letters the user has chosen in the other, array_diff it, will give you all letter user hasnt chosen, then do your code on that new arracy created by array_diff.

 Code: \$array_letters = array(abcdefghij...) \$user_letters = array(aeigqk) \$unpicked = array_diff(\$array_letters, \$user_letters) foreach (\$unpicked as \$letters){ print""; }
Denvis
cheers! Yeah, it's a school project. I like the logic behind how it works, algorithms as such but i hate the syntax... one silly mistake and it won't work.

Currently i'm not storing the letters clicked. I'm just replacing letters in the word with the letter clicked.

 Code: while (\$x
PureReborn
I think you're better off doing this through Ajax (javascript) than PHP. Through PHP you need to reload the page after each time a button is pressed.

Through javascript you just do the following:
give each button element an unique id (the letter they represent works well here)
then give each element an onclick function that:
1) does watever it's suppose to call (some POST some info to your PHP script)
2) changes the button style to either hide it or disable the button.

You can do this pretty easily with jQuery.
sonam
 PureReborn wrote: I think you're better off doing this through Ajax (javascript) than PHP. Through PHP you need to reload the page after each time a button is pressed. Through javascript you just do the following: give each button element an unique id (the letter they represent works well here) then give each element an onclick function that: 1) does watever it's suppose to call (some POST some info to your PHP script) 2) changes the button style to either hide it or disable the button. You can do this pretty easily with jQuery.

You are little bit late. He solved this problem before one month.

Sonam