dpnfxk251524 2017-12-20 08:47
浏览 34

php将child添加到parent中

I have this data in my database enter image description here

My Target is this to display that data in a table like this

enter image description here

I'm able to collect all of those data and display it in my HTML table but the things is I'm getting this.

enter image description here

I'm using array to get the user_id and the user type, and from that user ID i look for buyer, if got buyer under that ID, I display it, vise versa.

below are my code for your reference

$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>";

and the function

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>
        ";
    }}}

Please Help Me how can I achieve it, Thanks

  • 写回答

2条回答 默认 最新

  • drl37530 2017-12-20 08:59
    关注

    from

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

    to

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

    and before calling

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

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?