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


This piece of code isn't working properly :(





Denvis
Code:
$_SESSION['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');
            if (!isset($_SESSION['letters']))
               {
                  for ($x=0;$x<count($_SESSION['letters']);$x++)
                     {
                        echo '<input type="submit" name="letter" value="'.$_SESSION['letters'].'">';
                     }
               }
            else
               {
                  for ($n=0;$n<count($_SESSION['letters']);$n++)
                     {
                        if ($_SESSION['letters'][$n] == $_POST['letter'])
                           {
                              $_SESSION['letters'][$n] = "no";
                           }
                        for ($e=0;$e<count($_SESSION['letters']);$e++)
                           {
                              if ($_SESSION['letters'][$e] == "no")
                                 {
                                    $e++;
                                 }
                              else
                                 {
                                    echo '<input type="submit" name="letter" value="'.$_SESSION['letters'].'">';
                                 }
                           }   
                     }
               }


I don't think my logic is wrong... I hope not. This is my logic:

Code:
BEGIN
   INPUT letter
      IF input = nothing then
         FOR i = 0 to length of letters ()
            DISPLAY letters (i) as a button
         NEXT
      ELSE
         x = 0
            REPEAT
               IF letter !== letters(x) then
                  add 1 to x
               ELSE
                  letters (x) = "no"
               ENDIF
            UNTIL length of letters or letters(x) = "no"
         FOR x = 0 to length of letters()
            IF letters(x) = "no" then
               add 1 to x
            ELSE
               DISPLAY letters(x) as a button
            ENDIF
         NEXT
      ENDIF
END


it's the buttons for my hangman game Wink Not displaying the ones that have been clicked.
sonam
Did you get any error or blank page.

Sonam
Fire Boar
At a glance, you're setting the $SESSION['letters'] variable to an array, then checking if it is set. But since you've just set it, that condition will always return TRUE. So the code always skips to the "else" and loops over every letter.

Also, you don't want $e++ since $e increments itself in the for loop anyway.

What exactly are you trying to do?
Denvis
No errors, the page isn't blank however, all of its entirety is filled with submit buttons that have a value of "array"

I'm displaying all letters from A to Z as buttons and if a user has clicked a letter, it won't display the they clicked.

I'm adding 1 to e because i'm skipping it
rickylau
Denvis wrote:
all of its entirety is filled with submit buttons that have a value of "array"
Code:
<input type="submit" name="letter" value="'.$_SESSION['letters'].'">
You are not going to print the whole array $_SESSION ['letters'] in a button, instead you are looping through it and print buttons with one character in that array right?
Code:
<input type="submit" name="letter" value="'.$_SESSION['letters'][$x].'">



[Opinion, not sure if related to your problem] I can't get your idea... You firstly assign an array into $_SESSION ['letters'], then how can't it exist (!isset ($_SESSION ['letters'])), if it doesn't exist then how can you loop through it?
Denvis
rickylau wrote:
Denvis wrote:
all of its entirety is filled with submit buttons that have a value of "array"
Code:
<input type="submit" name="letter" value="'.$_SESSION['letters'].'">
You are not going to print the whole array $_SESSION ['letters'] in a button, instead you are looping through it and print buttons with one character in that array right?
Code:
<input type="submit" name="letter" value="'.$_SESSION['letters'][$x].'">


Yep, that's exactly what I'm doing. Looping through it and displaying each letter in its own submit button.
sonam
Ok, I have imagination what do you need but I cannot rebuild your script. For this reason I am create my way. Maybe you will find something good in this script. Wink

Sonam

PHP:
Code:
<?php
session_start();
$word = array("P", "H", "O", "E", "N", "I", "X", "space", "P", "H", "P");

$_SESSION['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', 'space');
if(empty($_POST['letter']) || $_POST['letter'] === "new game") {
   $_SESSION['submited']= "";
   foreach($_SESSION['letters'] as $val) {
       $input = '<input type="submit" name="letter" value="' . $val . '" />';
      $input_all = @$input_all . $input;
      $form = $input_all . ' <input type="submit" name="letter" value="new game" />';
    }
    foreach($word as $val) {
          $value_all = @$value_all . '_ ';
    }
} else {
    $_SESSION['submited'][]= $_POST['letter'];
    $_SESSION['submited'] = array_unique($_SESSION['submited']);
    foreach($_SESSION['letters'] as $val) {
       if(in_array($val, $_SESSION['submited']) !== FALSE) {
          $input = $val . " ";
       } else {
          $input = '<input type="submit" name="letter" value="' . $val . '" />';
       }
      $input_all = @$input_all . $input;
    }
    $form = $input_all . ' <input type="submit" name="letter" value="new game" />';
   
    foreach($word as $val) {
       if(in_array($val, $_SESSION['submited']) !== FALSE && $val != "space") {
          $value = $val . " ";
       } elseif(in_array($val, $_SESSION['submited']) !== FALSE && $val == "space") {
          $value = '&nbsp;&nbsp;' ;
       } else {
          $value = '_ ';
       }
      $value_all = @$value_all . $value;
    }
}
?>


HTML/PHP:
Code:
<form name="form1" method="post" action="proba.php">
  <?php echo $form;?>
</form>
  <?php echo $value_all; ?>
Denvis
thanks! Very Happy problem fixed
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.