John Davidson

arrays - Displaying php mysqli query result with one to many relationship

0 comments
Message:


Current:
































Process Skill
Process A Skill_1
Process A Skill_2
Process A Skill_3
Process B Skill_1
Process B Skill_2


My Answer:
To display the PHP MySQLi query result with a one-to-many relationship, you can use an array to store the data and then loop through the array to display the results. Here's an example code snippet to demonstrate this:

php
// Assuming you have already executed the MySQLi query and stored the result in $result

// Initialize an empty array to store the data
$data = array();

// Loop through the query result and store the data in the array
while ($row = $result->fetch_assoc()) {
$process = $row['Process_Skill'];
$skill = $row['Skill'];

// Check if the process already exists in the array
if (!isset($data[$process])) {
$data[$process] = array();
}

// Add the skill to the process in the array
$data[$process][] = $skill;
}

// Loop through the array and display the results
foreach ($data as $process => $skills) {
echo "Process: $process
";
foreach ($skills as $index => $skill) {
echo "Skill " . ($index + 1) . ": $skill
";
}
echo "
";
}


In this code snippet, we first initialize an empty array `$data` to store the data. We then loop through the MySQLi query result and store the process and skill data in the array. Finally, we loop through the array and display the results, grouping the skills by process.

You can customize the output format as needed based on your requirements.

Rate this post

3 of 5 based on 2976 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us