1

I'm having trouble doing simple maths (that will get more complicated) in a dynamic table. Every time i submit the results, it should add up and store the total score in the $players[$player]['score'] however once its cycled once it resets to zero again.
Any ideas where i'm going wrong?

    $new_players = $_SESSION["players"]; 
    //$_SESSION["players"] - brings in array an array like following 

    /* array (size=1)
      'John' => 
        array (size=3)
          'previous' => int 0
          'score' => int 0
          'hand' => int 0  */

    if (empty($players)) {//added as i thought $new_players = $_SESSION["players"]; might be blanking scores
        $players = $new_players;
    } 

if (isset($_POST[$submit])) {
    $new_array = $_POST['player'];
     foreach ($players as $player =>$i) {
        $players[$player]['hand'] = $new_array[$player]+0;//add to convert the $new_array[$player] to int
        $players[$player]['previous'] = $players[$player]['hand'];
        $players[$player]['score'] = $players[$player]['score'] + $players[$player]['hand'];
      }
  }
    ?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="formAcc">
    <ul style= "text-align: center">

    <article>
    <li>
    <label>Current Standing</label>
    <?php
      echo '<table id="db_results">
      <colgroup>
        <col class="col15" />
        <col class="col15" />
        <col class="col15" />
        <col class="col15" />
      </colgroup>
    <tr>
    <th>Name</th>
    <th>Previous Score</th>
    <th>Current Score</th>
    <th>Hand</th>
    </tr>';

    foreach ($players as $player =>$i) {
        echo "<tr class=\"center\">
            <td>". $player . "</td>
            <td>". $i['previous'] . "</td>
            <td>". $i['score'] . "</td>"
            . '<td><input type="text" name="player['.$player.']" value="0"/></td>
            </tr>';
            }
    echo "</table>"; 
    ?>            

<input type="submit" name="<?php echo $submit?>" value="Input Scores" style="margin-top: 5px;"/>

            </li>
        </article>

        </ul>
    </form>
7
  • what do you mean by cycled ? Commented Feb 6, 2019 at 16:50
  • @saibbyweb i just mean once i hit submit button. The results display in the table. Then i enter new input and only the new input is in the total Commented Feb 6, 2019 at 16:52
  • what is the value of $submit ? Commented Feb 6, 2019 at 16:59
  • Simply 'add_scores' Commented Feb 6, 2019 at 17:02
  • 1
    Are you starting session by session_start()? Commented Feb 6, 2019 at 17:12

1 Answer 1

1

You are not saving the changes to the $_SESSION variable when submit is clicked, try this for submit click handler :

if (isset($_POST[$submit])) {
    $new_array = $_POST['player'];
     foreach ($players as $player =>$i) {
        $players[$player]['hand'] = $new_array[$player]+0;//add to convert the $new_array[$player] to int
        $players[$player]['previous'] = $players[$player]['hand'];
        $players[$player]['score'] = $players[$player]['score'] + $players[$player]['hand'];
      }
      /* overwrite $_SESSION['players'] */
      $_SESSION['players'] = $players;
  }
Sign up to request clarification or add additional context in comments.

1 Comment

I think you have hit the nail on the head they're @saibbyweb. So simple yes I missed it I will try it as soon as I get home thank you :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.