What I have is some tables with a listing of books, rather than having one large table with (id, name, category, etc…), I have each genre of book split up into separate categories (there are many more, but this is just an example)
books_fantasy (id, name, etc…)
books_fiction (id, name, etc…)
Then there are user tables that track the user’s collection of these books
user_books_fantasy (username, fantasy1, fantasy2, etc…)
user_books_fiction (username, fiction1, fiction2, etc…)
The reason why the fields are that way is so I can use one $cat variable that will select the right table and then loop thru each field
What I want to do is query the user table, see if they own that book (field value of more than 0), then get the info from the books table and display it.
Currently the way I have I setup it works ok
It is in a loop that queries the user table, if they have the book, it then queries the books table to get the info.
function display_books($cat)
{
//set max
$result=db_query("select * from user_books_$cat");
$max=mysql_num_fields($result);
//loop thru users(books they own)
$cntr=1;
do
{
$place=$cat.$cntr;
//get data from the users, $place will be a number value of how many they own
$result=mysql_query("select $place from user_books_$cat");
$own_amount=mysql_result($result);
if ( $own_amount > 0 )
{
//they own the book, get book info
$result2 = mysql_query("select * from books_$cat where id='$cntr'");
$info=mysql_fetch_array($result2);
//display the book info
}
$cntr++;
} while ( $cntr < $max );
} //end function
//some code here that defines "$cat" and then calls the function
I'm at work right now, and just typing this out, so if you test it might have parse error or something, but this is the meat of what it should be
Like I said, this works fine and I have no problems with it, I am just wandering if there is another way I could go about doing it, like maybe doing a join statement to do just one query (which at this point I'm not very good at, lol)...I still don't see any other way to do the queries(or query)without using the loop though.
That's why I'm throwing this out here, I keep thinking of it and this is all that I come up with
Thanks