Search code examples
phpmysqlecho

how can I select and display all the rows from the same user, underneath each other with php


Users can add their workouts into a database. There will have more than one workout per user. How can i display the workout names for the logged in user, underneath each other? I have no problem adding the workouts. When I echo the names of the added workouts, it display right next to each other like "workout1workout2". I want to be able to display the workouts underneath each other. I have no idea how to do it. It would be best if I could display each workout as a button.

$query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
$results = mysqli_query($db, $query);
while($data = mysqli_fetch_array($results)) {
    echo $data['workout'];
}

Solution

  • Firstly, you shouldn't put that username in query like that. Look into SQL Injection and how to avoid that in PHP.

    As for displaying buttons/workouts. You can embed HTML code inside the echo statement like below. You can add any html like that and it will render HTML.

       $query = "SELECT * FROM info WHERE username='$_SESSION[username]' ";
       $results = mysqli_query($db, $query);
       while($data = mysqli_fetch_array($results)) {
       echo "<button>$data['workout']</button></br>";
       }