Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding data to the end of a multi-demensional array

Status
Not open for further replies.

theoneweasel77

Programmer
Joined
Aug 10, 2004
Messages
54
Location
US
I'm trying to add an element on to the end of a multi demensional array, which should end up as $games[$i][9]. The code below is included in my page.
Code:
<?
function find_games($db, $username)
	{
		$games_in_progress = 0;
		$result = mysql_query("select * from games where game_creator = '$username'", $db);
		if (! $result)
			$games_in_progress++;
		else
		{
			$i = 0;
			while (true)
			{
				$row = mysql_fetch_row($result);
				if ($row)
					$games[$i] = $row;
				else
				{
					$games[$i][9] = "b";
					break;
				}
				$i++;
			}
		}
		$result = mysql_query("select * from games where game_partner = '$username'", $db);
		if (! $result)
			$games_in_progress++;
		else
		{
			while (true)
			{
				$row = mysql_fetch_row($result);
				if ($row)
					$games[$i] = $row;
				else
				{
					$games[$i][9] = "r";
					break;
				}
				$i++;
			}
		}
		if ($games_in_progress == 2)
			$games = "You aren't playing any games yet";
		return $games;
	}
	function games_check($db, $username)
	{
		$games = find_games($db, $username);
		if (! is_array($games))
			return "You are not playing any games";
		else
			return $games;
	}
?>
and it is included in the following script using the document root. (the entire script is not listed below)
Code:
<?
	if ( ! $games[$i] )
	{
		print "<tr valign=center bgcolor=DDDDDD>";
		print "<td align=left colspan=4>";
		print "<b>You are not playing any games</b>";
	}
	{
		while(true)
		{
			?>
				<TR VALIGN=CENTER  bgcolor=DDDDDD>
					<TD ALIGN=LEFT>
						<A HREF="in game source.php?ID=<? print $games[$i][4]; ?>&color=<? print $games[$i][9]; ?>">
							<? print $games[$i][0]; ?>
						</A>
						<BR>
						<SPAN CLASS=small_text_8>
							Field Command
						</SPAN>
					</TD>
					<TD ALIGN=CENTER>
						<? print $games[$i][2]; ?>
					</TD>
					<TD ALIGN=LEFT>
						<SPAN CLASS=small_text>
							<? print $games[$i][5]; ?>
						</SPAN>
						<BR>
						<SPAN CLASS=small_text>
							<? print $games[$i][6]; ?>
						</SPAN>
					</TD>
					<td align=left>
						<? 
							if ( $games[$i][7] == "yes")
							{
								print $games[$i][1];
								print "<br>";
							}
							if ( $games[$i][8] == "yes")
								print $games[$i][2];
						?>
					</td>
					<TD>
						&nbsp;
					</TD>
					<td>
						&nbsp;
					</td>
				</tr>
			<?
			$i++;
			if ($games[$i] == 0)
				break;
		}
	}
?>
everything works fine, except that the color is not being passed\set in $games[$i][9], and is being acted upon as though it were another element in the first array level ($games[num]). Why is this?
output is at Also, I've tried using array_push(), but I can't get that to work since it's a multi-demensional array.
 
hi. I realize that you can't see the output of that, however my login script just broke. Problems, problems...
 
I suggest that you use an associative array rather than a numerically keyed array.
$games[$i][9] is extremely undescriptive while $games[$i]['color'] makes more sense.

Debugging tips:
1. print_r($games) and inspect if the value is correctly set.
2. Make sure that $i is the correct game you expect.
 
$i looks good, and the print shows that $games[$1][9] is r, what it should be. If you hover over the URL (I suppose you could follow it, but nothing works on the other end), you'll notice that color= nothing. it should equal red. I left the print_r() if you want to see that
 
oh...Looked at it a little closer...how would I add my 'r' or 'b' at the end of [$i][] and not just [$i]?
 
thanks, I figured that problem out. If it bugs again I'll need to write another post
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top