Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    I am building a complex menu structure from data in a DB. In order to build the menu, the data has to come out of the DB in a specific order. So in order to do this I have to write a number of queries to get the data into the correct order. (All the data comes from the same table in the DB)

    Now what I need help with is, I want to store all the querie's results into 1 array or resultset so I can work with it in a logical order.

    Is this possible? Would I declare a multi-dimension array or can I declare a resultset and just add to it? How would I do this?

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Quote Originally Posted by jbagley
    I am building a complex menu structure from data in a DB. In order to build the menu, the data has to come out of the DB in a specific order. So in order to do this I have to write a number of queries to get the data into the correct order. (All the data comes from the same table in the DB)
    Why do you have to build multiple queries? Is there no way you make the query do the sorting for you (alphabetically, by number, by topic, whatever).

    When iterating though the resultset, you can dump the values into an array. If you need data in the array to be sub-categorized, you can use a multidimentional array.

    It's, however, difficult to suggest anything unless you can provide an example of what your table (and data) looks like, and how you want to sort it. (What you want the end-resulting array to look like)
    S. Rosland

  4. #3
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    Well, im designing a dynamic menu structure. So a person can add, edit and delete menu items as they please.

    The table has the following columns:

    ID - of course
    title - the links title
    sublevel - has the link got a submenu?
    active - must i disply the link?
    sortorder - self explanatory
    link - the URL
    parentID - the ID of the parent link.

    for example:

    news > world
    > finance > money
    > more money
    sport
    weather

    So when you go over the news link, world and finance will appear as a submenu, then if you mouse over finance it will open up money and more money etc etc

    In order to build the menu it has to follow sequence in order to generate the HTML....

    So the links have to come out of the DB in this order:
    1. News
    2. World
    3. Finance
    4. Money
    5. More money
    6. Sport
    7. Weather

    But if I have inactive links or links in different sort orders the links wont come out in the specific order and the menu structure will break.

    Soooo, is it possible to feed my queries into an array and then pull them out one by one as I am building the menu?

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Not sure what you have in the column 'sublevel'. Is it an 'enum' (Y/N), or does it contain an id-pointer to the sub link-title?
    'Sortorder' what does that hold. What in the menu or sub menu structure does that relate to?

    Wouldn't it be better to seperate the info into multiple relational tables instead?
    I.e. A top_level, sub1, sub2, sub3, sub4.
    The links in sub4 contains a sub3-id referance column, sub3 contains a sub2-id referance, etc.
    Then you can use a join query to link it all together. The order of added entries wouldn't matter, as the internal relations between table entries maintains the hierarchical order of the menu.
    S. Rosland

  6. #5
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    sublevel is an 'enum' - which tells me that the link does or doesnt have a sub-level.

    sortorder is the order in which the links are displayed. So you can change the order of the top level links, as well as the sub level etc.

    I could have a DB with multiple relational tables, but the problem there is, what if the user wants to create a 6 level hierarchy? Most unlikely, but it is possible.

  7. #6
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I would go for a multi table solution, as it would be easier to maintain, but for the heck of it:

    If I understand correctly, a three item menu with two sublevels, would create the following rows in your database:

    Ex:
    news
    sport->baseball->clubs
    weather


    Rows:
    id| title | sublevel | active | sortorder | link | parentID
    1 | news | N | Y | 1 | http://..| none
    2 | sport | Y | Y | 2 | http://..| none
    3 | weather | N | Y | 3 | http://..| none
    4 | baseball| Y | Y | 1 | http://..| 2
    5 | clubs | N | Y | 2 | http://..| 4

    Is this correct?

    And you would want an array holding the data in this order (?):
    1. news
    2. sport
    3. baseball
    4. clubs
    5. weather
    S. Rosland

  8. #7
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    The parent id will work very well, actually - it's by far the most flexible.


    Give me a while to think about it... there is a simple solution, I just don't know how quite yet =/ I can do it using XML easy lol.
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Heeey, interesting!

    Maybe you should post your XML solution here.
    XML is anyway the most flexible way of organizing these types of data.

    Any other types of ideas (with regard to the original question) is interesting as well.

    I'm in the same sand box. I've done equivalent PHP versions of data sorting from arbitrary SQL returns, I just need to adjust previous solutions to this specific problem.
    On the XML side, I have poor real-time experience. I know (tried) PHP5 has some very simple interface with regard to parsing XML data in a presentable fashion. Then again, I know you can embed a lot of interactive tools in the XML sheet itself (sortable data, layout features etc), but this is beyond my skills for the time beeing.
    S. Rosland

  10. #9
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Okay, so we have a programatic solution at work that spits out an XML navigation tree (for example):

    Code:
        <nav>
          <nav.item sort="1" type="section" class="explorecarneros" text="Explore Carneros" active="true" selected="false" link="/explore/">
            <nav.item sort="1" type="section" class="carnerosappellation" text="Carneros Appellation" active="true" selected="false" link="/explore/carnerosappellation/" />
            <nav.item sort="2" type="section" class="buenavistaestate" text="Buena Vista Estate" active="true" selected="false" link="/explore/buenavistaestate/" />
            <nav.item sort="3" type="section" class="estatevineyards" text="Estate Vineyards" active="true" selected="false" link="/explore/estatevineyards/" />
          </nav.item>
          <nav.item sort="3" type="section" class="artisanwinemaking" text="Artisan Winemaking" active="true" selected="false" link="/winemaking/">
            <nav.item sort="1" type="section" class="winemaker" text="Winemaker" active="true" selected="false" link="/winemaking/winemaker/" />
            <nav.item sort="2" type="section" class="winemakingprocessred" text="Winemaking Process: Red" active="true" selected="false" link="/winemaking/winemakingprocessred/" />
            <nav.item sort="3" type="section" class="winemakingprocesswhite" text="Winemaking Process: White" active="true" selected="false" link="/winemaking/winemakingprocesswhite/" />
          </nav.item>
        </nav>
    I then, use XSL to my advantage and use a simple, recursive parsing template (simplified):
    Code:
    	<xsl:template match="nav.item" mode="mainnav">
    		<li>
    			<xsl:choose>
    				</xsl:when>
    				<xsl:when test="../@selected='true' and position()=last()">
    					<xsl:attribute name="class">subnav <xsl:value-of select="@class" /> last</xsl:attribute>
    				</xsl:when>
    				<xsl:otherwise>
    					<xsl:attribute name="class"><xsl:value-of select="@class" /></xsl:attribute>
    				</xsl:otherwise>
    
    			</xsl:choose>
    			<a href="{@link}"><xsl:value-of select="@text" /></a>
    		</li>
    		<xsl:if test="@selected='true' and not(../@selected='true')">
    			<xsl:apply-templates select="nav.item" mode="mainnav" />
    		</xsl:if>
    	</xsl:template>
    So, it spits it out again real quick like.

    Now, creating this tree with PHP would be pretty easy using the DOM extension. Just use a simple Xpath query (like //nav.item[id='$parentid'] ) to look for your parent element, use the addChild and so on. The problem is, it becomes pretty heavy code to just make something this simple, and it would be actually faster to use multiple queries. But, if I can just think of some simple array parsing, it would be better.
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  11. #10
    Senior Member jbagley's Avatar
    Join Date
    Sep 2004
    Location
    Cape Town
    Posts
    845
    Member #
    7422
    To reply to Rosland, That is correct. That is how the data should come out of the database.

    To Brak, you have completely lost me Im not too familiar with XML or even XSL. I understand the XML that you posted, what does that tree hierarchy look like in graphical terms?

    Ok, I had another look at the XSL, it does make semse. All you really doing is looping through the XML, and printing the correct navbar element at its specific place. Is this correct?

    But back to me... Should I be trying to do it in XML?


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:16 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com