gpt4 book ai didi

php - 如何选择家长的 child 号码?

转载 作者:行者123 更新时间:2023-11-29 10:59:45 26 4
gpt4 key购买 nike

我有一个表,其中有子项和父项,父项的 pid 为 0,子项的 pid 为父项的 id。我想选择父级及其子级数。

我的模型:

    function parent_child()
{


$row1 =
"SELECT tbl_test.ID,tbl_user.Name,tbl_user.FamilyName,
tbl_test.Name
from tbl_test,tbl_user
WHERE tbl_user.UID=tbl_test.UserID
AND tbl_test.PID=0
AND tbl_test.Status=0
";
$parent=$this->db->query($row1)->result();
foreach ($parent as $row)
{
$row1 =
"SELECT COUNT(ID)
from tbl_test
WHERE tbl_test.PID IN $row->ID ";
$parent['childcount']=$this->db->query($row1)->result();

return $parent;
}

我的 Controller :

public function parent_child()
{


$this->load->model('test');
$temp=$this->test->parent_child();
$output= json_encode($temp);

die($output);

}

我想要这个输出

{
"parent": {
"ID": "2",
"childcount": 3

}
}

最佳答案

试试这个

public function parent_child()
{

$st=$this->db->select('SELECT tbl_test.ID, tbl_test.Name, tbl_user.Name, tbl_user.FamilyName from
tbl_test
JOIN
tbl_user on tbl_user.UID=tbl_test.UserID
WHERE
tbl_test.PID=0 AND
tbl_test.Status=0')->result_array();
if(count($st)>0)
{
for($i=0;$i<count($st);$i++)
{
$rows=$this->db->select('*')->from('tbl_test')->WHERE('PID',$st[$i]['id'])->get()->result_array();
$st[$i]['childcount']=count($rows);
}
return $st;
}
else
{
return array();
}
}

关于php - 如何选择家长的 child 号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466358/

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