0

Hope everyone has had a good new year

I am trying to echo a value from a loop however it just returns the last value of a for loop

while ($row = $stmt->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td><button type=\"button\" class=\"btn btn-info btn-sm\" data-toggle=\"modal\" data-target=\"#modal-username\">Username</button></td>";
    echo "<td>" . $row['password'] . "</td>"; // Don't worry the password is hashed
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['admin'] . "</td>";
    echo "</tr>";

    // Assign variables to use out of loop
    $id = $row['id'];
    $username = $row['username'];
    $password = $row['password'];
    $email = $row['email'];
    $admin = $row['admin'];
}

On my modal body is

<div class="modal-body">
    <p><?php echo $username; ?></p>
</div>

I've tried using .= instead of = but that just concatenates all the usernames together

I understand it is probably an easy fix and have tried looping through the variable assigning section but it just returns 0

Thanks in advance

10
  • @Rain Just to stress, that column was only there for me to test echoing rows into a table so it won't be included in the final part Commented Jan 3, 2020 at 0:47
  • 1
    Which username should be in the modal? Commented Jan 3, 2020 at 0:51
  • The first one in the db, however I tried all the buttons and the same result Commented Jan 3, 2020 at 0:51
  • You should clarify that in the question. Commented Jan 3, 2020 at 0:52
  • Im not, Im trying to display all the users so I can then use modals to edit them and the button brings up the modal with that username in Commented Jan 3, 2020 at 0:54

2 Answers 2

1

This is what i do in this kind of case.

change the loop button

while ($row = $stmt->fetch_assoc()) {
    echo "<tr>";
    echo "<td>" . $row['id'] . "</td>";
    echo "<td><button type=\"button\" class=\"btn btn-info btn-sm modal-trigger\" data-uname='" .  $row['username'] . "'>Username</button></td>";
    echo "<td>" . $row['password'] . "</td>"; // Don't worry the password is hashed
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['admin'] . "</td>";
    echo "</tr>";
}

and change modal to

<div class="modal-body">
    <p id="uname-select"></p>
</div>

jquery code

$('body').on("click", '.modal-trigger', function() {
    var GetUname = $(this).attr('data-uname');
    console.log('Selected Username: ' + GetUname);
    $('#uname-select').html(GetUname); // load the <p> tag with username selected
    $('.modal').modal(); // Make Modal & Show
});

give this a try..

Sign up to request clarification or add additional context in comments.

Comments

0

The $username is a variable an it can keep a value only
if you wants to keep all of usernames then should use Arrays

some thing like :

$id=$username=$password=$email=$admin=Array(); 

while ($row = $stmt->fetch_assoc()) {
    // Assign variables to use out of loop
    $id[] = $row['id'];
    $username[] = $row['username'];
    $password[] = $row['password'];
    $email[] = $row['email'];
    $admin[] = $row['admin'];
}

echo $username[0];// first username
echo $email[1];// second email

UPDATE

you can limit the query results like :

$result = mysqli_query($connection,'select * from tablename limit 20;');

if you wants to fetch some data without fix length :

$username=Array();

while ($row = $stmt->fetch_assoc()) {
    $username[] = $row['username'];
}

echo count($username);// how many? as integer

or after while loop you can use for loop

for ( $i=0; $i<count($username); $i++){

    echo $username[$i].'<br>';

}

UPDATE 2

if you try to store and show first username only then you can do :

while ($row = $stmt->fetch_assoc()) {
    if(!isset($username)){
        $username = $row['username'];
    }
}
echo $username;// first only

or some shorter :

while ($row = $stmt->fetch_assoc()) {
    $username = $username ?: $row['username'];
}
echo $username;// first only

2 Comments

Hi, I don't know how many rows there are since the database could be expanding etc. Is there any way to implement that when you don't know how many rows there are?
you can limit results in database query also may you set order , although may you want fetch some data that have not a fix lengh , i will update the answer

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.