gpt4 book ai didi

php - 如何根据级别数计算结果是放在 TreeView 中的单词?

转载 作者:行者123 更新时间:2023-11-30 21:26:31 25 4
gpt4 key购买 nike

我有一个问题要计算树的每一层。例如,我在 TreeView 的第一行,所以我是 1 级,我的下线是 stand level 2,我下线的下线是 stand level 3 等等。我的问题是如何将每个级别的编号更改作为单词放入我的 TreeView 中。

下面是我的代码:

    <?php


$sql_select = 'SELECT * FROM users WHERE id = ' . $user_id;
$query_select = db_conn_select($sql_select);
$i=1;
$level= 'Level';
foreach ($query_select as $rs_select) {
$email2 = $rs_select['email'];
$name2 = $rs_select['name'];
}

$lang = $_COOKIE["Language"];

require_once("language/lang_team_" . $lang . ".php");
?>




<!-- Level tree -->
<div class="row">
<!-- block -->
<div class="col-lg-12">
<div class="box">
<header>
<h5><?php echo $language["LIST_TITLE"]; ?>:</h5>
<!-- .toolbar -->
<!--div class="toolbar">
<nav style="padding: 8px;">
<a href="javascript:;" class="btn btn-default btn-xs collapse-box">
<i class="fa fa-minus"></i>
</a>
</nav>
</div--><!-- /.toolbar -->
</header><br><br>
<div class="block">

<div class="block-content collapse in">
<div class="span6">
<?php
$sql = "select * from level_tree lt JOIN users u ON lt.user_id = u.id where lt.referal_id =" . $user_id . ' and lt.level=1';
$query = mysql_query($sql);
if (mysql_num_rows($query) > 0) {
$select_name = 'SELECT * FROM users WHERE id = ' . $user_id;
$query_select = db_conn_select($select_name);
foreach ($query_select as $rs_select) {
$name = $rs_select['name'];
$email = $rs_select['email'];
$email_arr = explode('@', $rs_select['email']);
$email_length = strlen($email_arr[0]);
$email_first = substr($email_arr[0], 0, 1);
$email_last = substr($email_arr[0], -1, 1);
}
$email_address = '';
for ($i = 0; $i < $email_length; $i++) {
if ($i == 0) {
$email_address.=$email_first;
} elseif ($i == $email_length - 1) {
$email_address.=$email_last;
} else {
$email_address.='*';
}
}
$email_user = $email_address . '@' . $email_arr[1];


?>
<div id="jstree" style="font-size:15px;">
<ul>
<li><b><?php echo "<b>&nbsp;&nbsp;&nbsp;".$level." ".$i.": &nbsp;".$name2."&nbsp;(".$email2.")"; ?></b></li>

<ul>
<?php
while ($rs = mysql_fetch_array($query)) {
$email_address = '';
$email_arr = explode('@', $rs['email']);
$email_length = strlen($email_arr[0]);
$email_first = substr($email_arr[0], 0, 1);
$email_last = substr($email_arr[0], -1, 1);
for ($i = 0; $i < $email_length; $i++) {
if ($i == 0) {
$email_address.=$email_first;
} elseif ($i == $email_length - 1) {
$email_address.=$email_last;
} else {
$email_address.='*';
}
}
$email_user = $email_address . '@' . $email_arr[1];
echo "<li><b><img src='images/down_right_arrow.png' />".$level."&nbsp".$i.":&nbsp;".$rs['name']."&nbsp;(".$email_user.")";
// echo "<li><b>" . $email_user . "";
downline_list($rs['id']);
echo "</b></li>";
}
?>
</div>
<?php
} else {
// echo $email2;
//echo "<b>&nbsp;&nbsp;&nbsp;".$name2."&nbsp;(".$email2.")";
echo '<div style="font-size:15px;color:black;font-weight:bold;"><img src="images/down_right_arrow.png /><span style="font-size:15px;color:black;font-weight:bold;">&nbsp;&nbsp;&nbsp;'.$name2.'&nbsp;('.$email2.')</span></div>';
echo "<br />";

}

function downline_list($id) {
$sql = "select lt.user_id,lt.referal_id,lt.`level`,u.email,u.name from level_tree lt JOIN users u ON lt.user_id = u.id where lt.referal_id =" . $id . " and u.is_active=1 and lt.level=1";
$query = mysql_query($sql);
if (mysql_num_rows($query)) {
echo "<ul>";
while ($rs = mysql_fetch_array($query)) {
$email_address = '';
$email_arr = explode('@', $rs['email']);
$email_length = strlen($email_arr[0]);
$email_first = substr($email_arr[0], 0, 1);
$email_last = substr($email_arr[0], -1, 1);
$level = 'Level';
for ($i = 0; $i < $email_length; $i++) {
if ($i == 0) {
$email_address.=$email_first;
} elseif ($i == $email_length - 1) {
$email_address.=$email_last;
} else {
$email_address.='*';
}
}
$email_user = $email_address . '@' . $email_arr[1];
echo "<li><b><img src='images/down_right_arrow.png' />".$level."&nbsp;".$i.":&nbsp; ".$rs['name']."&nbsp;(".$email_user.")";
// echo "<li><b>" . $email_user;
downline_list($rs['user_id']);
echo "</b></li>";
}
echo "</ul>";
}
$i++;
}
?>
</ul></div>
</div>
</div>
</div>
</div>
<!-- /block -->
</div>

<script src="plugins/jstree/dist/jquery-1.10.2.min.js"></script>
<link rel="stylesheet" href="plugins/jstree/dist/themes/default/style.min.css" />
<script src="plugins/jstree/dist/jstree.min.js"></script>
<script>
$(function() {
// 6 create an instance when the DOM is ready
$('#jstree').bind("ready.jstree", function() {
$('#jstree').jstree('open_all');
}).jstree();

});
</script>
<style type="text/css">
.jstree li > a > .jstree-icon { display:none !important; }
</style>

我的输出显示在下面的图片中,它可以将数字显示为单词,但是在每个级别中获取的数字都是错误的:

Level Screenshot 1

其实我想要的是下图这样的输出结果,number的level是按照level来计算的:

Level Screenshot 2

我被困在这个问题上几天了,希望有人能帮我解决这个大问题。谢谢。

最佳答案

您的代码是关于以深度优先搜索 (DFS) 方式探索树并在这种搜索中找到每个节点的级别(当然,您也可以使用不同的方法以广度优先搜索方式进行) .因此,您应该递归调用downline_list 函数并将当前节点的级别作为参数发送。在主要的时间内,您处于一级,并且通过递归调用该函数,每次递归调用时级别都会增加一级。当您探索树的 $level 层时,您调用 downline_list 函数来查找当前节点的子节点,直接子节点将在 $ level + 1 等等。所以你有这样的东西:

   /// Main while loop 
while ($rs = mysql_fetch_array($query)) {
// ...
downline_list($rs['id'], 1);
// ...
}

function downline_list($id, $level)
{
//...
//Nodes here are in level $level
//but the children of this node would be in level $level + 1
downline_list($rs['user_id'], $level + 1);
//...
}

关于php - 如何根据级别数计算结果是放在 TreeView 中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58695730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com