FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


I need help with a very simple php code :(





Denvis
Right now I have this

Code:
<html>
   <body>
   <center>
   <h1>Hangman</h1>
<?php
$word="d";
   echo $word;
   if ($_POST['letter']==$word)
   {
      echo " good";
   }
   else
   {
      echo " bad";
   }
?>
   <form action="index.php" method="post">
      <?php
      $letters = array('a','b','c','d','e','f','g','h','i','j','k','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.'">'."\r\n";
      }
      ?>
   </form>
   </center>
   </form>
</html>


But now I wanna take it to the next step and have an actual word instead of just a single letter. I want it so that whenever I click a letter and if it matches a letter in the word, something happens (maybe a tally or something)

I'm so confused Crying or Very sad Any help will be much appreciated!
Fire Boar
You want the condition...

Code:
if (strpos($word, $_POST['letter']) !== false)


The function strpos returns the position where the second parameter occurs in the first, or false if not found. Note that you need to use !== and not != because it can return 0 (where the letter is found at the beginning of the word), and 0 == false according to PHP. !== is stricter.
rvec
Fire Boar wrote:
You want the condition...

Code:
if (strpos($word, $_POST['letter']) !== false)


The function strpos returns the position where the second parameter occurs in the first, or false if not found. Note that you need to use !== and not != because it can return 0 (where the letter is found at the beginning of the word), and 0 == false according to PHP. !== is stricter.

!= and == only check if the values are the same, and convert the types if they are not the same (for example 0 and FALSE with be seen as the same because when converted to boolean 0 is the same as FALSE and when converted to integer FALSE is the same as 0)
!== and === also check if the types are the same, so 0 and FALSE will not be the same.
Denvis
Code:
<html>
   <body>
   <center>
   <h1>Hangman</h1>
<?php
   $word = array('kingsgrove','david','school','software','computer');
for ($num=0; $num<strlen($word) ;$num++) {
   echo "*";
}

$pos = strpos($word, $_POST['letter']);
if ($pos === false) {
   echo "</br>" . " No, this letter " . " '$letter' " . " is not in the word.";
} else {
   echo "</br>" . " Yes, this letter " . " '$letter' " . " is in the word.";
}
?>
   <form action="index.php" method="post">
      <?php
      $letters = array('a','b','c','d','e','f','g','h','i','j','k','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.'">'."\r\n";
      }
      ?>
   </form>
   </center>
   </form>
</html>


I used === instead of !== and it seems to work. I have another problem. I want to select a random word from $word and then display that word as an * until a letter is clicked. Then a letter will replace the * if the letter clicked matches a letter in the word.

How will I come about doing something like that? Confused

edit: oh, don't worry about generating a random element from the array. I used
Code:
$rand = $word [array_rand($word)];
to achieve it.
Fire Boar
I'll start you off with a function that might help you. It takes as input the word and an array of letters that have already been guessed, and returns the word with * in place of any letters not correctly guessed yet.

Code:
function word_output($word, $letters = array())
{
  $return = '';
  $len    = strlen($word);

  for ($i = 0; $i < $len; $i++)
  {
    // Add either the letter of the word, or '*'.
    $return .= (in_array($word[$i], $letters) ? $word[$i] : '*');
  }

  return $return;
}


For example,

Code:
echo word_output('television', array('e', 'i', 't')); // prints 'te*e*i*i**'


Look up PHP session variables to find out how you can keep data going over several page views. You will want to store the array of letters guessed in a session variable, updating it each time the user guesses a letter and passing it to word_output. You should also store the word to be guessed in an array.


Aside:

</br> is not valid HTML. You should use <br /> instead. Also note that you're joining strings unnecessarily:

Code:
echo "<br />" . " No, this letter " . " '$letter' " . " is not in the word.";


Could be more efficiently represented as:

Code:
echo "<br /> No, this letter '$letter' is not in the word.";
Related topics
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.