josephman1988 Posted August 18, 2008 Share Posted August 18, 2008 Hey guys, What my project does, is have user profiles and operation profiles, when adding an operation i can add a user that is involved in that operation. Doing this populates a 'user_data' table with that users id to that operation id: ---------------------------------- |operation_dataoid | user_datauid | ---------------------------------- On a users profile i want to list the operation names that the user is involved with. My attempt: <?php $collect3 = @mysql_query("SELECT * FROM user_operation WHERE user_datauid = $uid"); while ($result3 = mysql_fetch_array($collect3)) { $collect2 = @mysql_query("SELECT * FROM operation_data "); $result2 = mysql_fetch_array($collect2); $result2['oid'] = $result3['operation_dataoid']; print "Operation ID: " . $result2['oid']; } ?> $result3 uses the current userid to get all operation ids in that table associated with that uderisd. $result2 gets the information from the operation_data (with its name etc) and i put the id gotten from the user_operation table and match it with the oid. The print will get each operation, and sucessfully get the id of the operations the user is involved in and displays it. However, i want the name of the operation aswell, how would i get this? Thanks in advanced. =] Quote Link to comment https://forums.phpfreaks.com/topic/120194-solved-displaying-dynamic-content/ Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 You want to use a JOIN and then link them with their IDs. Quote Link to comment https://forums.phpfreaks.com/topic/120194-solved-displaying-dynamic-content/#findComment-619179 Share on other sites More sharing options...
josephman1988 Posted August 18, 2008 Author Share Posted August 18, 2008 Thanks for the instant reply. I've read up a bit on joins, but it is a bit confusing =[. I tried: (With previous code still intact) <?php $collect3 = @mysql_query("SELECT * FROM user_operation WHERE user_datauid = $uid"); while ($result3 = mysql_fetch_array($collect3)) { $collect2 = @mysql_query("SELECT * FROM operation_data "); $result2 = mysql_fetch_array($collect2); $queryjoin = mysql_query("SELECT user_operation.operation_dataoid, operation_data.name FROM user_operation LEFT JOIN operation_data ON operation_data.name = operation_data.oid WHERE user_operation.user_datauid = $uid") or die(mysql_error()); $result4 = mysql_fetch_array($queryjoin); print $result4['name'] . "<br />"; } ?> My DB Tables: user_data table: -------------- | uid | name | -------------- operation_data table: -------------- | uid | name | -------------- user_operation: ----------------------------------- | operation_dataoid | user_datauid | ----------------------------------- As my attempt prints nothing, what am i doing wrong? My head hurts =[ Quote Link to comment https://forums.phpfreaks.com/topic/120194-solved-displaying-dynamic-content/#findComment-619205 Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 And what do you want your final display to show? You are close on your JOIN, you just have to link IDs, not name and ID. Also, you need to do a while loop for the result, unless you only expect 1 result. Quote Link to comment https://forums.phpfreaks.com/topic/120194-solved-displaying-dynamic-content/#findComment-619219 Share on other sites More sharing options...
josephman1988 Posted August 18, 2008 Author Share Posted August 18, 2008 Well, I finally want to show the name of the operation (operation_data.name). After changing the query JOIN from: ON operation_data.name = operation_data.oid To: ON operation_data.oid = user_operation.operation_dataoid And after displaying the results I get: nbcxnxncn nbcxnxncn Basically 2 of the same operation. Should i get rid of the while loop. while ($result3 = mysql_fetch_array($collect3)){} And do a while loop on the JOIN query we just did? UPDATE: I just got rid of the while loop i first did and did it on the JOIN query and it worked like a charm, thanks hugely for your help. Quote Link to comment https://forums.phpfreaks.com/topic/120194-solved-displaying-dynamic-content/#findComment-619229 Share on other sites More sharing options...
revraz Posted August 18, 2008 Share Posted August 18, 2008 Well that means it found 2 rows in your DB. Quote Link to comment https://forums.phpfreaks.com/topic/120194-solved-displaying-dynamic-content/#findComment-619231 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.