Basic quest code in PHP I'm a beginner in PHP and I would like to know how to make a good quest system.

What is the code for : This code is to accept quest. It show the quest and where the user is in the quest (Ongoing, Available, Finished..).

What I'm looking for : As a beginner, this code is quite bad, and i'm looking for an improved version. For example, all the code is only working for 1 quest, and I would like an useful way to automatically put the quest when created instead of having to copy/paste the whole thing and change the id of the quest. And of course if i could do this in 1 query instead of querya and queryb (1 For the Fetch and 1 For the FetchColumn) it would be good.

The code is actually working if you wonder

Important informations : Most of the text is in frech, so while I'm translating it, I may forgot to change 1 or 2 things. If that's the case, don't assume the code is not working : just tell me what is wrong and it may be a mistake.

The Code

&lt;?php
  $querya = $db-&gt;prepare('SELECT * FROM quetejoueur WHERE id_joueur=:id AND id_queteorij=1');
  $querya-&gt;bindValue(':id', $donnees["id"], PDO::PARAM_STR);
  $querya-&gt;execute();

  $queryb = $db-&gt;prepare('SELECT * FROM quetejoueur WHERE id_joueur=:id AND id_queteorij=1');
  $queryb-&gt;bindValue(':id', $donnees["id"], PDO::PARAM_STR);
  $queryb-&gt;execute();

while ( $findquetes = $querya-&gt;fetch() ) {
    if ($queryb-&gt;fetchColumn() &gt; 0) {
      if ($findquetes["comp_quetej"] &lt; 1) {
          ?&gt;&lt;button disabled class="pomegranate-flat-button" style=" border-radius:6px; font-size:15px; padding-left:45px; padding-right:45px;"&gt; Quest - Quest 1 (Ongoing) &lt;/button&gt;&lt;?php
      }


    if ($findquetes["comp_quetej"] &gt; 0) {
      if ($findquetes["recomp_quetej"] &lt; 1) {
          echo '&lt;form action="" method="POST"&gt;&lt;input type="submit" class="peter-river-flat-button" style=" border-radius:6px; font-size:15px; padding-left:45px; padding-right:45px;" value="Quest - Quest 1 (Confirm the quest)" name="confirmerquete"&gt;&lt;/form&gt;';
      }

      if ($findquetes["recomp_quetej"] &gt; 0) {
        echo'&lt;button class="emerald-flat-button" style=" border-radius:6px; font-size:15px; padding-left:45px; padding-right:45px;"&gt;Quest- Quest1 (Finished) &lt;/button&gt;';
      }
    }

    }
    else {
      ?&gt;&lt;button class="peter-river-flat-button" style="border-radius:6px; padding-left:45px; padding-right:45px;" onclick="window.location.href='../quete/vigier.php'"&gt;Quête - Le Vigier (Disponible) &lt;/button&gt;&lt;?php
    }

}

$querya-&gt;CloseCursor();
$queryb-&gt;CloseCursor();

  if ((isset($_POST["confirmerquete"])) AND ($findquetes["recomp_quetej"] &lt; 1)) {
    $querya=$db-&gt;prepare('UPDATE users SET exp= (exp + :queteexp) WHERE username=:username');
    $querya-&gt;bindValue(':queteexp', $findquetes["xp_quetej"], PDO::PARAM_INT);
    $querya-&gt;bindValue(':username', $username, PDO::PARAM_STR);
    $querya-&gt;execute();
    $querya-&gt;CloseCursor();

    $queryb=$db-&gt;prepare('UPDATE users SET money=(money + :moneyquest) WHERE username=:username');
    $queryb-&gt;bindValue(':moneyquest', $findquetes["or_quetej"], PDO::PARAM_INT);
    $queryb-&gt;bindValue(':username', $username, PDO::PARAM_STR);
    $queryb-&gt;execute();
    $queryb-&gt;CloseCursor();

    $query=$db-&gt;prepare('UPDATE quetejoueur SET recomp_quetej=1 WHERE id_quetej = :idqj');
    $query-&gt;bindParam(':idqj', $findquetes['id_quetej'], PDO::PARAM_INT);
    $query-&gt;execute();
    $query-&gt;CloseCursor();
  }


?&gt;


