MySQL Forums
Forum List  »  Newbie

Choosing records from a table.
Posted by: Zev Olivkovitch
Date: August 24, 2010 08:28PM

Hi.

I have 2 tables, Favorites and Recipes.

In the Favorites table I have the fields: User ID and Recipe ID.
In the Recipes table I have the fields Recipe ID and other fields about the recipe itself.

Based on the User ID (in the Favorites table) and lets say 5 favorite recipes for that user, I call each recipe (from the Recipes table and display it on the Favorites page (using a while loop).

Assuming that user X has recipes with ID 5, 32, and 117. How can I (or the user) sort the recipes (in the Favorites page) based on a certain field?

My thinking is that I need first to pull all the recipes first (ALL at once) and then perform the sorting in the query. But I do not know how to pull multiple records/rows that have different IDs.

Please help.


The code I use is:

....

$findFavorites = mysql_query ("SELECT * FROM $tblName3 WHERE mataccUser = '$user' LIMIT $adjustment, $rcpFavViewPerPage"); // ORDER BY $rcpFavSortBy
if ($findFavorites)
{
$getFavorite = mysql_fetch_row ($findFavorites);

while ($getFavorite)
{
$matrcpRecipe = $getFavorite[4];


$findCurrentRecipe = mysql_query ("SELECT * FROM $tblName2 WHERE matrcpRecipe = '$matrcpRecipe'");
$getCurrentRecipe = mysql_fetch_row ($findCurrentRecipe);

$matrcpRecipe = $getCurrentRecipe[0];
$mataccUser = $getCurrentRecipe[4];
$matrcpName = $getCurrentRecipe[5];
$matrcpDescription = $getCurrentRecipe[6];
$matrcpPhoto = $getCurrentRecipe[7];
$matrcpRegion = $getCurrentRecipe[11];
$matrcpCountry = $getCurrentRecipe[12];
$matrcpCategory = $getCurrentRecipe[13];

....

sample Favorite table:

user Recipe ID
---- ---------
aaa 251
aaa 32
bbb 68
aaa 7

sample Recipes Table:

Recipe ID Country Category
--------- ------- --------
7 ppp Salad
32 hhh Soup
68 ppp Main Dish
251 sql Soup



Thanks in advance.

Zev.



Edited 2 time(s). Last edit at 08/24/2010 08:46PM by Zev Olivkovitch.

Options: ReplyQuote


Subject
Written By
Posted
Choosing records from a table.
August 24, 2010 08:28PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.