PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Sep 23, 2018 2:05 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: Re: Need help
PostPosted: Fri Feb 09, 2018 3:14 am 
Offline
Forum Newbie

Joined: Fri Feb 09, 2018 3:01 am
Posts: 5
Hello,

I need help. I am learning the php, and i am trying to create a hangman game. I have got 2 problems.
1. In the array every accented letter take 2 slots.
2. After the submission, the page does not change.

My code:
akasztofa. php:
Syntax: [ Download ] [ Hide ]
<!DOCTYPE html>
<?php header ('Content-type: text/html; charset=utf-8'); ?>
<html> <head>
<meta http-equiv="content-type"  content="text/html; charset=iso-8859-2" />
<style>
        .tokkozepre{ /*Teljesen középre helyezés */
        position: absolute; top: 50%; left: 50%;
        transform: translate(-50%, -50%);
        }
</style> </head>
<body>
<?php
$generalSzavak=array("macska", "kerékpár", "folyosó", "tenisz", "nyelőcső", "ember", "citrus", "narancslé", "gyöngytyúk", "köszörü", "aknaszedő", "alábbhagy", "áhítat");
$betuk = ['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'];
$rossz=array();
$helyes=array();

?> <div class="tokkozepre"><?php

function oldalbetolt($rossz, $randomszo, $helyes){
        ?> <form method = "POST" action =" " id="beform">
                <b style="font-size: 20px;">A tippelendő betű: </b><br><br>
                <input type="text" style="height:30px; padding: 5px; font-size: 15px;" name="bevittbetu" pattern="[a-záéiíóöőúüű]{1}" title="A mező csak 1 kisbetűt tartalmazhat!!!"><br><br>
                <input type="hidden" name="info1" id="info1" value="<?php print $rossz; ?>" />  
                <input type="hidden" name="info2" id="info2" value="<?php print $randomSzo; ?>" />
                <input type="hidden" name="info3" id="info3" value="<?php print $helyes; ?>" />
                <br><input type="submit" name ="submit" value="Küld!" style="height:45px; width:60px; font-size: 16px" >
        </form><?php
}

function ujjatek(){
        global $generalSzavak; global $rossz; global $randomSzo;
        $randomSzo=$generalSzavak[array_rand($generalSzavak)];
        print $randomSzo."<br>";
        for ($i=0; $i<strlen($randomSzo); $i++) $valasz[]= $randomSzo[$i];
        print_r($valasz);
        ?><h1 id="szo">
        <?php for ($y=0; $y<strlen($randomSzo); $y++) print $y; ?>
        </h1>
        <script>
        funtion atvalt(){ document.getElementById('beform').action = "akasztofa.php";}
        </script><?php
        oldalbetolt(0,$randomSzo,0);
}

function koztesoldal(){
        $ertek1=$_POST['info1']; $ertek2=$_POST['info2']; $ertek3=$_POST['info3'];
        $tippbetu=$_POST['bevittbetu'];
       
        for ($a=0; $a<strlen($randomSzo); $a++){
                        if ($tippbetu==$randomSzo[$a]){
                                $helyes[]=$tippbetu; $valasz[$a]=$tippbetu;
                        } else { $rossz[]=$tippbetu;}
        }
        oldalbetolt($ertek1, $ertek2, $ertek3);
        if (count($ertek3)==strlen($ertek2)) {print "<b style=\"color:green; font-size: 20px\"\">NYERTÉL - KITALÁLTAD! A keresett szó: ".$randomSzo."<br><br></b>";}
        else{
               
                print "<table>"; print "<tr>"; print "<td>";
                require_once 'abrak.php'; /* Akasztási ábrák */
                print "</td>"; print "</tr>";
                print "<tr>"; print "<td>"; print "<b>Hibás betűk: </b><br>";
                for ($y=0; $y<count($ertek1); $y++) print "<b>".$ertek1[$y].", </b>";
                print "</table>";              
        }
}

if (isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['bevittbetu']) && ($_POST['submit'] === 'submit')) koztesoldal();
else ujjatek();
?>
</div></body></html>

abrak.php:
<?php
switch (count($ertek1)){ /*Ábra fázisok */
                                case "1":
                                print ("Hibás! Próbáld újra.<br><br>");
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp<br>";
                                print("_ _ _|_ _ _");
                                break;

                                case "2":
                                print ("Hibás! Próbáld újra<br><br>");
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print("_ _ _|_ _ _");
                                break;

                                case "3":
                                print ("Hibás! Próbáld újra<br><br>");
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print("_ _ _|_ _ _");
                                break;

                                case "4":
                                print ("Hibás! Próbáld újra<br><br>");
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp_|_<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."_&nbsp; _/<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print("_ _ _|_ _ _");
                                break;

                                case "5":
                                print ("Hibás! Próbáld újra<br><br>");
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp_|_<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."_&nbsp; _/<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp<br>";
                                print("_ _ _|_ _ _");
                                break;

                                case "6":
                                print ("<b style=\"color:red; font-size: 20px\"\">VESZTETTÉL-Nincs több lehetőség<br><br></b>");
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_ _ _ _ _ _<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp_|_<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp".chr(92)."".chr(92)."_&nbsp; _/<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp|<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/ ".chr(92)."".chr(92)."<br>";
                                print"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; / &nbsp;&nbsp".chr(92)."".chr(92)."<br>";
                                print("_ _ _|_ _ _");
                                break; 
                        }
                ?>


