By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,405 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,405 IT Pros & Developers. It's quick & easy.

How to create html list from such array ?

P: n/a
lp
I've got an example array like this:

$myArr = array(
array("jj", "0", "jjj"),
array("ee", "0", "eee"),
array("bb", "ee", "bbb"),
array("ll", "ee", "lll"),
array("ff", "0", "fff"),
)

Where each row is an array with columns: id of list element, id of parent
list element ("0" means main node) and content of the element. So, from my
array I'd like to create a list like this:

<ul>
<li>jjj</li>
<li>eee
<ul>
<li>bbb</li>
<li>eee</li>
</ul>
</li>
<li>fff</li>
<ul>

Could anybody help me ?

Best regards.
LP
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
lp пишет:
I've got an example array like this:

$myArr = array(
array("jj", "0", "jjj"),
array("ee", "0", "eee"),
array("bb", "ee", "bbb"),
array("ll", "ee", "lll"),
array("ff", "0", "fff"),
)

Where each row is an array with columns: id of list element, id of parent
list element ("0" means main node) and content of the element. So, from my
array I'd like to create a list like this:

<ul>
<li>jjj</li>
<li>eee
<ul>
<li>bbb</li>
<li>eee</li>
</ul>
</li>
<li>fff</li>
<ul>

Could anybody help me ?

Best regards.
LP


Look's like a usual tree. Try to use not array, but SQL DB, it's easy
Jul 17 '05 #2

P: n/a
lp
Ivan Omelchenko 608308824 wrote:
Look's like a usual tree. Try to use not array, but SQL DB, it's easy


Sorry, but I can't use SQL here, the data deas not come from database. I've
got only array like that which will be passed to my class.

LP
Jul 17 '05 #3

P: n/a
lp пишет:
Ivan Omelchenko 608308824 wrote:
Look's like a usual tree. Try to use not array, but SQL DB, it's easy

Sorry, but I can't use SQL here, the data deas not come from database. I've
got only array like that which will be passed to my class.

LP

okay, it's worstly, but also real
you have to use 'usort' function that
looking for a $array[2] - (keys parent or not) and sorting an array.
It's only idea, not realization.

P.s sorry for my bad english
Jul 17 '05 #4

P: n/a
lp пишет:
Ivan Omelchenko 608308824 wrote:
Look's like a usual tree. Try to use not array, but SQL DB, it's easy

Sorry, but I can't use SQL here, the data deas not come from database. I've
got only array like that which will be passed to my class.

LP

more little question: is you array sorted correctly?
if your array has
$myArr = array(
array("jj", "0", "jjj"),
array("bb", "ee", "bbb"),
array("aa", "0", "fff"),
)

then HTML should be

<ul>
<li>jjj</li>
<ul>
<li>bbb</li>
</ul>
</li>
<li>fff</li>
<ul>

or it have to be

<ul>
<li>fff</li>
<li>jjj</li>
<ul>
<li>bbb</li>
</ul>
<ul>

(sorted by name of parent keys)
Jul 17 '05 #5

P: n/a
lp
Ivan Omelchenko 608308824 wrote:
more little question: is you array sorted correctly?
if your array has
$myArr = array(
array("jj", "0", "jjj"),
array("bb", "ee", "bbb"),
array("aa", "0", "fff"),
)


This array ^ is wrong. There can't be any row with idParent = 'ee' if
there's no row with id = 'ee'.

Regards,
LB
Jul 17 '05 #6

P: n/a
lp пишет:
Ivan Omelchenko 608308824 wrote:
more little question: is you array sorted correctly?
if your array has
$myArr = array(
array("jj", "0", "jjj"),
array("bb", "ee", "bbb"),
array("aa", "0", "fff"),
)

This array ^ is wrong. There can't be any row with idParent = 'ee' if
there's no row with id = 'ee'.

Regards,
LB

I mean
$myArr = array( array("jj", "0", "jjj"),
array("bb", "jj", "bbb"),
array("aa", "0", "fff"),
)

Jul 17 '05 #7

P: n/a
On Mon, 30 May 2005 13:29:31 +0200, lp wrote:
$myArr = array(
array("jj", "0", "jjj"),
array("ee", "0", "eee"),
array("bb", "ee", "bbb"),
array("ll", "ee", "lll"),
array("ff", "0", "fff"),
)

<ul>
<li>jjj</li>
<li>eee
<ul>
<li>bbb</li>
<li>eee</li>
</ul>
</li>
<li>fff</li>
<ul>


How about

$html = "<ul>\n";
$prv = -1;

foreach ( $myArr as $a )
{
$cur = $a[1];
$dat = "\t<li>{$a[2]}";

if ( $prv !== -1 )
if ( strcmp( $cur, '0' ) == 0 )
{
$dat = "</li>\n$dat";
if ( strcmp( $prv, '0' ) != 0 )
$dat = "\t\t</ul>\n\t$dat";
}
else
{
$dat = "\t\t$dat</li>\n";
if ( strcmp( $prv, '0' ) == 0 )
$dat = "\n\t\t<ul>\n$dat";
}

$html .= $dat;
$prv = $cur;
}

if ( strcmp( $prv, '0' ) != 0 )
$html .= "\t\t</ul>\n\t";
$html .= "</li>\n</ul>\n";
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.