Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with query

Posted on 2006-06-10
4
Medium Priority
?
43 Views
Last Modified: 2013-12-12
PHP and SQL

Hello all.  I am hoping for some help in determining the needed SQL and best way to create a resulting html SELECT input.

Details:

- I need to create an html select that contains a treelike category structure.
- I only want the categories to be in the select if there are files with the category id
- I want the select to demonstrate the level. For example:
Cat 1
  Sub Cat 1
    Sub Sub Cat 1
  Sub Cat 2
Cat 2

Here are the details on the tables/fields
tblCategory
  CategoryID (int pk)
  CategoryName (varchar)
  TopLevelFlag (tinyint)
  ParentCategoryID (int)

tblFileCategory
  FileID
  CategoryID

Additional Notes:
Some categories do not directly have files associated. For example
Living Accomodations
  Home
    Small
      [Small contains 1 file]
    Large
  Apartment
    [Apartment contains 5 files]
  Condo

Please help me come up with the sql and method that creates the options for the select. It will be fun :).  I will award points quickly. I have to have this completed by tomorrow night.  Thanks EE!

Best,
kerkor
0
Comment
Question by:kerkor
4 Comments
 
LVL 3

Accepted Solution

by:
NewJorg earned 2000 total points
ID: 16895081
Hallo,

maybe it's to late for you but here my answer. (I'm using the adodb for database connection)

<?php

function load_menu ($db)
{
        $GLOBALS['menu'] = Array();

        $query = "SELECT a.CategoryID,CategoryName,ParentCategoryID,COUNT(FileID) CountObjects FROM tblCategory a LEFT JOIN tblFileCategory b ON a.CategoryID = b.CategoryID GROUP BY a.CategoryID";
        $db->SetFetchMode(ADODB_FETCH_ASSOC);
        $rs = $db->Execute($query);
        while(!$rs->EOF)
        {
                $GLOBALS['menu'][$rs->fields['CategoryID']] = $rs->fields;
                $rs->MoveNext();
        }

}
function my_menu ($pid=0, $level=0)
{
        // need a local copy for using foreach
        $menu_local = $GLOBALS['menu'];
        $html = "";
        foreach($menu_local as $node)
        {
                if($node['ParentCategoryID'] == $pid)
                {
                        $submenu=my_menu($node['CategoryID'], $level+1);
                        if(!empty($submenu) || $node['CountObjects'] > 0)
                        {
                                $html.=sprintf("<option>%s%s</option>\n",
                                        str_repeat('&nbsp;', $level),
                                        $node['CategoryName']);
                                $html.=$submenu;
                        }
                }
        }
        return $html;
}

// $db is my database connection
load_menu($db);

echo "<select>\n";
echo my_menu();
echo "</select>\n";

?>
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 16920749
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question