/    Sign up×
Community /Pin to ProfileBookmark

Hi!
I have 2 query
The first show father list:

select id, father_name from sitetree where ShowInMenus=’1′

and this show for each father, his children:

SELECT id, child_name FROM sitetree where ParentID='{father_id}’

I neww build an array like this..

$data = array(
$father_id_1 => array(
‘name’ => ‘Father_1’,
‘children’ => array(
$child_id_1_1 => ‘Child_1_1’,
$child_id_1_2 => ‘Child_1_2’
)
),
$father_id_2 => array(
‘name’ => ‘Father_2’,
‘children’ => array(
$child_id_2_1 => ‘Child_2_1’,
$child_id_2_2 => ‘Child_2_2’
)
)
);

can yuo help me create php code to build this array?thanks!

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 05.2010 — Untested (big surprise):
[code=php]
$sql = "
SELECT
t1.id AS father_id,
t1.father_name,
t2.id AS child_id,
t2.child_name
FROM sitetree AS t1
LEFT JOIN sitetree AS t2 ON t2.ParentID = father_id
WHERE ShowInMenus = 1
";
$result = mysql_query($sql) or die(mysql_error());
$data = array();
while($row = mysql_fetch_assoc($result)) {
$data[$row['father_id']]['name'] = $row['father_name'];
if(!empty($row['child_id']) && strtoupper($row['child_id']) != 'NULL') {
$data[$row['father_id']]['children'][$row['child_id']] = $row['child_name'];
}
}
[/code]
×

Success!

Help @ojal__ spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...