Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

php nested loop help

Guest
Dec 23, 2010 Dec 23, 2010

Copy link to clipboard

Copied

query has columns id col1 col2 col3

I'm looping these results (do repeat region)

I need to create a sub loop where I list all the rows where col2 = col3

an example would be something like this:

id = unique int

col1 = y/n is main category

col2 = row category name

col3 = name of parent category

table data

id     col1          col2                    col3

1      Y              category 1           category 1

2      N              category 2           category 1

3      N              category 3           category 1

4      Y              category 4           category 4

5      N              category 5           category 4

6      N              category 6           category 4

output would be similar to this:

category 1

     category 2

     category 3

category 4

     category 5

     category 6

etc.

this seems pretty basic, but whatever I try I either breaks the logic, or I get a column missing in where clause error.  I believe this output can be achieved using one recordset, so I'd appreciate the help.  Looking for nested loops via google has not helped so far.

TOPICS
Server side applications

Views

545
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 23, 2010 Dec 23, 2010

Copy link to clipboard

Copied

You shouldn't need a nested loop for this. A single loop that just evaluates each row and determines whether to indent or not should suffice.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 23, 2010 Dec 23, 2010

Copy link to clipboard

Copied

make the assumption that the data is in a random order so it needs to loop head categories and sub loop matching categories and there be no duplicates.

i.e.

table data

id     col1          col2                    col3

1      Y              category 1           category 1

2      N              category 5           category 4

3      Y              category 4           category 4

4      N              category 3           category 1

5      N              category 2           category 1

6      N              category 6           category 4

i'd like code examples if possible.

for reference, the reason why I have to do this, I want the main category to be a trigger for a dropdown slider to reveal the subs.  so in the results they'd see all the main categories, and click to see the subs.  for that reason, i'm breaking it apart.

note:  http://www.dmxzone.com/go?4937

I did it this way which was my first thought doing this... didn't work.  I get Unknown column 'category 1' in 'where clause'  which doesn't make sense to me since this should work.  any thoughts on how to do this?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 23, 2010 Dec 23, 2010

Copy link to clipboard

Copied

LATEST

I'm starting to realize that perhaps my issue isn't the query or loops, but the slider.

I want the slider to contain the sub loop elements related to parent.  any recommendations on a slider that won't break with this loop - sub loop ?

update:  yes, this was the issue.  because the collapsible container div class was also getting repeated in the sub loop it was creating havoc.

FYI for anyone, that tutorial mentioned above has a sql syntax error.  You have to wrap the row value in the sub query in '  : for example  ' " . $row_menu['id'] . " '

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines