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', '[email protected]', 2147483647, '0'),
(4, '2022-08-31', 12345, 32143, 7272, 'Uzumaki Naruto', '[email protected]', 2147483647, '0'),
(3, '2022-08-31', 12345, 32145, 7272, 'Sasuke Uchiha', '[email protected]', 2147483647, '0'),
(5, '2022-08-31', 32143, 42234, 7272, 'Tanjiro Kamado', '[email protected]', 2147483647, '0'),
(6, '2022-08-31', 54321, 43545, 7272, 'Mikey', '[email protected]', 2147483647, '0'),
(7, '2022-08-31', 42234, 53523, 7272, 'Bakugo', '[email protected]', 2147483647, '0'),
(2, '2022-08-31', 12345, 54321, 7272, 'Uzui Tengen', '[email protected]', 2147483647, '0');
ALTER TABLE `users`
ADD PRIMARY KEY (`uid`),
ADD UNIQUE KEY `sno` (`sno`);
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));
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.