Results 1 to 5 of 5
  1. #1
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Is there a way to create a nested query?

    I want to basically create a left nav menu based on Category and Subcat.

    I need to first query the db for distince categories then againg to pull distinct subcats of a category.

    I sure there is a way I am just kinda new to PHP.

    Thanks,
    DOug
    Thanks,
    Doug

  2. #2
    ABW Ambassador
    Join Date
    January 18th, 2005
    Posts
    2,419
    Use joins when retrieving the results. Depends on your tables and how you want to output but the basic sql would look something like :

    select a.category_name, b.subcategory_name
    from category a, sub-categpory b
    where a.category_id = b.category_id
    order by a.category_name, b.subcategory_name

    (assuming you have a unique id on a category stored in the subcategory table) If no id and the category name is repeated in the subcat table then change the query as needed

    The pph module could then inspect when a new category is obtained and do processing as needed: IE:

    $result = mysql_query($query);
    $num_results = mysql_affected_rows();
    $LastCategory="";
    for ($ndx=0; $ndx <$num_results; $ndx++)
    {
    $row = mysql_fetch_array($result);
    $CategoryName = $row['category_name'];
    // ...
    if ($CategoryName != $LastCategory)
    {
    // output processing for new category
    // ...
    $LastCategory = $CategoryName
    }
    // do processing for subcategory
    // ...
    }

  3. #3
    ABW Veteran Student Heyder's Avatar
    Join Date
    January 18th, 2005
    Posts
    5,482
    Wow there is a hint of brilliance in this dark corner of the web today.

  4. #4
    ABW Ambassador Doug247's Avatar
    Join Date
    January 18th, 2005
    Location
    DE USA
    Posts
    931
    Happy...all the data is in one table. How is that handled?

    THanks,
    DOug
    Thanks,
    Doug

  5. #5
    ABW Ambassador
    Join Date
    January 18th, 2005
    Posts
    2,419
    Your sql changes as follows:

    select category_name, subcategory_name
    from table_name
    order by category_name, subcategory_name

    The basic php processing loop remains the same.

  6. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Nested IF statements -why don't they work?
    By sotonman in forum WebMerge (Fourthworld.com)
    Replies: 6
    Last Post: August 16th, 2006, 06:55 AM
  2. Nested Tag Replacement...????
    By AddHandler in forum Programming / Datafeeds / Tools
    Replies: 3
    Last Post: August 13th, 2006, 06:36 PM
  3. Frank - question on nested tutorial
    By zimmy in forum WebMerge (Fourthworld.com)
    Replies: 7
    Last Post: May 19th, 2004, 03:50 PM
  4. Nested Tables Question
    By ~Michelle in forum WebMerge (Fourthworld.com)
    Replies: 6
    Last Post: January 19th, 2004, 12:32 PM
  5. Nested IF statement is it possible in new or old version?
    By phpking in forum WebMerge (Fourthworld.com)
    Replies: 2
    Last Post: August 30th, 2003, 07:00 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
  •