Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (120 points)

My table 2 have a few values in the same column but different rows and I would like to display my result as 1 line in a page instead of multiple lines.

Currently it is displayed as :

FormA, UserA, phone number
FormA, UserA, email
FormA, UserB, phone number
FormA, UserB, email
FormB, UserA, phone number
FormB, UserA, email
FormB, UserB, phone number
FormB, UserB, email

How I want it to display :

FormA
UserA, phone number, email, date and other values that is associated with this user
UserB, phone number, email, date and other values that is associated with this user

FormB
UserA, phone number, email, date and other values that is associated with this user
UserB, phone number, email, date and other values that is associated with this user

Below are my database structure from some wordpress plugin :

Table 1

form_id | form_name
-------------------
1       | FormA
2       | FormB
3       | FormC

Table 2

form_id | user  | value
----------------------
1       | userA | phone number
1       | userA | email
1       | userA | date
1       | userA | sometext
1       | userB | phone number
1       | userB | sometextA
1       | userB | sometextB
2       | userA | phone number
2       | userA | email
2       | userA | sometext
2       | userB | phone number
2       | userB | email
2       | userB | sometextA
2       | userB | sometextB

Currently below is my code :

$sql = "SELECT wppj_rm_forms.form_name, wppj_rm_submission_fields.value
FROM wppj_rm_forms
RIGHT JOIN wppj_rm_submission_fields ON wppj_rm_forms.form_id = wppj_rm_submission_fields.form_id
ORDER BY wppj_rm_forms.form_id;";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Form Name: " . $row["form_name"]. "<br>" . "Users: " . $row["user_name"] . "<br>" . "Details: " . $row["value"] . "<br>";
    }
} else {
    echo "0 results";
}

Please log in or register to answer this question.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+3 votes
1 answer
asked Jul 3, 2019 in SQL by Tech4ever (20.3k points)
+3 votes
1 answer
asked Jul 3, 2019 in SQL by Tech4ever (20.3k points)

Browse Categories

...