1

Why doesn't this code work?

<?php

$filen = "C:/wamp/www/uppgifter/saves/speltva.txt";
$s=fopen($filen,'a+');
$f=fopen($filen,'r');

if(isset($_POST['nyttspel'])) {
    $result = "--";
    $s=fopen($filen,'w');
    fwrite ($s, $result);
    fclose($s);
} else {
    $f=fopen($filen,'r');
    $result = fgets($f);
}

if(isset($_POST['sten'])) {
    if ($result[0] == "-")
        $result[0] == "a";
    else
        $result[1] == "a";
}

if(isset($_POST['sax'])) {
    if ($result[0] == "-")
        $result[0] == "b";
    else
        $result[1] == "b";
}

if(isset($_POST['pase'])) {
    if ($result[0] == "-")
        $result[0] == "c";
    else
        $result[1] == "c";
}

fclose($f);
$s=fopen($filen,'w');
fwrite ($s, $result);
echo $result;

?>
<form method=post action=stensax.php>
<center>
<input type='submit' name=nyttspel value=NYTT&nbsp;SPEL>
<input type='submit' name=sten value=STEN>
<input type='submit' name=sax value=SAX>
<input type='submit' name=pase value=P&Aring;SE>
</center>

I want it to put "--" in the file first as a gameboard. When someone selects "sten" (rock) it should change the first "-" into an "a" if it's empty, otherwise change the second "-" to an "a". When I press "nyttspel" (new game), it fills the .txt file with an empty gameboard, like it should, but nothing happens when I press any of the other buttons. Why is that?

John Slegers
  • 38,420
  • 17
  • 182
  • 152
Birks
  • 123
  • 10
  • Code should be put into the actual question...not in a link. Questions should be self contained – charlietfl Mar 27 '16 at 13:39
  • Your code deals with files in a strange and confusing manner. Rewrite it so that it assigns file handles ***only if*** and more importantly ***when*** it actually needs to read from or write to a file. This will probably make it clearer to you what happens and when as well. – Gralgrathor Mar 27 '16 at 13:44
  • Instead of assigning you are comparing in numerous occasions. If (==) assign (=) – Ruby Racer Mar 27 '16 at 13:45

2 Answers2

3

In your comparisons, then you should assign, not compare. For example:

Wrong:

if(isset($_POST['sten'])) {
    if ($result[0] == "-")
        $result[0] == "a"; // comparing
    else
        $result[1] == "a";
 }

Right:

if(isset($_POST['sten'])) {
    if ($result[0] == "-")
        $result[0] = "a"; // assigning
    else
        $result[1] = "a";
 }
Ruby Racer
  • 5,408
  • 1
  • 22
  • 40
0

I found several issues in your code :

  • You're using comparisons (==) where you should be using assignments (=)
  • You're using the <center> tag, which has been deprecated
  • You're using echo to print an array (which just returns Array)
  • There's no </form> to close your form
  • Your reading / writing from the file is all over the place. I don't know what you're trying to do, but I'm pretty sure there's something wrong with that as well!

And while there's technically nothing wrong with using == comparisons, it's better to use === comparisons. Also, it's always better to put the values of HTML properties between "" or ''


The following code implements all improvements I mentioned, except for the reading / writing (as I'm not sure what exactly you're trying to achieve) :

<?php

$filen = "speltva.txt";
$s=fopen($filen,'a+');
$f=fopen($filen,'r');

if(isset($_POST['nyttspel'])) {
    $result = "--";
    $s=fopen($filen,'w');
    fwrite ($s, $result);
    fclose($s);
} else {
    $f=fopen($filen,'r');
    $result = fgets($f);
}

if(isset($_POST['sten'])) {
    if ($result[0] === "-")
        $result[0] = "a";
    else
        $result[1] = "a";
}

if(isset($_POST['sax'])) {
    if ($result[0] === "-")
        $result[0] = "b";
    else
        $result[1] = "b";
}

if(isset($_POST['pase'])) {
    if ($result[0] === "-")
        $result[0] = "c";
    else
        $result[1] = "c";
}

fclose($f);
$s=fopen($filen,'w');
fwrite ($s, $result);
print_r($result);

?>
<form method="post" action="stensax.php" style="text-align:center">
    <input type="submit" name="nyttspel" value="NYTT&nbsp;SPEL">
    <input type="submit" name="sten" value="STEN">
    <input type="submit" name="sax" value="SAX">
    <input type="submit" name="pase" value="P&Aring;SE">
</form>
John Slegers
  • 38,420
  • 17
  • 182
  • 152
  • Thanks for the correction, much appreciated. I have some follow-up questions though, What's the problem with using
    -tags aswell as not having "" to close the html-enteties? What's the difference between print_r and echo in this case (I get the same result with both of them) & isn't
    self-closing?
    – Birks Mar 27 '16 at 23:30
  • @Olle : [Why is the
    tag deprecated in HTML?](http://stackoverflow.com/questions/1798817/why-is-the-center-tag-deprecated-in-html)
    – John Slegers Mar 28 '16 at 00:18
  • @Olle : [Unquoted attribute values in HTML and CSS/JS selectors](https://mathiasbynens.be/notes/unquoted-attribute-values) – John Slegers Mar 28 '16 at 00:21
  • @Olle : [The `
    ` tag](https://developer.mozilla.org/en/docs/Web/HTML/Element/form)
    – John Slegers Mar 28 '16 at 00:22
  • @Olle : [What's the difference between echo, print, and print_r in PHP?](http://stackoverflow.com/questions/1647322/whats-the-difference-between-echo-print-and-print-r-in-php) – John Slegers Mar 28 '16 at 00:25