Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 602575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:50:15+00:00 2026-05-13T16:50:15+00:00

So I have a loop to be nested inside another loop based on two

  • 0

So I have a loop to be nested inside another loop based on two queries. I have the first loop working fine-

$sql_categories = mysql_query("SELECT * FROM $categories_table");

$results = mysql_query("SELECT * FROM $events_table");
    while ($num_rows = mysql_fetch_assoc($sql_categories)) {
        extract($num_rows);
        echo "<h2>$category_name</h2>";
        // Begin second loop to output events
        while/for(not sure) {

    }
}

I want to output into the second loop all the $vars for the corresponding $category_id. In the second query, the matching value is $event_category_id.

I don’t know if that makes sense, but what I’m trying to get is basically–

<h2>Category One</h2>
Event Name
Event Name
Event Name

<h2>Category Two</h2>
Event Name
Event Name
Event Name

etc. where the “Event Name” corresponds to the “Category Name”

The two tables I’m working with look like this-

CREATE TABLE `wp_wild_dbem_categories` (
  `category_id` int(11) NOT NULL auto_increment,
  `category_name` tinytext NOT NULL,
  PRIMARY KEY  (`category_id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

CREATE TABLE `wp_wild_dbem_events` (
  `event_id` mediumint(9) NOT NULL auto_increment,
  `event_author` mediumint(9) default NULL,
  `event_name` tinytext NOT NULL,
  `event_start_time` time NOT NULL default '00:00:00',
  `event_end_time` time NOT NULL default '00:00:00',
  `event_start_date` date NOT NULL default '0000-00-00',
  `event_end_date` date default NULL,
  `event_notes` text,
  `event_rsvp` tinyint(1) NOT NULL default '0',
  `event_seats` tinyint(4) default NULL,
  `event_contactperson_id` mediumint(9) default NULL,
  `location_id` mediumint(9) NOT NULL default '0',
  `recurrence_id` mediumint(9) default NULL,
  `event_category_id` int(11) default NULL,
  UNIQUE KEY `event_id` (`event_id`)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=latin1

Thanks for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:50:15+00:00Added an answer on May 13, 2026 at 4:50 pm

    You need to do the second query inside the while loop for it to have any meaningful effect:

    $sql_categories = mysql_query("SELECT * FROM $categories_table");
    
    while($category = mysql_fetch_assoc($sql_categories)) {    
        extract($category);    
        $events = mysql_query("SELECT * FROM $events_table WHERE event_category_id = '".mysql_real_escape_string($category_id)."'");
    
        echo "<h2>$category_name</h2>";    
        while($event = mysql_fetch_assoc($events) {
            extract($category);
            echo "<p>$event_name</p>";
        }
    }
    

    This should get you where you want, but note that this is not the optimal way to do things. You should first get all of the events, build an array of them indexed by event_category_id and loop that array inside your while loop. This is because now you are doing one extra query per each category whereas only two queries in total should suffice.

    But perhaps this would get you started on that then.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 423k
  • Answers 423k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here are some resources to get you started: jQuery and… May 15, 2026 at 11:42 am
  • Editorial Team
    Editorial Team added an answer Since this changes in an event context (points to global… May 15, 2026 at 11:42 am
  • Editorial Team
    Editorial Team added an answer The easiest way that I know to do something like… May 15, 2026 at 11:42 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.