如何在PHP中构建引荐系统? [英] How can I build a referral system in PHP?
问题描述
我有一个带有推荐系统的网站,我希望用户能够使用树视图查看他们所推荐的用户.
设置我的数据库表,以便当用户使用其推荐代码推荐某人时, 新用户会获得一个ID,一个赞助商代码(来自他的发起人"的推荐代码,又称他为访问此网站的人)和一个推荐代码(他自己的推荐代码,以吸引其他人加入他的行列).>
我不知道如何从MySQL数据库中获取此信息,并导入到树视图脚本中.
我需要使用户能够看到他所介绍的所有人员(十层深).
这可能吗,我该怎么做?
您应该看一下分层数据( 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);
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?
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);
这篇关于如何在PHP中构建引荐系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!