Jump to content


Photo

Problem with the need for two simultaneous while() loops


  • Please log in to reply
6 replies to this topic

#1 Gast

Gast
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts
  • LocationSouth East England

Posted 08 July 2006 - 07:05 PM

I know it sounds stupid, but let me explain.

For a project myself and obsidian are working on, I have come across a problem. I wont explain the whole project here but here is the problem:

I need to display 6 "stables", in as in three rows of two. I have done this with HTML, however each user will at some point buy these "stables" so they need to be dynamic, as some of them have a different background image as they are unavailable.

They may only have bought 3 stables, so the first two of the stables on the first row are available, and the first on the second row. This is a little hard to explain in words, so:

Available | Available
Available | Unavailable
Unavailable | Unavailable

This is all OK, I have managed to generate all the stables with some available and some unavailable depending on how many the user has bought. But here comes the problem.... The user will have an "animal" that will be in one of the stables. However, they may not have an animal in all of their available stables.

Both the stables and animals are stored in a database, so I run the query to get the stables and in a while() loop echo the actual stables in HTML. But I need to then run the query for the "animals" to be in the stables at the same time. I dont think it is possible to run two conditions in a while() loop, and the way the HTML is constructed I cannot put the loop inside the other.

PLEASE HELP! I can provide a link and code if needed.
OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 July 2006 - 07:12 PM

Can you post how your tables are defined? This should be fairly straight forward depending on how the tables are defined. You should only need one loop.

Ken

#3 Gast

Gast
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts
  • LocationSouth East England

Posted 08 July 2006 - 07:46 PM

Sure, below is the PHP code. I apologise if it is a little confusing...

<?php
function showStables($user_id) {

	// Check if user has any stables to start with
	$sql = mysql_query("SELECT * FROM choco_stables WHERE stable_user = ".$user_id." ORDER BY stable_id") or die(mysql_error());

	// Number of stables...
	$numStables = mysql_num_rows($sql);
	
	// If no stables...
	if($numStables == 0) {
		
		print("
			<div style='width:515px; padding:5px; background-color:#EFEFEF; border:1px solid #999999;'>
				You do not have any stables. In order to look after your Chocobos, you will need to purchase one. However, as it is your first one, we will give you one for <b>free</b>, but in the future, if you want more, it will cost you <b>2,500 gil</b> for each one!<br />
				<center>
					<input type='button' class='submits' style='width:200px; margin-top:15px; margin-bottom:15px; font-size:14px; background-image:url(images/orange_fade.jpg); font-weight:bold' value='Get My First Stable' />
				</center>
			</div>
		");
	
	// Already have some stables...	
	} else {
	
		// Start variables...
		$counter = 1; 
		$per_row = 2;
		$stableCount = 1;
		
		// Start table...
		$temp = '<table align="center" width="400" border="0" cellspacing="0" cellpadding="0" style="background-image:url(images/stable_floor.gif)">';
	
		while($row = mysql_fetch_assoc($sql)) {
		
			$stableLeft = rand(1, 3);
			$stableRight = rand(4, 6);
			
			if($counter == 1) { 
				$temp .= "<tr>";
			}
			
			if(($stableCount % 2) == 1) {
				$temp .=	'<td width="200" valign="top" class="stable" style="background-image:url(images/stable_background_'.$stableLeft.'.gif);">';
		// Here is where each animal will be displayed on the left...										
							$temp.= '</td>';
			} else {
				$temp .=	'<td width="200" valign="top" class="stable" style="background-image:url(images/stable_background_'.$stableRight.'.gif);">';
		// Here is where each animal will be displayed on the right...									
							$temp.= '</td>';
			}
			
			if($counter == $per_row) {
				$temp .= "</tr>";
				$counter = 1;
			} else {  
				$counter++;  
			} 
			
			// Increase counter
			$stableCount++;
			
		}
		
		if($counter != 1) {  
		
			while($counter <= $per_row) {  
				
				// Odd number of stables?
				if(($numStables % 2) == 1) {
					$temp .= '
						<td valign="top" class="stable" style="background-image:url(images/stable_background_dull_right.gif);">
							&nbsp;
						</td>
					';
					$counter++;
				} 
				$temp .= "</tr>";
				
			}
		
		}
		
		$temp .= "</table>";
		
		print($temp);
		
	}
		
}

?>

OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 July 2006 - 07:51 PM

What I'm looking for is the how your database tables are defined ... fields.

Ken

#5 Gast

Gast
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts
  • LocationSouth East England

Posted 08 July 2006 - 07:56 PM

Sorry, I misunderstood. That may help above, though.

Yes, I have one table which stores the users, simply users, which contains the usual, user_id, user_name, user_email.

For the stables, the table is choco_stables:
stable_id (Primary), stable_user (Foreign)

Obviously the "stable_user" relates to the "user_id".

And finally the actual animals, is the table choco_chocobos:
[i]choco_id (Primary), choco_owner (Foreign), choco_level, choco_name...[i]

Hopefully that may be a little more helpful. :)
OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 08 July 2006 - 08:21 PM

If I understand your tables correctly, here's the basic loop I would use (you would have to fit your display requirements in):
<?php
$q1 = "Select * from choco_stables";
$rs1 = mysql_query($q1) or die("Problem getting stables, query: $q1<br>" . mysql_error());
while ($rw1 = mysql_fetch_assoc($rs1)) {
     if ($rw1['stable_user'] > 0) { // or however you test for an owned stable
         $q2 = "select * from users where user_id = " . $rw1['stable_user'];
         $rs2 = mysql_query($q2) or die("Problem getting owner of stable, query: $q2<br>" . mysql_error());
         $rw2 = mysql_fetch_assoc($rs2);
         $q3 = "select * from choco_chocobos where choco_owner = " . $rw2['user_id'];
         $rs3 = mysql_query($q3) or die("Problem getting animal, query: $q3<br>" . mysql_error());
         if (mysql_num_rows(rs3) > 0) { // This person owns an animal
            $rw3 = mysql_fetch_assoc($rs3);
//
//          more code associated with the animal
//
          }
//
//          more code associated with the owner of the stable
//
    }
?>

Ken

#7 Gast

Gast
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts
  • LocationSouth East England

Posted 08 July 2006 - 08:27 PM

Thanks, Ken.

When I added the echo $rw3['choco_name']; part, it echoes the name of the animal three times. This is not right, it has the number of stables right, but it should only echo the name once.

Any other ideas?

<?php
$q1 = "Select * from choco_stables";
$rs1 = mysql_query($q1) or die("Problem getting stables, query: $q1<br>" . mysql_error());
while ($rw1 = mysql_fetch_assoc($rs1)) {
     if ($rw1['stable_user'] > 0) { // or however you test for an owned stable
         $q2 = "select * from users where user_id = " . $rw1['stable_user'];
         $rs2 = mysql_query($q2) or die("Problem getting owner of stable, query: $q2<br>" . mysql_error());
         $rw2 = mysql_fetch_assoc($rs2);
         $q3 = "select * from choco_chocobos where choco_owner = " . $rw2['user_id'];
         $rs3 = mysql_query($q3) or die("Problem getting animal, query: $q3<br>" . mysql_error());
         if (mysql_num_rows($rs3) > 0) { // This person owns an animal
            $rw3 = mysql_fetch_assoc($rs3);
			echo $rw3['choco_name'];
//
//          more code associated with the animal
//
          }
//
//          more code associated with the owner of the stable
//
	}
	
    }	
}
?>

OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users