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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:06:27+00:00 2026-06-15T09:06:27+00:00

I am working on a php project with MySQL as database. Here is the

  • 0

I am working on a php project with MySQL as database. Here is the table structure

product_id (PK)
shop_id (FK)
product_name
product_desc

Product_id is the primary key and shop_id is the foreign key (shows the product belongs to which shop). Now I want to select these product and display them in a <div>. For each shop products, I want to display a different <div>. e.g if there are three products say p1,p2 and p3, and they have same shop_id (say) 1, i want them to show in a separate <div>. there will be separate <div> for each shop_id, and all products belong to that shop_id will be displayed in that <div>. How can I do this with My_SQL query or using PHP. I have used a simple SELECT query like this but with this i could not find a way to initiate different <div> for separate shop.

$q = mysql_query("SELECT * FROM table");
echo "<div>";
while($product = mysql_fetch_assoc($q)){
echo $product['product_name']."<br/>";
}
echo "</div>";

It just throw all products in a single dive, I want separate div for separate shop (which contains all products of that shop). Thanks for reading..

  • 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-06-15T09:06:28+00:00Added an answer on June 15, 2026 at 9:06 am

    Simplest way is to group the products by id in a array and loop through them

    $all_products = array();
    while($product = mysql_fetch_assoc($q)){
        $all_products[$product['shop_id']][] = $product;
    }
    
    foreach($all_products as $shop_id => $product_array){
    echo "<div id='shop_".$shop_id."'>";
       foreach($product_array as $product){
          echo $product['product_name']."<br/>";
        }
    echo "</div>";
    }
    

    OR call product data fro each shop id

    $q = mysql_query("SELECT * FROM shops");
    
    while($shops = mysql_fetch_assoc($q)){
           $product_q = mysql_query("SELECT * FROM product_table WHERE shop_id=$shops['id']");
           echo "<div>";
           while($product = mysql_fetch_assoc($product_q)){
               echo $product['product_name']."<br/>";
               }
           echo "</div>";
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My team working on a php/MySQL website for a school project. I have a
I am working on a personal project based in PHP and MySQL, and I
I'm working on a project where there will be a MySQL database containing data
I am working on a c# project with a MySQL database. I have a
I am working on a Project management system on PHP (CodeIgniter framework), Mysql, AJAX,
I am currently working on a PHP project with an Oracle database. To update
I'm working on a php project which uses Mysql, javascrit and jquery as technologies.
I'm working on a php project based on Joomla! and i'm using Aptana. I'm
I am working for a PHP project that retrieves file meta information . All
I'm working on a PHP project with several developers, each who has their own

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.