How can PHP be used to display a hierarchical referral structure with multiple levels?
To display a hierarchical referral structure with multiple levels in PHP, you can use a recursive function to traverse through the levels of the structure and display each level accordingly. This function should check if a member has any referrals and if so, recursively call itself to display the referrals of that member.
function displayReferrals($member, $level = 0) {
echo str_repeat('-', $level) . $member['name'] . "<br>";
if(isset($member['referrals'])) {
foreach($member['referrals'] as $referral) {
displayReferrals($referral, $level + 1);
}
}
}
// Example hierarchical referral structure
$referralStructure = [
'name' => 'A',
'referrals' => [
[
'name' => 'B',
'referrals' => [
[
'name' => 'C',
'referrals' => []
],
[
'name' => 'D',
'referrals' => []
]
]
],
[
'name' => 'E',
'referrals' => []
]
]
];
// Display the hierarchical referral structure
displayReferrals($referralStructure);
Related Questions
- What is the purpose of using number codes for user rights in a MySQL database in PHP?
- How can PHP developers effectively debug issues related to undefined variables or indexes when working with radio buttons in a form?
- How can PHP developers ensure that the translation of query strings into SQL queries is done efficiently and accurately?