php将子项添加到父项

时间:2017-12-20 08:47:14

标签: php html

我的数据库中有这些数据 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>
        ";
    }}}

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

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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代码,但现在我到达了我的目的地。 再次感谢。