fife Posted February 22, 2011 Share Posted February 22, 2011 Ok i have been after the solution for this for a while and I have finally got it in a state where it is producing results but....... they are wrong so I will start from the beginning with what I have and what I am trying to do. I have these tables Members Id Name Clubs ID Name Permission ID Name Link MemberID PermissionID ClubID When a member joins a club thier ID ant the ID of the club are entered into the link table along with their permission (as a number) so MemberID PermissionID ClubID 01 02 01 I have a loop which is going to echo the clubs a member is a part of. So for my querys function GetUser($user) { $qFindUser = "SELECT * FROM members WHERE email = '$user'"; $rFindUser = mysql_query($qFindUser); $UserInfo = mysql_fetch_array($rFindUser); return $UserInfo; } function GetLinkByMemberID($link) { $qLink = mysql_query("SELECT * FROM link WHERE memberID = '$link'"); $Link = mysql_fetch_array($qLink); return $Link; } function GetClub1($clubs) { $qFindClub = mysql_query("SELECT * FROM clubs WHERE clubID = '$clubs'"); return $qFindClub; } function GetPermission($per) { $qPermission = mysql_query("SELECT * FROM `permissions` WHERE permissionID = '$per'"); $permission = mysql_fetch_array($qPermission); return $permission; } $User = GetUser($_SESSION['Username']); // returns as array from the login details all member details $Link = GetLinkByMemberID($User['memberID']); // returns as a fetch array all clubs the member is a part of $Club = GetClub1($Link['clubID']); // returns as the query $permission = GetPermission($Link['permission']); //returns as array so I can echo the permission word not ID Now the loop is limited by 3 so <?php $Club = mysql_fetch_array($Club); $stack3 = ($Club+$Link); for($i = 0; ($CP = ($stack3)) && $i < 3; $i++ ){ ?> <tr> <th scope="col"><img name="" src="" width="32" height="32" alt="" /></th> <th scope="col"><?php echo $CP['name']; //from the club table ?><span class="ownertext"> <?php echo $CP['permission']; //from the link table ?> The problem I'm having the the loop is returning the first record its selects 3 times and not echoing each club once. I have worked on it for so long I can no longer make sense of it. Can anyone see my errors and point out just how to fix it? Im guessing it involves something to do with how Im finding my permission but I really dont know. As im sure you can see from the code Im a php beginner so Im struggling to make sense of this. Quote Link to comment https://forums.phpfreaks.com/topic/228491-complicated-looping/ Share on other sites More sharing options...
fife Posted February 22, 2011 Author Share Posted February 22, 2011 sorry that last post was wrong where it says //from link table. Its not at all its from the permission table Quote Link to comment https://forums.phpfreaks.com/topic/228491-complicated-looping/#findComment-1178151 Share on other sites More sharing options...
fife Posted February 22, 2011 Author Share Posted February 22, 2011 OK after a lot of research I see I was going about this all wrong. I have to do a inner join. So I have done one, but there is an error as my query is a little more complicated than the practise ones. I have to do mine based on the current user logged in! Can somebody please explain what I have done wrong? the code $qLink = mysql_query("SELECT link.memberID, link.permission, link.clubID, clubs.clubID, clubs.name, FROM `link` INNER JOIN `clubs` ON link.clubID=clubs.clubID WHERE link.memberID = ".$User['memberID']."") or die(mysql_error()); for($i = 0; ($CP = ($qLink)) && $i < 3; $i++ ){ ?> <tr> <th scope="col"><img name="" src="" width="32" height="32" alt="" /></th> <th scope="col"><?php echo $CP['name']; ?><span class="ownertext"> <?php echo $CP['permission']; ?></span></th> <?php } ?> The Error; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `link` INNER JOIN `clubs` ON link.clubID=clubs.clubID WHERE link.memberID =' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/228491-complicated-looping/#findComment-1178236 Share on other sites More sharing options...
BlueSkyIS Posted February 22, 2011 Share Posted February 22, 2011 remove the comma before FROM Quote Link to comment https://forums.phpfreaks.com/topic/228491-complicated-looping/#findComment-1178238 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.