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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:01:09+00:00 2026-06-13T21:01:09+00:00

I have two tables. Each product could have one or more files. But each

  • 0

I have two tables. Each product could have one or more files. But each file must belong to one product.

**PRODUCTS**
ID     PRODUCT_CODE     DESCRIPTION
1      a1               small
2      a2               big

**PRODUCT_FILES**
ID     PRODUCT_ID       FILE_PATH     FILE_NAME
1      1                x.pdf         x
2      1                y.pdf         y
3      2                z.pdf         z

I want to have a table which includes

PRODUCT_ID, PRODUCT_CODE, DESCRIPTION, FILE_NAME(s), FILE PATH(s)

My query was;

$query = $this->db->query
("
    SELECT products.*,product_files.file_name
    FROM products 
    INNER JOIN product_files 
    ON products.id = product_files.product_id
    GROUP BY products.id
");

$i = 0;

while($row = $query->fetch_assoc())
{           
$details[]  = array
(
    'code' => $row['code'], 
    'description' => $row['description'],
    'file_name' => $row['file_name'],
    'file_path' => $row['file_path']
);
}

return $details;

And my php was;

<?php foreach($details  AS $product) { ?>
<tr>    
     <td><?php echo $product['code']; ?></td>
     <td><?php echo $product['description']; ?></td>
     <td><a href="files/<?php echo $product['file_path']; ?>">
     <?php echo $product['file_name']; ?></a></td>
</tr>
<?php } ?>

But with this query and php, product row is duplicating which product has more than one file like;

a1   small   x   x.pdf
a1   small   y   y.pdf
a2   big     z   z.pdf

And than i tried another query with same php code;

$query = $this->db->query
("
SELECT products.*,
GROUP_CONCAT(DISTINCT product_files.file_name SEPARATOR ',') 
AS file_name,product_files.file_path
FROM products 
INNER JOIN product_files 
ON products.id = product_files.product_id
GROUP BY product.id
");

And the result is;

a1   small   x,y   x.pdf,y.pdf (linked with x.pdf's path)
a2   big     z     z.pdf

Do i have to explode product_files cell now? If yes, please try to explain with a piece of code?

  • 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-13T21:01:10+00:00Added an answer on June 13, 2026 at 9:01 pm

    looks like you’re nearly there!

    you just need to do a GROUP_CONCAT on the file_path as well;

    SELECT products.*,
    GROUP_CONCAT(DISTINCT product_files.file_name ORDER BY product_files.id SEPARATOR ',') AS file_name,
    GROUP_CONCAT(DISTINCT product_files.file_path ORDER BY product_files.id SEPARATOR ',') AS file_path
    FROM products 
    INNER JOIN product_files 
    ON products.id = product_files.product_id
    GROUP BY products.id;
    

    and, for safety, order the GROUP_CONCAT-ed file_name & file_path, so they’re in the corresponding order.

    than you can explode file_name or file_path and loop through each array – to grab the file-path pairs (they’ll be in the same position in each array).

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

Sidebar

Related Questions

i have two tables products and reviews each product has several reviews linked by
I have two tables: one holding products and one holding related inventory moves. Product
I have two tables in one-to-many relation: product -> orders In the Products table
I have two tables: Folder, Files. Each table has 4 fields: ParentID, ID, Name,
A common (i assume?) type of query: I have two tables ('products', 'productsales'). Each
I have two tables: 1) product data 2) sales stats of these products In
I have two tables, one stores the products and quantity we have bought, the
I have two tables. One contains a list of products with the primary key
I have a problem with mutual constraints. I want to have two tables each
I have two tables table1 and table2 each having a column named email along

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.