Jump to content

Complicated Looping


fife

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.