php将child添加到parent中

问题描述 投票:1回答:2

我在我的数据库enter image description here中有这些数据

我的目标是在这样的表中显示该数据

enter image description here

我能够收集所有这些数据并将其显示在我的HTML表中,但事情是我得到了这个。

enter image description here

我正在使用数组来获取user_id和用户类型,并且从该用户ID我寻找买家,如果买了该ID,我会显示它,反之亦然。

以下是我的代码供您参考

$GparentType = $conn->query("SELECT type FROM profile WHERE user_id = '".$user_id."'")->fetch_object()->type;

$ParentDetails = array();
$sql = mysqli_query($conn,"SELECT user_id FROM profile WHERE Buy_From ='$user_id'");
while($resSql = mysqli_fetch_array($sql)){          
    $ParentDetails[] = $resSql["user_id"];
}

echo "<div id=\"container\" style=\"width:250px;margin:0 auto;\">
        <table class=\"table table-responsive table-hover\" border=\"1\">       
            <tr class=\"info\">
                <th style=\"width:20%;\">User</th>
                <th style=\"width:10%;\">Name</th>
                <!--th>Ordered</th>
                <th>Comm.&nbsp;</th>
                <th>Nett.&nbsp;&nbsp;&nbsp;</th>
                <th>Payout</th>
                <th>Balance</th-->                      
            </tr>
            <tr class=\"parent success\" data-toggle=\"collapse\" data-target=\"#child\" style=\"cursor:pointer\">                                  
                <td>".$user_id."<div style=\"float:right\">[".$GparentType."]</div></td>
                <td>".$uname."</td>
                <!--td id=\"total_order\"></td>  
                <td id=\"total_comm\"></td>  
                <td id=\"total_nett\"></td>  
                <td id=\"total_payout\"></td>  
                <td id=\"total_bal\"></td-->                                
            </tr>
            <tbody id=\"child\" class=\"collapse\">
            ";      
            getChild($ParentDetails);
            foreach($ParentDetails as $parent){

                $parentType = $conn->query("SELECT type FROM profile WHERE user_id = '".$parent."'")->fetch_object()->type;
                if($parentType == 'Retailer'){                      
                    $details = array();                     
                    $sql = mysqli_query($conn,"SELECT user_id,Buy_From FROM profile WHERE Buy_From ='$parent'");
                    while($resSql = mysqli_fetch_array($sql)){

                        $details[] = $resSql["user_id"].",".$resSql["Buy_From"];
                    }                       
                    getChild($details);                         
                }                   
            }   
echo "</tbody></table>";

和功能

function getChild($details){    
$rate = $_POST['rate'];
include("conf.php");
$conn = mysqli_connect($servername, $username, $password, "supplier"); 

foreach($details as $newDetails){       
    $expDetails = explode(",",$newDetails);

    $uType = $conn->query("SELECT type FROM profile WHERE user_id = '".$expDetails[0]."'")->fetch_object()->type;
    $uName = $conn->query("SELECT name FROM profile WHERE user_id = '".$expDetails[0]."'")->fetch_object()->name;

    if($uType == "Retailer"){
        echo "
            <tr class=\"danger\">
                <td>".$expDetails[0]."<div style=\"float:right\">[".$uType."]</div></td>
                <td>$uName</td>
            </tr>
        ";
    }
    else{
        echo "
            <tr>
                <td>".$expDetails[0]."<div style=\"float:right\">[".$uType."]</div></td>
                <td>$uName</td>                                         
            </tr>
        ";
    }}}

请帮助我如何实现它,谢谢

php html
2个回答
0
投票

   $details[] = $resSql["user_id"].",".$resSql["Buy_From"];

$details[$resSql["user_id"]] = $resSql["user_id"].",".$resSql["Buy_From"];

在打电话之前

getChild($details); to sort($details); getChild($details);

0
投票

谢谢大家,

在发生一些错误之后,我设法得到我想要的,我做的很简单,只需添加一个新函数,该函数将显示父节点(在本例中为Retailer)并在getChild中调用该函数($ details);这里我的代码是怎样的

$GparentType = $conn->query("SELECT type FROM profile WHERE user_id = '".$user_id."'")->fetch_object()->type;

