mysql - Return the team array in php in single function
what happens here is we find referral ids from database by step by step
here is the database
this is how i call my function
referralincome($uid, [], 1);
And here is the output. You can add pre tag to check
Array (
[0] => Array (
[0] => Array ( [uid] => 32143 [name] => Uzumaki Naruto )
[1] => Array ( [uid] => 32145 [name] => Sasuke Uchiha )
[2] => Array ( [uid] => 54321 [name] => Uzui Tengen )
)
[1] => Array (
[0] => Array ( [uid] => 42234 [name] => Tanjiro Kamado )
)
[2] => Array (
[0] => Array ( [uid] => 53523 [name] => Bakugo )
)
)
Array (
[0] => Array (
[0] => Array ( [uid] => 32143 [name] => Uzumaki Naruto )
[1] => Array ( [uid] => 32145 [name] => Sasuke Uchiha )
[2] => Array ( [uid] => 54321 [name] => Uzui Tengen )
)
[1] => Array (
[0] => Array ( [uid] => 42234 [name] => Tanjiro Kamado )
)
)
Array (
[0] => Array (
[0] => Array ( [uid] => 32143 [name] => Uzumaki Naruto )
[1] => Array ( [uid] => 32145 [name] => Sasuke Uchiha )
[2] => Array ( [uid] => 54321 [name] => Uzui Tengen )
)
[1] => Array (
[0] => Array ( [uid] => 43545 [name] => Mikey )
)
)
Array (
[0] => Array (
[0] => Array ( [uid] => 32143 [name] => Uzumaki Naruto )
[1] => Array ( [uid] => 32145 [name] => Sasuke Uchiha )
[2] => Array ( [uid] => 54321 [name] => Uzui Tengen )
)
)
This is the referral income function don't mind the name for now.
function referralincome($uid, $team, $level) {
global $conn;
$sql = "SELECT uid, name from users WHERE referral='$uid'";
$result = mysqli_query($conn, $sql);
$directs = [];
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
array_push($directs, $row);
}
array_push($team, $directs);
foreach ($directs as $row) {
referralincome($row['uid'], $team, $level+1);
$team;
}
print_r($team);
}
}
All i want is return $team once after all referrals completed someone please check it
I will also add MySQL import table code
CREATE TABLE `users` (
`sno` int(255) NOT NULL,
`date` date NOT NULL,
`referral` int(5) NOT NULL,
`uid` int(5) NOT NULL,
`password` int(4) NOT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`phone` int(255) NOT NULL,
`binance` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
INSERT INTO `users` (`sno`, `date`, `referral`, `uid`, `password`, `name`, `email`, `phone`, `binance`) VALUES
(1, '2022-08-31', 0, 12345, 7272, 'Luffy', 'lmoreno0@indiegogo.com', 2147483647, '0'),
(4, '2022-08-31', 12345, 32143, 7272, 'Uzumaki Naruto', 'lmoreno0@indiegogo.com', 2147483647, '0'),
(3, '2022-08-31', 12345, 32145, 7272, 'Sasuke Uchiha', 'lmoreno0@indiegogo.com', 2147483647, '0'),
(5, '2022-08-31', 32143, 42234, 7272, 'Tanjiro Kamado', 'lmoreno0@indiegogo.com', 2147483647, '0'),
(6, '2022-08-31', 54321, 43545, 7272, 'Mikey', 'lmoreno0@indiegogo.com', 2147483647, '0'),
(7, '2022-08-31', 42234, 53523, 7272, 'Bakugo', 'lmoreno0@indiegogo.com', 2147483647, '0'),
(2, '2022-08-31', 12345, 54321, 7272, 'Uzui Tengen', 'lmoreno0@indiegogo.com', 2147483647, '0');
ALTER TABLE `users`
ADD PRIMARY KEY (`uid`),
ADD UNIQUE KEY `sno` (`sno`);
Answer
Solution:
You should return the array from the function. And when you make the recursive call, append that result to the array you created from the current level.
There's also no need for $team
to be a function parameter, since you overwrite it in the function.
function referralincome($uid, $level) {
global $conn;
$sql = "SELECT uid, name from users WHERE referral=?";
$stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "i", $uid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$team = [];
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
array_push($team, $row);
}
array_push($team, $team);
foreach ($team as $row) {
$indirects = referralincome($row['uid'], $team, $level+1);
$team = array_merge($team, $indirects);
}
}
return $team;
}
print_r(referralincome($uid, 1));
Answer
Solution:
How about this simple one request, non-recursive solution ? I use the referral ID as the key in the resulting array.
<?
$sql = "SELECT * FROM users";
$result = mysqli_query($conn, $sql);
$team = [];
while ($row = mysqli_fetch_assoc($result))
$team[$row['referral']][] = array('uid' => $row['uid', 'name' => $row['name']);
print_r($team);
?>
which will produce :
Array (
[0] => Array (
[0] => Array ( [uid] => 12345 [name] => Luffy )
)
[12345] => Array (
[0] => Array ( [uid] => 32143 [name] => Uzumaki Naruto )
[1] => Array ( [uid] => 32145 [name] => Sasuke Uchiha )
[2] => Array ( [uid] => 54321 [name] => Uzui Tengen )
)
[32143] => Array (
[0] => Array ( [uid] => 42234 [name] => Tanjiro Kamado )
)
[42234] => Array (
[0] => Array ( [uid] => 53523 [name] => Bakugo )
)
[54321] => Array (
[0] => Array ( [uid] => 43545 [name] => Mikey )
)
)
if you really want sequential keys you'll only need a small modification.
Source