[Resolved] Populating a 2-level navmenu from 2 mysql tables

Codes here !

Moderators: egami, macek, gesf

Post Reply
Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Wed Jul 16, 2003 3:01 am

Your problem is in your loop. Notice how many times its repeating, it's repeating the SECTION stuff as many times as their is categories in that section. I had 2 tables like this...

Table: Sections

section_id | section
1 | Section 1
2 | Section 2
3 | Section 3

Table: Categories

cat_id | section_id | category
1 | 1 | Category 1
2 | 2 | Category 2 (in section 2)
3 | 1 | Cat 3
4 | 3 | Cat 4 (in section 3)

Then I had a loop within a loop, with 2 queries. Like so

Code: Select all

$sect_sql = mysql_query("SELECT * FROM sections ORDER BY section");

while ($sect_result = mysql_fetch_assoc($sect_sql)) {
     $sect_id = $sect_result['sect_id'];
     echo "<b>".$sect_result['section']."</b><br>";
     $cat_sql = mysql_query("SELECT * FROM categories WHERE cat_id = $cat_id ORDER BY category");
    while ($cat_result = mysql_fetch_assoc($cat_sql) {
           echo $cat_result['category']."<br>";
     }
}




I'm sure joins and Group by could make this more efficient, but I'm much too tired for more thinking right now, hope I helped, which I think may be unlikely for what I just did.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 2 guests