473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to create html list from such array ?

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
7 2222
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
8
by: ASP Yaboh | last post by:
I have an ArrayList of data gathered from a database. I want to create a web page from this data by creating a <table>, each cell in each row displays the appropriate data. One of those cells in...
18
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt...
4
by: sandeep | last post by:
When we use STL which memory space it will use whither it is stack or heap or data segment How to make STL to create in heap? How to make whole container to create in heap? I think container...
10
by: Generale Cluster | last post by:
Hello, I have the following situation: $list is an array of MyElement objects. MyElement has two members: MyElement->member1; MyElement->member2; What I want is to get the following: ...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.