/    Sign up×
Community /Pin to ProfileBookmark

dynamic table

Hi!
I need to create a dynamic html table with php function but I’m in trouble.
I have a table in my db with some records, some og them are father and other are children: for children I have parent_id field that indicate father id.
Html table is characterize by a first line with <td> tag (many as father), and a second line with children at the same <td> tag of father.
Something like this:

FATHER1 FATHER2 FATHER3
CHILD1_1 CHILD2_1 CHILD3_1
CHILD1_2 CHILD2_2 CHILD3_2

I create query to get father, but I can’t manage cycle to obtain my result, can you help me please?
Thanks!

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 20.2010 — It's hard to tell for sure without details on the data hierarchy, and also if you need to deal with a "childless father". In general, though, I might consider building up an array with the necessary data, something like:
[code=php]
$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'
)
)
);
[/code]

Then in your HTML that defines your <table>, you could loop through that array to output the table cells, something like:
[code=php]
foreach($data as $father) {
echo "<td>";
echo "<p class='father'>{$father['name']}</p>n";
foreach($father['children'] as $child) {
echo "<p class='child'>$child</p>n";
}
echo "</td>n";
}
[/code]
Copy linkTweet thisAlerts:
@ojal__authorSep 21.2010 — Thanks to reply me!I post my code..can you help me to modify query to create and array like yours please?



function dinamyctable(){

$query=DB::query("SELECT * FROM table where ParentID='0'"); //cerco i padri

$record=array();

while ($record[]=$query->nextRecord())

{

}

$ch=array();

for($i=0;$i<(count($record)-1);$i++)

{ $q=DB::query("SELECT * FROM table where ParentID='{$record[$i]['ID']}'"); //creco i figli

while ($ch[]=$q->nextRecord())

{

}

$doSet = new DataObjectSet();

for($x=0;$x<count($ch);$x++)

{ if($ch[$x]['ID']!='')

{

$result = array(

'father_name' => $record[$x]['father_name'],

'child_name' => $ch[$x]['child_name'],

);

$doSet->push(new ArrayData($result));

} }

}

return $doSet;

and html..

<table border="1" cellpadding="0" cellspacing="0" width="100%">

<% control dinamyctable(1) %>

<tr><td width="14%" valign="top">

$father_name

</td></tr>

<tr><td width="14%" valign="top">

$child_name

</td></tr>

<% end_control %>

</table>
×

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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...