You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

技术求助:如何在多层次营销(MLM)中展示二级与三级推荐人(已实现一级推荐人展示)

实现MLM二级与三级推荐人展示方案

嘿,你已经搞定一级推荐人展示了,这步走得很稳!要扩展到二级和三级其实就是逐层向下查询推荐关系的事儿,我给你梳理个清晰的实现方案,还顺便帮你修复了原代码里的SQL注入风险(这块可不能马虎)。

核心思路

你的现有代码是查询当前用户($cle)作为推荐人的所有直接被推荐人(一级)。那二级推荐人就是这些一级推荐人的直接被推荐人,三级则是二级推荐人的直接被推荐人——说白了就是逐层递归查询推荐关系链。

第一步:封装复用的查询函数

先写一个通用函数来获取某用户的所有直接推荐人,这样一级、二级、三级都能复用,代码更整洁:

/**
 * 获取指定用户的直接推荐人列表
 * @param PDO $db 数据库连接对象
 * @param int $parentId 推荐人ID
 * @return array 推荐人列表
 */
function getDirectReferrals($db, $parentId) {
    // 使用预处理语句避免SQL注入!
    $stmt = $db->prepare("SELECT * FROM parrain WHERE idParrain = :parentId");
    $stmt->bindParam(':parentId', $parentId, PDO::PARAM_INT);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

第二步:逐层获取各级推荐人

现在我们可以用这个函数分别获取一级、二级、三级推荐人:

// 当前用户ID
$currentUserId = $cle;

// 一级推荐人(直接推荐)
$level1Referrals = getDirectReferrals($db, $currentUserId);

// 二级推荐人(一级推荐人的直接推荐)
$level2Referrals = [];
foreach ($level1Referrals as $referral) {
    // 假设被推荐人的ID字段是idFilleul,根据你的表结构调整!
    $level2Referrals = array_merge($level2Referrals, getDirectReferrals($db, $referral['idFilleul']));
}

// 三级推荐人(二级推荐人的直接推荐)
$level3Referrals = [];
foreach ($level2Referrals as $referral) {
    $level3Referrals = array_merge($level3Referrals, getDirectReferrals($db, $referral['idFilleul']));
}

注意:如果你的parrain表中被推荐人的ID字段不是idFilleul,一定要换成你实际的字段名(比如userId之类的)。

第三步:分层次展示推荐人

现在把各级推荐人按层次展示,用标题区分层级,空数据时显示提示:

<!-- 一级推荐人 -->
<h5>Premier niveau</h5>
<?php if (empty($level1Referrals)): ?>
    <p class="alert alert-danger">Vous n'avez pas encore de filleul</p>
<?php else: ?>
    <?php foreach ($level1Referrals as $referral): ?>
        <div class="row">
            <div class="d-flex key-feature align-items-center p-3 rounded shadow mt-4">
                <div class="flex-1 content ms-3">
                    <h4 class="title mb-0">
                        <?= htmlspecialchars($referral['prenom']) ?> <?= htmlspecialchars($referral['nom']) ?>
                    </h4>
                    <p class="text-muted mb-0">
                        <?= htmlspecialchars($referral['email']) ?>
                    </p>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

<!-- 二级推荐人 -->
<h5 class="mt-5">Deuxième niveau</h5>
<?php if (empty($level2Referrals)): ?>
    <p class="alert alert-warning">Aucun filleul au deuxième niveau</p>
<?php else: ?>
    <?php foreach ($level2Referrals as $referral): ?>
        <div class="row ms-4"> <!-- 用margin-left区分层级 -->
            <div class="d-flex key-feature align-items-center p-3 rounded shadow mt-4">
                <div class="flex-1 content ms-3">
                    <h4 class="title mb-0">
                        <?= htmlspecialchars($referral['prenom']) ?> <?= htmlspecialchars($referral['nom']) ?>
                    </h4>
                    <p class="text-muted mb-0">
                        <?= htmlspecialchars($referral['email']) ?>
                    </p>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

<!-- 三级推荐人 -->
<h5 class="mt-5">Troisième niveau</h5>
<?php if (empty($level3Referrals)): ?>
    <p class="alert alert-warning">Aucun filleul au troisième niveau</p>
<?php else: ?>
    <?php foreach ($level3Referrals as $referral): ?>
        <div class="row ms-8"> <!-- 更大的margin区分层级 -->
            <div class="d-flex key-feature align-items-center p-3 rounded shadow mt-4">
                <div class="flex-1 content ms-3">
                    <h4 class="title mb-0">
                        <?= htmlspecialchars($referral['prenom']) ?> <?= htmlspecialchars($referral['nom']) ?>
                    </h4>
                    <p class="text-muted mb-0">
                        <?= htmlspecialchars($referral['email']) ?>
                    </p>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

关键优化点

  1. SQL注入防护:原代码直接拼接$cle到SQL中,存在严重安全风险,我换成了PDO预处理语句,一定要坚持这么做!
  2. XSS防护:用htmlspecialchars()转义输出的用户信息,防止跨站脚本攻击。
  3. 层级区分:通过ms-4ms-8的margin样式让不同层级的推荐人视觉上更清晰,你也可以用边框、图标等方式优化。

如果你的数据库结构和我假设的不一样(比如用户信息存在单独的users表,parrain表只存推荐关系),可以告诉我具体结构,我再帮你调整查询语句!

内容的提问来源于stack exchange,提问作者Damien le Bodoul

火山引擎 最新活动