Page 1 of 1

Help with array/query

Posted: Tue Jul 11, 2006 6:07 am
by sejf83
Hi all,

I have a perplexing problem and I'm hoping someone can give me a tip that points me in the right direction.

I need to create a hierarchal list for some data and display it in a series of <ul> HTML lists. The query will return an array that looks like this:

array("cat1", "cat2", "info", "info2", "info3")

I need to somehow cycle through this array and output HTML that looks like this:

cat1
<ul>
cat2
<li> info, info2, info2</li>
</ul>

In other words, there will be a number of different cat1 and cat2 values, and I need to place the correct "info" into the cat1/cat2 it is associated with. The cat1 and cat2 combos will need to be dynamically generated.

Thanks for any insight.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.

Posted: Tue Jul 11, 2006 7:18 am
by Oren
Well your example:

Code: Select all

cat1
<ul>
cat2
<li> info, info2, info2</li>
</ul> 
Does not make sense to me, but anyways, I can point you to the right direction... Try to do whatever you are trying to do in the query level - perhaps more than one query?

Posted: Tue Jul 11, 2006 7:33 am
by sejf83
Thanks. I am sorry I wasn't clear. Let me try a better explanation.

I am trying to create a page that lists a bunch of file names. These files are assigned into a top-level folder and then a sub-folder. (In my previous example, the top-level folder is cat1 and the sub-folder is cat2).

What I need to do is create a new unordered list for each top-level folder, and then a nested unordered list for each sub-folder within it. Then, I need to output as list items the names of the files within each sub-folder.

So, what I will have is this (pardon my poorly written HTML):

Code: Select all

Top-level 1
<ul>
subfolder 1
<ul>
<li>File 1</li>
<li>File 2</li>
<li>File 3</li>
</ul>
subfolder 2
<ul>
<li>File 3</li>
<li>File 4</li>
<li>File 5</li>
</ul>
</ul>
Top-level 2
<ul>
subfolder 3
<ul>
<li>File 6</li>
<li>File 7</li>
<li>File 8</li>
</ul>
subfolder 4
<ul>
<li>File 9</li>
<li>File 10</li>
<li>File 11</li>
</ul>
</ul>
So, should I run a query for all of the top-level folder names, another query for the associated subfolder names, and then a third query for all of the file names?

Posted: Tue Jul 11, 2006 7:50 am
by Oren
Post the table(s) you have in your database.

Posted: Tue Jul 11, 2006 7:54 am
by jamiel
Your array you are getting will be useless for that unless there is a way to differentiate a folder from a file etc. You ideally will need a multi-dimensional array returned.

Code: Select all

array( 
    0 => array(
                   'folder1' => array(
                                      'file1', 'file2', 'file3'
                                 )
                   )
         );
If you run a query for all three as you asked, you will still need a way to link all three to get them in the right order.

Alternatively your first query can get an array with all the Top levels. Then nest a foreach loop to get the subfolders for that Top Level and then another nest for the Files.

Posted: Tue Jul 11, 2006 8:02 am
by sejf83
I haven't yet created a table in my DB to house the top-level/sub-folder information as I'm not sure how to do it.

So..I'd need to run a second query inside the foreach loop? How do I create the subarray?