Results 1 to 5 of 5
  1. #1
    itsnexgen is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default Generate <UL> list from Database with sub categories

    Hello,

    I am facing difficulties to generate subcategories in <UL> list.

    I have a table with Menu Items :
    Java Code:
    cat_id | parent_id | title
    -------------------------
    1 | 0 | Solutions
    2 | 0 | Products
    3 | 1 | Web
    4 | 1 | Development
    5 | 2 | Softies
    6 | 2 | PHP Book
    -------------------------
    I want to list get the out put in the form as below :

    Java Code:
    Solutions
      Web
      Development
    Products
      Softies 
      PHP Book
    What i have written is :
    Java Code:
    public  String generateMenuHTML(String id1) throws SQLException {
    		pdb = new Pdb();
    		String sql = "select ID,PARENT_ID,TITLE,URL,SORT,ICON from menu";
    		rset = pdb.executeQuery(sql);
    		
    	    html = "<ul id='menu" + id1 + "'>";
    
    	    while(rset.next())
    	    {
    			 id = rset.getString("ID");
    			 parent_id = rset.getString("PARENT_ID");
    			 title = rset.getString("TITLE");
    			 sort = rset.getString("SORT");
    			 url = rset.getString("URL");
    			 icon = rset.getString("ICON");
    	         html = html + "<li id='it" + id + "'><a href='" + url + "'>" + title + "</a>";
    	         if (parent_id.equalsIgnoreCase("1"))
    		      {
    		    	  System.out.println(parent_id);
    		    	  html = html +generateMenuHTML(id) ;
    
    		      }
    	      html = html + "</li>";
    	    }	  
    	    html = html + "</ul>";
    		
            return html;
    	}
    Could you please help me how to retrieve sub categories and get the output in :

    <ul>
    <li>
    <ul>
    </ul>
    </li>
    </ul>

    Kindly suggest me how to go about..

    Thanks and Rgds,
    Pradeep

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Smile Partial solution,.....

    Java Code:
    public String generateMenuHTML(String id1) throws SQLException {
        StringBuffer html = new SringBuffer();
        while(rset.next())
        {
            html.append("<blockquote>");
            html.append("<ul id=\""+ id1 +"\ >");
            String cat_id = rset.getString("CATALOG_ID");
            String parent_id = rset.getString("PARENT_ID");
            String title = rset.getString("TITLE");
            String sort = rset.getString("SORT");
            String url = rset.getString("URL");
            String icon = rset.getString("ICON");
            // begin building string....
            html.append("<li>" +cat_id+   "</li>");//
            html.append("<li>" +parent_id+ "</li>");//
            html.append("<li>" +sort+      "</li>");//
            html.append("<li>" +id+        "</li>");//
            html.append("</ul></blockquote>");
        }
    // .........
        return html.toString();
    }
    This is only to elicit further information, I have had great success using blockquote to get pleasing indentation, I find ol does not behave in an orderly fashion. DT / DL also work well. I skipped several details pending further info.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    itsnexgen is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks for reply..

    But for me to work.. output should be something like..

    Java Code:
    <ul id='menu0'><li id='it1'><a href='SomeURL1'>Solution</a></li><li id='it2'><a href='SomeURL2'>712</a><ul><li id='it3'><a href='SomeURL3'>XX</a></ul></li></ul>
    <ul id='menu0'> initially.. then it should take from the cat_id itself.

    Thanks again..

    Rgds,
    Pradeep

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Default

    Can we make cat_id a file scope class ( default access ) on in some way put all the cat_id stuff in one place?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    itsnexgen is offline Member
    Join Date
    Sep 2008
    Posts
    9
    Rep Power
    0

    Default

    SOrry for delayed reply as i was sick.

    coming aback to the problem - I am tryin to retrieve data in JSP as :

    Java Code:
    <c:forEach var="section" items="${menu}">
    	    <ul id="menu">
    	      <li id="${section.id}"><a href="${section.url}">${section.title}</a>
    	          <ul id="section">
    	              <c:forEach var="item" items="${section.menuItems}">
    	                  <li id="${item.id}"><a href="${item.url}">${item.title}</a></li>
        	          </c:forEach>
    	          </ul>
    	      </li>
    	    </ul>
    	</c:forEach>
    But i am not getting the value from database in JSP. But in tomcat logs if i take System.out.println it gets the value.

    i see below display in JSP file :

    Java Code:
    ${section.title} 
          ${item.title} 
    ${section.title} 
    ${section.title}
    Kindly help me if u can.. thanks!

Similar Threads

  1. How to generate primary key in EJB
    By naresh_m in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-17-2008, 09:41 AM
  2. how to generate dynamic pdf
    By valery in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 10:01 PM
  3. how to generate xml with Dom4j
    By leonard in forum XML
    Replies: 1
    Last Post: 08-06-2007, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •