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 1039699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:07:57+00:00 2026-05-16T15:07:57+00:00

that´s my first question in stackoverflow . I have two MYSQL tables: categories and

  • 0

that´s my first question in stackoverflow.

I have two MYSQL tables: categories and products. I manage the query results with a while loop in PHP to group every category with its products. It´s the first time I do something so, and I think I made it very “crafty/hardcoded” (Sorry for my English). I think that should be a better way to do this. So, I´d like to ask to professional programmers. Here is my code:

 CREATE TABLE IF NOT EXISTS `categories` (
 `id` int(11) NOT NULL auto_increment,
 `name` text NOT NULL,
 PRIMARY KEY  (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;


 CREATE TABLE IF NOT EXISTS `products` (
 `id` int(11) NOT NULL auto_increment,
 `name` text NOT NULL,
 `description` text NOT NULL,
 `category` int(11) NOT NULL,
 `photo` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=33 ;

I have a query that returns products relationed with their category:

SELECT categories.name as category,products.name as product
FROM categories
INNER JOIN products ON(categories.id = products.category)

With PHP I manage the results to create an unordered list for every category with its products:

<?php
$category = '';//A control variable

while($row = mysql_fetch_array($query))
{
    //if its a new category I start a new <ul>
    if($row['category'] != $category)
    {
        //If it´s not the firt category I need to close previous one
        if($category != '')
        {
            echo "</ul>\n";
        }
        //I start the new <ul>
        echo '<h2>' . $row['category'] . '</h2>';
        echo '<ul>';
    }

    //I create the correspondient <li>
    echo '<li>' . $row['product'] . "</li>\n";

    //Asign the value of actual category for the next time in the loop
    $category = $row['category'];

}

//I neeed to close last <ul>
echo "</ul>\n";
?>

Is there a better way to do this operation?
Thanks in advance for your answers!

  • 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-16T15:07:57+00:00Added an answer on May 16, 2026 at 3:07 pm

    It’s always best not to mix HTML with PHP.

    Since you wanted my advice (veteran PHP coder of some 12 years), I’ll just code it from scratch real fast:

    <?php
    $pdo = new PDO($dsn, $user, $pass);
    
    $sql = "SELECT categories.name as category,products.name as product
    FROM categories
    INNER JOIN products ON(categories.id = products.category)";
    
    $stmt = $pdo->prepare($sql);
    $stmt->execute();
    
    $categories = array();
    while (($row = $stmt->fetch(PDO::FETCH_ASSOC)))
    {
        $category = $row['category'];
        $categories[$category][] = $row['product'];
    }
    
    // In your view:
    ?>
    <html>
        <body>
    <?php
        foreach ($categories as $category => $products)
        {
    ?>
            <h2><?php echo $category; ?></h2>
            <ul class="products">
    <?php
            foreach ($products as $product)
            {
    ?>
                <li><?php echo $product; ?></li>
    <?php
            }
    ?>
            </ul>
    <?php
        }
    ?>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.