If you need further explainations, just tell me. I'm sorry if this code is bad written, as I said this is only a beginner's work.
I don't understand the need for two queries.  They seem to be retrieving the same exact result set, so why query twice?



Your code is difficult to read because of the inconsistent indentation and the inconsistent way in which you render your HTML output (sometimes dropping out of PHP, sometimes echoing out of PHP, for more or less that same type of use case).
 I would also encourage you to get better with overall style consistency.  Variable names are not very meaningful (querya/queryb). Spacing around operators is inconsistent. Spacing around parenthesis is inconsistent.  Use an IDE that allows you to enforce a styleguide and stick with it.



The nesting in your while loop is a bit out of control. You should think about inverting conditionals and/or using continue to clean up these nested conditionals.  Consider how much easier it is to read code like this:

&lt;?php 
while ( $findquetes = $querya-&gt;fetch() ) {
    if ($queryb-&gt;fetchColumn() &lt;= 0) {
?&gt;
&lt;button
    class="peter-river-flat-button"
    style="border-radius:6px; padding-left:45px; padding-right:45px;" 
    onclick="window.location.href='../quete/vigier.php'"
&gt;Quête - Le Vigier (Disponible)&lt;/button&gt;
&lt;?php
        continue;
    }
    if ($findquetes["comp_quetej"] &lt;= 0) {
        continue;
    }
    if ($findquetes["comp_quetej"] &lt; 1) {
?&gt;
&lt;button disabled class="pomegranate-flat-button"
    style=" border-radius:6px; font-size:15px; padding-left:45px; padding-right:45px;"&gt;Quest - Quest 1 (Ongoing)&lt;/button&gt;
&lt;?php
        continue;
    }
    if($findquetes["recomp_quetej"] &lt;= 0) {
        continue;
    }
    if ($findquetes["recomp_quetej"] &lt; 1) {
?&gt;
&lt;form action="" method="POST"&gt;
    &lt;input type="submit" class="peter-river-flat-button"
        style=" border-radius:6px; font-size:15px; padding-left:45px; padding-right:45px;"
        value="Quest - Quest 1 (Confirm the quest)" name="confirmerquete"&gt;&lt;/form&gt;
&lt;?php
        continue;       
    }
?&gt;
&lt;button class="emerald-flat-button"
    style=" border-radius:6px; font-size:15px; padding-left:45px; padding-right:45px;"&gt;Quest- Quest1 (Finished)&lt;/button&gt;';
&lt;?php
}


Now, the code path is much more clear, and having done your conditionals this way, you can clearly see what might be problematic conditional logic in your current approach (what happens with certain values between 0 and 1?). What I have shown above obviously still needs to be cleaned up to the point where the design intent is made clear.  The general philosophical approach should be to make quick exits from functions/loops, etc. to enable you to de-nest your happy path code.  Also note that there are very few times where you really need an else condition.  If you have written an else, you may want to revisit your condition to see if there is a better way.

Note that you should also de-nest your very last conditional.  You have ~15 lines of code inside a conditional when it doesn't need to be.



Why so much inline styling?  If you have class names being used, why are these styles not attached to the classes in external CSS? This certainly makes code harder to read the way you have it. This along with perhaps getting to a better approach for templating your display code would server you well in getting to a higher quality code base.




onclick="window.location.href='../quete/vigier.php'"



Do you really need this in javascript as opposed to setting form's action or using a simple &lt;a&gt; styled similarly to button?

If you really need to apply javascript here, consider moving away from in-line usage.  Over time you will begin to realize that separation of application display and behavior code allows for a tremendous amount of code re-use.  Consider putting this in document.ready handler and applying it to element based on class or id.



You should be able to combine you to update queries against users table into a single query.



I don't know what DB class you are using, but it is unclear how you handle unexpected database interactions.  What happens is prepare fails? What happens if execution fails?  Are there any possible exceptions you should be catching or error handling that should be in place?  You code seem to really only consider "happy path".



I find it odd to have all you POST logic crammed way down at the bottom of the script after the point where you have already rendered output to the browser. Ideally, if you have a script which have different GET and POST logic, you would handle all the database interactions up front and make it much clearer earlier in the script whether the code is relevant to GET or POST.



Why would $findquetes be part of the conditional criteria for this POST logic?  This variable may or may not even be populated with a record.