$ParentDetails = array();
$sql = mysqli_query($conn,"SELECT user_id FROM profile WHERE Buy_From ='$user_id'");
while($resSql = mysqli_fetch_array($sql)){          
    $ParentDetails[] = $resSql["user_id"];
}

echo "<div id=\"container\" style=\"width:250px;margin:0 auto;\">
        <table class=\"table table-responsive table-hover\" border=\"1\">       
            <tr class=\"info\">
                <th style=\"width:20%;\">User</th>
                <th style=\"width:10%;\">Name</th>
                <!--th>Ordered</th>
                <th>Comm.&nbsp;</th>
                <th>Nett.&nbsp;&nbsp;&nbsp;</th>
                <th>Payout</th>
                <th>Balance</th-->                      
            </tr>
            <tr class=\"parent success\" data-toggle=\"collapse\" data-target=\"#child\" style=\"cursor:pointer\">                                  
                <td>".$user_id."<div style=\"float:right\">[".$GparentType."]</div></td>
                <td>".$uname."</td>
                <!--td id=\"total_order\"></td>  
                <td id=\"total_comm\"></td>  
                <td id=\"total_nett\"></td>  
                <td id=\"total_payout\"></td>  
                <td id=\"total_bal\"></td-->                                
            </tr>
            <tbody id=\"child\" class=\"collapse\">
            ";      
            getChild($ParentDetails);
            foreach($ParentDetails as $parent){

                $parentType = $conn->query("SELECT type FROM profile WHERE user_id = '".$parent."'")->fetch_object()->type;
                if($parentType == 'Retailer'){                      
                    $details = array();                     
                    $sql = mysqli_query($conn,"SELECT user_id,Buy_From FROM profile WHERE Buy_From ='$parent'");
                    while($resSql = mysqli_fetch_array($sql)){
                        $details[] = $resSql["user_id"].",".$resSql["Buy_From"];
                    }                       
                    getChild($details);                         
                }                   
            }   
echo "</tbody></table>";

function getParent($details){   
$rate = $_POST['rate'];
include("conf.php");
$conn = mysqli_connect($servername, $username, $password, "supplier"); 

foreach($details as $newDetails){       
    //echo $details."</br>";
    $expDetails = explode(",",$newDetails);     

    $uType = $conn->query("SELECT type FROM profile WHERE user_id = '".$expDetails[0]."'")->fetch_object()->type;
    $uName = $conn->query("SELECT name FROM profile WHERE user_id = '".$expDetails[0]."'")->fetch_object()->name;

    if($uType == "Retailer"){
        echo "
            <tr class=\"danger\" id=\"agent\">
                <td>".$expDetails[0]."<div style=\"float:right\">[".$uType."]</div></td>
                <td>$uName</td>
                <td id=\"subT_Ord\"></td>
                <td id=\"subT_Comm\"></td>
                <td id=\"subT_Nett\"></td>
                <td id=\"subT_Pay\"></td>
                <td id=\"subT_Bal\"></td>                               
            </tr>
        ";
    }
}}

这里是更改getChild()函数

function getChild($details){    
$rate = $_POST['rate'];
include("conf.php");
$conn = mysqli_connect($servername, $username, $password, "supplier"); 

$upline = array();
foreach($details as $newDetails){       
    //echo $newDetails."</br>";     
    $expDetails = explode(",",$newDetails);                 
    $upline[] = $expDetails[1];
}

if($upline){    
    $upline = array_unique($upline);
    getParent($upline);     
}

foreach($details as $newDetails){       
    $expDetails = explode(",",$newDetails);

    $uType = $conn->query("SELECT type FROM profile WHERE user_id = '".$expDetails[0]."'")->fetch_object()->type;
    $uName = $conn->query("SELECT name FROM profile WHERE user_id = '".$expDetails[0]."'")->fetch_object()->name;

    echo "
        <tr>
            <td>".$expDetails[0]."<div style=\"float:right\">[".$uType."]</div></td>
            <td>$uName</td>                                         
        </tr>
    ";
}}

我知道它不是Pro-Programmer代码,但现在我到达了我的目的地。再次感谢。

© www.soinside.com 2019 - 2024. All rights reserved.