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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:40:09+00:00 2026-05-31T21:40:09+00:00

I’m trying to write a php function that gets all the basic food data.

  • 0

I’m trying to write a php function that gets all the basic food data. Then get’s the ingredients for that food.

So, for the first query, Each row received has a “food id”.

Then, I want to take each food id… (this might be where my problem is)

Then Select ingredients from the food_ing table that have the ids I provide.
(there might also be an issue with multiple selects)

It’ll be returning multiple ingredients for each food. This will be a pretty big array itself.

So, to give some more context to my setup I have 3 tables. food, food_ratings, food_ing.

food_ing is full of ingredients, each row has the count, the ingredient, then the food_id it’s associated with.

So what im asking is…what would be the best way to query and return this information?

-An array with each food, it’s id, description, type…

-An array with all the ingredients for all the foods originally queried. (I figure I can sort them to their respective food on the AS3 side after I get the data back)

I was thinking something like this. But it’s not working. I can’t seem to get each foods id to work in my loop that’s building my select ingredients loop thing. I suppose this is because im using

What would be awesome if someone could explain how to do this all in 1 query.

function getFoods($start, $limit) {

    $qOne = mysql_query("SELECT a.id, a.name, a.type, AVG(b.r) AS fra, COUNT(b.id) as tvotes FROM `foods` a LEFT JOIN `foods_ratings` b ON a.id = b.id GROUP BY a.id ORDER BY fra DESC, tvotes DESC LIMIT $start, $limit;");

    $arrOne = mysql_fetch_array($qOne);

    $i = 0;
    $qry = "";
    foreach ($arrOne as $value) {
        $fid = $value['id'];
        if ($i > 0)
            $qry .= " UNION ";
        $i++;
        $qry .= "SELECT fid, ing, amount FROM foods_ing WHERE fid='$fid'";
    }

    $qTwo = mysql_query($qry);

            /*
            if this helps anyone...I'm using amfphp to communicate with an AS3 project.
            also returning $qOne works. so no issues there. The issue is just with $qTwo.
            Unless the reccomendation is to somehow do this with 1 query.
            */

    return array($qOne, $qTwo);
}

Actually I might have figured out why it was looking weird. It seems that was only getting 1 row. I tried this, but im still having trouble. now nothing is being returned

EDIT: sharing another loop i tried. Now the first result is not working…Does using this while loop ruin my result or something

but at least with this loop, its getting the query correct, and returning the proper result.(for the second array)

function getFoods($sort, $start, $limit) {

            $qOne = mysql_query("SELECT a.id, a.name, a.type, AVG(b.r) AS fra, COUNT(b.id) as tvotes FROM `foods` a LEFT JOIN `foods_ratings` b ON a.id = b.id GROUP BY a.id ORDER BY fra DESC, tvotes DESC LIMIT $start, $limit;");

            $i = 0;
        $qry = "";
        while ($row = mysql_fetch_array($qOne)) {
            $fid = $row['id'];
            if ($i > 0)
                $qry .= " UNION ";
            $i++;
            $qry .= "SELECT fid, ing, amount FROM foods_ing WHERE fid='$fid'";
        }

            $qTwo = mysql_query($qry);

            return array($qOne, $qTwo);
        }

EDIT:

Here’s another attempt at trying to do it all with 1 query. But it’s just returning the first values it gets for ing and amount (It should be an array that’s coming back for ing because there are at least a few ingredients

SELECT c.ing, c.amount, a.id, a.name, a.type, AVG(b.r) AS fra, 
COUNT(b.id) as tvotes 
FROM foods a 
LEFT JOIN foods_ratings b ON a.id = b.id, foods_ing c 
GROUP BY a.id 
ORDER BY fra DESC, tvotes DESC 
LIMIT $start, $limit
  • 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-31T21:40:10+00:00Added an answer on May 31, 2026 at 9:40 pm

    Ok so….

    The solution was with a while loop

    while ($row = mysql_fetch_array($qOne)) {
                $fid = $row['id'];
                if ($i > 0)
                    $qry .= " UNION ";
                $i++;
                $qry .= "SELECT fid, ing, amount FROM foods_ing WHERE fid='$fid'";
            }
    

    Thanks for the input everyone, but I found out about the while loop on my own. figure i’ll post the answer to help anyone out that might need it.

    Upvotes tho to those that suggestion using UNION to combine the queries.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, 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.