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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:45:12+00:00 2026-06-18T06:45:12+00:00

I have a SQL Query: SELECT mealdesc, group_concat(fooddesc) as fooddesc FROM plan p INNER

  • 0

I have a SQL Query:

SELECT mealdesc, group_concat(fooddesc) as fooddesc 
FROM plan p
INNER JOIN mealplan mp
ON mp.planid = p.id
INNER JOIN meal m
ON m.id = mp.mealid
INNER JOIN foodmeal fm
ON fm.mealid = m.id
INNER JOIN food f
ON f.id = fm.foodid
where userid = 2
GROUP by mealdesc
order by mealdesc

Results look like this:

mealdesc,fooddesc
Meal 1,"egg,egg whites,oatmeal,wheat toast,fruit,berries"
Meal 2,"Isolyze,raw nuts"
Meal 3,"chicken,turkey,yam,baked potato,veggies,medium salad"
Meal 4,"Isolyze,raw nuts"
Meal 5,"veggies,lean red meat,salmon,Chilean Sea Bass,large salad"
Meal 6,"veggies,large salad,Casein Protein,white fish"

When I remove the group by and group_concat results look like this:

mealdesc,fooddesc
Meal 1,egg
Meal 1,egg whites
Meal 1,oatmeal
Meal 1,wheat toast
Meal 1,fruit
Meal 1,berries
Meal 2,Isolyze
Meal 2,raw nuts
Meal 3,veggies
Meal 3,medium salad
Meal 3,chicken
Meal 3,turkey
...and so on.

Instead of doing a Group by and Group_Concat in the sql statement and having to deal with explode etc. to deal with the delimiters and get it into a php array, is there a way to do this same group by and group_concact logic and organize all this in PHP and push it into an array?

Update:
Thank you for the responses so far. Adding my PHP and HTML sections to this.

I’m basically looking to output each Meal as a Heading and each food in a list. I’ve used explode in the past but I can’t get the array formed right, perhaps thats just my problem.

PHP Code:

$sql = "SELECT mealdesc, group_concat(fooddesc) as fooddesc 
    FROM plan p
    INNER JOIN mealplan mp
    ON mp.planid = p.id
    INNER JOIN meal m
    ON m.id = mp.mealid
    INNER JOIN foodmeal fm
    ON fm.mealid = m.id
    INNER JOIN food f
    ON f.id = fm.foodid
    where userid = 2
    GROUP by mealdesc
    order by mealdesc";

$result = mysqli_query($link, $sql);
if (!$result)
{
echo 'Error';
exit();
}


// Fetch food data
while($row = mysqli_fetch_assoc($result)){
$meals[] = array('mealdesc' => $row['mealdesc'], 'fooddesc' => explode(",", $row['fooddesc']));
}


echo '<pre>';
echo print_r($meals, true);
echo '</pre>';

Then in my HTML i loop through like this to output the Meal and food descriptions

<?php foreach ($meals as $meal): ?>
<h4><?php htmlout($meal['mealdesc']); ?></h4>
<ul>
  <li><?php htmlout($meal['fooddesc']); ?></li>
</ul>
<?php endforeach; ?>
  • 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-18T06:45:14+00:00Added an answer on June 18, 2026 at 6:45 am

    I’ll assume you used PDO to get your recordset above with a Prepared Statement:

    $meals = array();
    while ($row = $stmt->fetch()) {
        $meals[$row[0]][] = $row[1];
    }
    

    That gives you the following format:

    array(
        "Meal 1" => array("egg","egg whites","oatmeal","wheat toast","fruit","berries"),
        "Meal 2" => array("Isolyze","raw nuts"),
        "Meal 3" => array("chicken","turkey","yam","baked potato","veggies","medium salad"),
        "Meal 4" => array("Isolyze","raw nuts"),
        "Meal 5" => array("veggies","lean red meat","salmon","Chilean Sea Bass","large salad"),
        "Meal 6" => array("veggies","large salad","Casein Protein","white fish"),
    );
    

    You can then select one in the format you wish by doing:

    implode(",", $meals["Meal 1"]);
    

    Output

    egg,egg whites,oatmeal,wheat toast,fruit,berries
    

    EDIT

    Looking at your code above, you probably want to do the following in your HTML:

    <?php foreach ($meals as $meal): ?>
    <h4><?php htmlout($meal['mealdesc']); ?></h4>
    <ul>
      <?php foreach ($meal['fooddesc'] as $d): ?>
      <li><?php htmlout($d); ?></li>
      <?php endforeach; ?>
    </ul>
    <?php endforeach; ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SQL Query: SELECT documents.*, t_rights.rights, documents_list.docs FROM documents INNER JOIN t_rights
I have following SQL Query SELECT * FROM KDMS_dynamic vd INNER JOIN KDMS_definition tblKDMS
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have Sql query: SELECT News.NewsId, News.Subject, Cm.Cnt FROM dbo.News LEFT JOIN (SELECT Comments.OwnerId,
I have a SQL Query : SELECT * FROM Customer c LEFT JOIN Invoice
I have an SQL query SELECT * FROM A FULL OUTER JOIN B ON
i have this sql query: SELECT b.topbid, b.topdate, a.* FROM auction_items a LEFT JOIN
Consider I have such SQL query: SELECT t1.* FROM table1 t1 LEFT JOIN table2
i have this sql query select * from table where name like ? but

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.