Jump to content

Word tally


jaArch

Recommended Posts

Hi: I can't seem to figure out how I would display the words the user enters in an ordered list on the bottom of the page in the <div id="guesses"> area. A hidden form variable is used to store all the previous guesses. (no cookies, no session variables).

 

Also, how do I get the guesses to appear in the top of the page so that it looks like this:

 

Array

(

    [myguess] => eee

    [alltheguesses] => aaa|ccc|ddd|ggg

)

 

This is what I seem to be getting:

 

Array

(

    [myguess] => yy

    [allTheGuesses] =>

)

 

<?= '<pre>' . print_r($_POST,true) . '</pre>'; ?>
<?
    $WORDS = array("grape","apple","orange","banana","plum","grapefruit");
    define ("GUESSINDEX",3);
    define("THEWORD",$WORDS[GUESSINDEX]); 


foreach ($WORDS as $val){
    
    if ('banana' == $val)
    echo 'found it.';
     else 
    echo 'not found.';
}

if (in_array('plum',$WORDS == true)){
    
}

foreach ($guessesarr as $val)
echo "<li> $val</li>";

foreach ($WORDS as $val) echo "$val | ";
echo "<br>";
echo implode(' | ',$WORDS);

?>

<html>
<style type="text/css">
body {background-color:orange}
div {margin:10px}
div#container {background-color: white; border: 3px solid black;width: 400px;margin-right: auto;margin-left: auto; text-align:center;}
div#error {color:red}
div#guesses,div#header p span {color:green; }
h1,h2,h3 {color:#633}
</style>
<body>
<div id="container">
   <div id="header">
       <p>grape | apple | orange | <span>banana</span> | plum | grapefruit</p>
       <h1>Word Guess</h1>
       <a href="<?$_SERVER['PHP_SELF']?>">refresh this page</a>
   </div> 
   <div id="form">
      <h3>Guess the word I'm thinking</h3>
      <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>" >
        <input name="myguess" type="text" value="<? echo $_POST['myguess']; ?>">
        <input type="hidden" name="allTheGuesses" value = "<? echo $_POST['allTheGuesses']; ?>">
        <input type="submit" value="GUESS">
      </form>       
   </div>
    
   <div id="error">
         <?
        if ($_SERVER['REQUEST_METHOD'] == 'GET')
            echo "It's time to play the guessing game! (1)";
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $MYGUESS = trim($_POST['myguess']);
            
            if ($MYGUESS == "")
                echo "C'mon, guess something (2)";
            elseif (in_array($MYGUESS,$WORDS) == false)
                echo "Hey, thats not even a valid guess. Try again (5)"; 
            elseif ($MYGUESS == THEWORD)
                echo "You guessed " . THEWORD . " and that's CORRECT!!! (3)";
            elseif ($MYGUESS <> THEWORD)
                echo "Sorry $MYGUESS is wrong. Try again (4)";
        }
           ?>
   </div>



   <div id="guesses">
   <center>
   <?

     echo "<ol>";
    $allguesses = trim($_POST['allTheGuesses'],", ");
    $guessarr = explode(", ", $allguesses);
    foreach( $guessarr as $guess ){
        echo "<li>$guess</li>";
    }
    echo "</ol>";


   ?>
   </center>
   </div>

</div>     
</body>
</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/248672-word-tally/
Share on other sites

$_SERVER

 

This will just tell if the server was sent a GET or a POST request.  It is usually used like:

<?php
if($_SERVER['REQUEST_METHOD'] == 'post') {
//Do
}

 

But what am I supposed to put in after the if statement? That's where I'm lost. Also, where does the code go if I were to put it in here?

 

<?= '<pre>' . print_r($_POST,true) . '</pre>'; ?>
<?

    $WORDS = array("grape","apple","orange","banana","plum","grapefruit");
    define ("GUESSINDEX");
    define("THEWORD",$WORDS[GUESSINDEX]); 



if (in_array('plum',$WORDS == true)){
    
}

foreach ($guessesarr as $val)
echo "<li> $val</li>";

foreach ($WORDS as $val) echo "$val | ";
echo "<br>";
echo implode(' | ',$WORDS);

?>

