问题
I have a website with a referal system, and I want the users to be able to see what users they referred, using a treeview.
My database table is set up so that when a user refers somebody with his referal code, the new users gets an ID, a sponsor code (the referal code from his "sponser" aka the person who got him into this site) and a referal code (his own referal code to get other people to join under him).
I have no idea how i can get this info out of my MySQL database, and into a treeview script.
I would need to be able to let the user see all the people that he referred, 10 levels deep.
Is this possible and how could I do that?
回答1:
You should give a look at hierarchical data (http://www.sitepoint.com/hierarchical-data-database/)
<?php
function tree_view($index)
{
$q = mysql_query("SELECT * FROM table_name WHERE SCode=$index");
if (!mysql_num_rows($q))
return;
echo '<ul>';
while ($arr = mysql_fetch_assoc($q))
{
echo '<li>';
echo $arr['UserID']; //you can add another output there
tree_view($arr['RCode']);
echo '</li>';
}
echo '</ul>';
}
mysql_connect('localhost', 'root', '');
$link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
tree_view(11111);
来源:https://stackoverflow.com/questions/7149060/how-can-i-build-a-referral-system-in-php