Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Help me echo an individual $row?

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #242640
    cscodismith
    Participant

    I am trying to make it so when I am changing a users admin level in the control panel I made it only targets the one that I chose (clicked on) but when the bootstrap modal pops up it is displaying every user that is not an admin instead of just the one I selected. Here is a screenshot of what I mean. So I would want it show ‘Changing admin level for lowheartrate’ instead of both users and obviously only change one users admin_level instead of both.

    My code currently looks like this:

    // users
    $sth2 = $conn->prepare("SELECT id, username, email, admin_level FROM users WHERE admin_level < 1");
    $sth2->execute();
    $users = $sth2->fetchAll();
    
    <!-- myModal -->
            <div class="modal fade" id="myModal" role="dialog">
                <div class="modal-dialog">
    
                  <!-- Modal content-->
                  <div class="modal-content">
                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal">×</button>
                       <?php
                       // Display username that we are changing!
                       foreach ($users as $row) {
                        echo '<p class="title">Changing admin level for ' . $row['username'] . '!</p>';
                       }
                       ?>
                       </p>
                    </div>
                    <div class="modal-body">
                      <?php
                      // Display current admin level
                      foreach ($users as $row) {
                        echo '<p class="title2">Current admin level is level ' . $row['admin_level'] . '. <br /><p class="title">Change to admin level:</p><br />
                        <input class="level-input" type="text" name="admin_level" placeholder="Change to level .... (ex: 1)" required/>
                        </p>';
                      }
                      ?>
                    </div>
                    <div class="modal-footer">
                      <button class="level-submit" type="submit" data-dismiss="modal">Submit Changes</button>
                    </div>
                  </div>   
                </div>
      </div>
    
    #242641
    rkieru
    Participant

    You need to review your SQL, because right now it’s doing exactly what it was written to do: Pull a list of users from the database where admin_level &lt; 1.

    In your post you reference that you are selecting some number of users, but your query does not provide any indication that this is ever being applied.

    #242642
    cscodismith
    Participant

    Yes the query I am using is correct for the admin panel because the module is echoing every single user that is admin_level &lt; 1 but I need to make new query now that only echo’s the id that I have clicked on? Not sure exactly how to make a query that will only show one specific users $row['username'] and $row['admin_level'] depending on which user I actually select.

    I know $_POST gets the variable that the user actually submits but seeming in my scenario I am trying to get the variable that I click on what kind of php would I use to fetch that in my foreach statement?

    Also, forgot to put in this code but this is what is linked to the ‘Change Level’ link in the website which is making me a bit confused as to how to query that I selected a specific user.

    foreach ($users as $row) {
        echo '<center>';
        echo '<tr>';
        echo '<td class="info">' . $row['id'] . '</td>';
        echo '<td class="info">' . $row['username'] . '</td>';
        echo '<td class="info">' . $row['email'] . '</td>';
        echo '<td class="info">' . $row['admin_level'] . '<br /><a data-toggle="modal" data-target="#myModal">Change Level</a></td>';
        echo '</tr>';
        echo '</center>';
    }
    

    That code is what also is displaying all of the users that are admin_level 0

    #242664
    rkieru
    Participant

    Assuming you are flagging multiple users for an admin_level change via something like a checkbox, you would have something like this:

    &lt;input type="checkbox" name="change_id[]" value="&lt;?php echo $row['id']; ?&gt;"&gt;

    The brackets after change_id indicate that this is an array, so now you have an array of user IDs that are subject to the change, under $_POST['change_id']

    From there you can use IN() to modify your ‘results’ query along the lines of:

    $sth2 = $conn-&gt;prepare("SELECT id, username, email, admin_level FROM users WHERE id IN(' . implode(",", $_POST['change_id']) .'");

    Note: For the purposes of sanitization you would likely first want to make sure that $_POST['change_id'] exists and is an array, as implode() will return an error otherwise.

    Resources:

    #242692
    cscodismith
    Participant

    For some reason when using the query you provided I am getting a syntax error:

    Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in A:\wamp\www\hn\index.php on line 25

    #242697
    rkieru
    Participant

    I believe I used single quotes and omitted a closing parenthesis in my outline earlier. Try something like this, with the caveat that you’ll obviously need to test it yourself, and adjust variable names as needed.

    $sth2 = $conn-&gt;prepare("SELECT id, username, email, admin_level FROM users WHERE id IN(" . implode(",", $_POST['change_id']) . ") );

    Remember – if you get an error like the one you posted above, see it as a learning experience in correcting syntax errors. It’s usually an issue of missing semi-colon or a parenthesis missing, etc. Learning to identify and correct these sort of issues will make you a better programmer.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.