<html>
<style type="text/css">
body {background-color:orange}
div {margin:10px}
div#container {background-color: white; border: 3px solid black;width: 400px;margin-right: auto;margin-left: auto; text-align:center;}
div#error {color:red}
div#guesses,div#header p span {color:green; }
h1,h2,h3 {color:#633}
</style>
<body>
<div id="container">
   <div id="header">
       <p>grape | apple | orange | <span>banana</span> | plum | grapefruit</p>
       <h1>Word Guess</h1>
       <a href="<?$_SERVER['PHP_SELF']?>">refresh this page</a>
   </div> 
   <div id="form">
      <h3>Guess the word I'm thinking</h3>
      <form name="form1" method="post" action="<?=$_SERVER['PHP_SELF']?>" >
        <input name="myguess" type="text" value="<? echo $_POST['myguess']; ?>">
        <input type="hidden" name="allTheGuesses" value = "<? echo $_POST['allTheGuesses'] , '|' ,  $_POST['myguess']; ?>">
        <input type="submit" value="GUESS">
      </form>       
   </div>
    
   <div id="error">
         <?
        if ($_SERVER['REQUEST_METHOD'] == 'GET')
            echo "It's time to play the guessing game! (1)";
        
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $MYGUESS = trim($_POST['myguess']);
            
            if ($MYGUESS == "")
                echo "C'mon, guess something (2)";
            elseif (in_array($MYGUESS,$WORDS) == false)
                echo "Hey, thats not even a valid guess. Try again (5)"; 
            elseif ($MYGUESS == THEWORD)
                echo "You guessed " . THEWORD . " and that's CORRECT!!! (3)";
            elseif ($MYGUESS <> THEWORD)
                echo "Sorry $MYGUESS is wrong. Try again (4)";
        }
           ?>
   </div>



   <div id="guesses">
   <center>
   <?
echo "Your guesses";
     echo "<ol>";
    $allguesses = trim($_POST['allTheGuesses'],", ");
    $guessarr = explode(", ", $allguesses);
    foreach( $guessarr as $guess ){
        echo "<li>$guess</li>";
    }
    echo "</ol>";


   ?>
   </center>
   </div>

</div>     
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/248672-word-tally/#findComment-1277259
Share on other sites

I wasn't sure exactly how the flow of the script was suppose to operate.  I did notice a lot of redundancy, so I re-wrote portions, trying to keep the output the same.

 

<?php //echo '<pre>' . print_r($_POST,true) . '</pre>'; //debugging?>
<?php
    $WORDS = array("grape","apple","orange","banana","plum","grapefruit"); //add or take away as many as you like;
    define ("GUESSINDEX",3); //define the desired word, count starts at 0.
    define("THEWORD",$WORDS[GUESSINDEX]); //defines the actual word.
$count = (isset($_POST['count'])) ? (int)$_POST['count'] : 0; //keep count of how many times the form is submitted WRONG.
$all_guesses = (isset($_POST['allTheGuesses'])) ? $_POST['allTheGuesses'] : NULL; //our guesses.
$message = 'It\'s time to play the guessing game! (1)'; //if POST isn't the request method, this will be our message.
if($_SERVER['REQUEST_METHOD'] == 'POST') {
if($_POST['myguess'] == THEWORD) {
	$message = 'You guessed ' . THEWORD . ' and that\'s CORRECT!!! (3)'; //if the answer is correct.
}
else {
	++$count; //if the answer is incorrect, increment the count.
	$all_guesses .= '|' . $_POST['myguess']; //add the guess to our wrong list.
	if(empty($_POST['myguess'])) { 
		$message = 'C\'mon, guess something (2)'; //if the guess is emtpy.
	}
	elseif(!in_array($_POST['myguess'], $WORDS)) {
		$message = 'Hey, thats not even a valid guess. Try again (5)'; //if the guess doesn't exist in the array.
	}
	else {
		$message = "Sorry {$_POST['myguess']} is wrong. Try again (4)"; //if the guess is just wrong.
	}
}
}

?>

<html>
<style type="text/css">
body {background-color:orange}
div {margin:10px}
div#container {background-color: white; border: 3px solid black;width: 400px;margin-right: auto;margin-left: auto; text-align:center;}
div#error {color:red}
div#guesses,div#header p span {color:green; }
h1,h2,h3 {color:#633}
</style>
<body>
<div id="container">
   <div id="header">
       <p><?php echo implode(' | ',$WORDS); //automatically write out the word list, if you add more to the array, it will show up.?></p> 
       <h1>Word Guess</h1>
       <a href="?">refresh this page</a>
   </div> 
   <div id="form">
      <h3>Guess the word I'm thinking</h3>
      <form name="form1" method="post" action="<?php //do not use PHP SELF, XSS vulnerability?>" >
        <input name="myguess" type="text" value="<?php echo (isset($_POST['myguess'])) ? $_POST['myguess'] : NULL; ?>">
        <input type="hidden" name="allTheGuesses" value = "<?php echo $all_guesses; ?>">
	<input type="hidden" name="count" value="<?php echo $count; ?>">
        <input type="submit" value="GUESS">
      </form>       
   </div>
    
   <div id="error">
         <?php echo $message; ?>
   </div>

   <div id="guesses">
   <center>
   <?php
$arr = (!empty($all_guesses)) ? explode('|',ltrim($all_guesses,'|')) : 'string';
     echo (is_array($arr)) ? "{$count} wrong guesses:<ol>\n<li>" . implode("</li>\n<li>",$arr) . "</li>\n</ol>": NULL;
   ?>
   </center>
   </div>

</div>     
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/248672-word-tally/#findComment-1277263
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.