Top
 Profile  
 
 Post subject: Re: Re: Need help
PostPosted: Fri Feb 09, 2018 2:41 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13576
Location: New York, NY, US
I think you want strlen($ertek1) instead of count($ertek1).

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Re: Need help
PostPosted: Fri Feb 09, 2018 2:57 pm 
Offline
Forum Newbie

Joined: Fri Feb 09, 2018 3:01 am
Posts: 5
which row?
if this: "switch (count($ertek1)){ /*Ábra fázisok */"
then i used count, because i examine how many incorrect(guessed) letters contain the array.


Top
 Profile  
 
 Post subject: Re: Re: Need help
PostPosted: Sun Feb 11, 2018 4:49 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13576
Location: New York, NY, US
Andora wrote:
which row?
if this: "switch (count($ertek1)){ /*Ábra fázisok */"
then i used count, because i examine how many incorrect(guessed) letters contain the array.

count() gives you the length of an array. strlen() gives you the length of a string. The value of $ertek1 is from the $_POST which are strings.
Syntax: [ Download ] [ Hide ]
 $ertek1=$_POST['info1'];

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Need help
PostPosted: Sun Feb 11, 2018 8:51 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 366
Doesn't strlen give incorrent length for strings containing é ñ ø ü ä å etc ?

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
 Post subject: Re: Re: Need help
PostPosted: Sun Feb 11, 2018 8:54 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 366
echo strlen("hello");
echo "<br>";
echo strlen("héllo");
echo "<br>";
echo mb_strlen("héllo",'UTF-8');

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
 Post subject: Re: Re: Need help
PostPosted: Mon Feb 12, 2018 6:14 am 
Offline
Forum Newbie

Joined: Fri Feb 09, 2018 3:01 am
Posts: 5
Thank you Christopher, I did not know that it changes to a string. I thought the array will stay from the post.

Thinsoldier: i tried with mb_strlen and it work, thank you.


And how can i change the appearance after the submit? Because now they are identical. Firstly i want the tip form to be displayed. If the tip is incorrect the gallows should appear but if its correct the guessed letter should appear in the right place.


Top
 Profile  
 
 Post subject: Re: Re: Need help
PostPosted: Mon Feb 12, 2018 11:33 am 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13576
Location: New York, NY, US
Andora wrote:
And how can i change the appearance after the submit? Because now they are identical. Firstly i want the tip form to be displayed. If the tip is incorrect the gallows should appear but if its correct the guessed letter should appear in the right place.

I am not sure looking at your code, but you could check each guess by comparing
Syntax: [ Download ] [ Hide ]
// $guess = $_POST['guess']   // e.g., "hangm" (correct) or "hange" (incorrect)
$word = 'hangman';
if ($guess == substr($word, 0, strlen($guess)) ) {
    if (strlen($guess) == strlen($word)) {
        // word matched! you won!
    } else {
        // guesses matches so far, keep going
    }
} else {
    // a character in guess does not match word, show error
}

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Need help
PostPosted: Mon Feb 12, 2018 11:43 am 
Offline
Forum Newbie

Joined: Fri Feb 09, 2018 3:01 am
Posts: 5
I tried with "function ujatek()" and "function koztesoldal()" and with this:
if (isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['bevittbetu']) && ($_POST['submit'] === 'submit')) koztesoldal();
else ujjatek();

2 different page.
Sorry that is not entirely in the English language code. I am Hungarian, and variables, alerts i wrote in Hungarian.
Example= "NYERTÉL - KITALÁLTAD!" = "You won!"


Top
 Profile  
 
 Post subject: Re: Need help
PostPosted: Mon Feb 12, 2018 12:04 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13576
Location: New York, NY, US
Can you explain what this code is doing?
Syntax: [ Download ] [ Hide ]
if (isset($_POST['info1']) && isset($_POST['info2']) && isset($_POST['info3']) && isset($_POST['bevittbetu']) && ($_POST['submit'] === 'submit'))
  koztesoldal();
else
  ujjatek();

_________________
(#10850)


Top
 Profile  
 
 Post subject: Re: Need help
PostPosted: Mon Feb 12, 2018 1:19 pm 
Offline
Forum Newbie

Joined: Fri Feb 09, 2018 3:01 am
Posts: 5
Id rather tell what I want to do.

ujjarek() = start a new game, which generates a random word from the generalSzavak array and it stores it in the "randomSzo""($_POST['info2']). Furthermore, I create a input field which can be used to type in the selected letter ($_POST['bevittbetu']). Below this will be the submit button.
After the player submits the letter, the button activates the "koztesoldal()" on the same page with the previous. I change just the appearance of the website.
It also displays the input field and the submit button, and if the tip is incorrect, the next part of the gallows (which is contains in 'abrak.php') will also appear. If the tip is correct the letter should appear in the right position.
Also, if the tip is incorrect, it stores it in the "rossz" array($_POST['info1']), but if its correct then in the "helyes" array($_POST['info3']).
If the "helyes" array corresponds with the length of the "randomSzo", it writes out on the screen: You Won.

The hidden fields are required due to multiple programs running - after each guess letter it run again. And after every submit, I need to post the data.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group