FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
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 '<input type="submit" name="letter" value="'.$letter.'">';



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 Very Happy
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"<button> $letters </button>";
}
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<strlen($_SESSION['chosenword']))
   {
      if ($_POST['letter'] == $_SESSION['chosenword'][$x]) //if it matches
         {
            $_SESSION['hidden'][$x] = $_POST['letter']; //replace
         }
            $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
Related topics
Adverts blocking submit button
Why not have a report button??
How Spyware Works!!!
AIM Mail
How to burn CLONEs
(official) Firefox & Thunderbird 1.5 Beta2 Now Available
To rainbox or not to rainbox?
JS Disable Button.
How to expired a page so a user cannot press back button ?
How can I disable the back button of my browser?
Disabling a textbox on click of a radio button
Disable Right Click On ANy Browser
Disable a button when clicked
Automatic disable of replies?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.