데이터베이스에 테이블이 있고이 테이블에 i, name, parent_id.please라는 3 개의 열이 추가되었습니다.PHP를 사용하여 테이블에서 계층 구조 만들기
ID | name | parent_id
1 name1 0
2 name2 1
3 name3 1
이제 데이터베이스에서이 ID를 가져 오려고합니다. 나는 PHP에서 메소드를 만들고 데이터베이스에서 하나씩 데이터를 가져온다. 내가 그
Array(
[0]=>name1
)
처럼 돌아갑니다
$this->getData(3); // has a parent
같은 ID 3이 메소드를 호출하면 이제
function getData($id)
{
$array = array();
$db = JFactory::getDBO();
$sql = "SELECT * from table_name when id = ".$id;
$db>setQuery($sql);
$fetchAllDatas = $db->getObjectList();
foreach ($fetchAllDatas as $fetchAllData)
{
if($fetchAllData->parent_id > 0)
{
$array[$fetchAllData->parent_id] = $fetchAllData->name;
$this->getData($fetchAllData->parent_id);
}
else
{
$array[$fetchAllData->parent_id] = $fetchAllData->name;
}
}
return $array;
}
아래를 참조하지만 난 싶지하십시오 같은
Array(
[1]=>name3,
[0]=>name1
)
아래
나는 배열을 재정의했다는 것을 안다 if 우리에게는 부모가 있지만 어떻게 관리 할 수 있습니까? array_push PHP 함수를 사용했지만 내 조건에서 작동하지 않습니다.
'SELECT * from table_name id = some_id'는이 쿼리가 작동합니까 ??? –