PDA

View Full Version : Create single variable from while loop


ptmuldoon
04-06-2007, 02:34 AM
I have a simply loop that is getting the 'player' field from a table, and then echo'ing out all of them.

What I want to do is be able to place all the names in one variable so that i can include that single variable in a another echo statement be used in creating a bubble tooltip.

while($row = mysql_fetch_array($player_data)){
echo $row['player'] . ' <br /> ';

}

Mhtml
04-06-2007, 02:53 AM
Not quite sure if I'm getting you.. Perhaps you mean to:

$players = array();
while($players[] = mysql_fetch_array($player_data));

ptmuldoon
04-06-2007, 03:07 AM
I wish I knew more php than I did, and perhaps I didn't explain it clearly enough, or perhaps you provided me with a solution but I'm still green on understanding arrays.

Essentially, the current while loop will output something like

player a
player b
player c
player d
etc, etc depending on the number of rows.

I want to list all of the players into one variable (including the <br />) that I can use in another echo statement being created and used within a tool tip. So the end result is, when a user mouses over a link, it gives them a tool tip of all the players.

I'm getting close. I have the tool tip working, and have it echo a variable of a single player. Just need to it list all the players.

iLLin
04-06-2007, 03:30 AM
while($row = mysql_fetch_array($player_data)){
$html .= $row['player'] . ' <br /> ';
}

echo $html;

//what? tool tip? where?
echo $html;


:)

ptmuldoon
04-06-2007, 03:30 AM
I uploaded a sample of where I'm at.

Essentially, I'm querying a table to get the data. Then trying to place the players into a tool tip.

http://www.riskwars.com/risk/bubble_test.php

Mhtml
04-06-2007, 04:54 AM
Yeah ok, I understand now - refer to what iLLin posted. Although if you're only using players names from that particular query I assume you're just "SELECT name FROM players WHERE team_id="-ing or something similar.. In which case you can also do as I suggested and just implode("<br />", $players) and you still have your original array to access somewhere else where you might need to use them in a different context but still have access to them individually.

Unless of course you're using all the for information from the query, in which case I recommend going OOP and working with model objects and collections - honestly it's well worth the hour you spend writing a dynamic model object and collection object.

iLLin
04-06-2007, 05:25 AM
Yea well if hes asking that question, then you can try explaining object to him :p

ptmuldoon
04-06-2007, 01:05 PM
lol, I'll try and stay away from objects for a while.

Well, anyway, iLLin got me pretty close, but for some strange reason, when it loops, it is adding the players of the previous game to the existing game, and so on all the way through the loop. Thus the last game listed will about about 90 names in, when it should only have about 4 or 5 depending on the game size.

Here's the full code and link again.

$query_rows = mysql_num_rows($some_games);

$rownum = 0;
while ($game = mysql_fetch_array($some_games)){
$sql = "SELECT player FROM game_{$game['id']} WHERE id != 1 ";
$player_data = mysql_query($sql);

while($row = mysql_fetch_array($player_data)){
echo $row['player'] . ' <br /> ';
$player .= $row['player'] . '<br />';
}

echo ("Very long echo here on game name, includes $player variable for tool tip");
$id++ ;
}

http://www.riskwars.com/risk/bubble_test.php

Mhtml
04-06-2007, 01:47 PM
Well, for each game you are just adding the players of that game to the $player variable. You'll need to initialize the $player variable to null each time you loop.

query_rows = mysql_num_rows($some_games);

$rownum = 0;
while ($game = mysql_fetch_array($some_games)){
$sql = "SELECT player FROM game_{$game['id']} WHERE id != 1 ";
$player_data = mysql_query($sql);
$player = null;//just reset it to null here

while($row = mysql_fetch_array($player_data)){
echo $row['player'] . ' <br /> ';
$player .= $row['player'] . '<br />';
}

echo ("Very long echo here on game name, includes $player variable for tool tip");
$id++ ;
}

ptmuldoon
04-06-2007, 02:44 PM
Just want to say thanks, and it works pretty good.

Also, if anyone was interested, the tool tip is entirely css driven. there's no javascript used at all.

iLLin
04-06-2007, 03:22 PM
Good job! Looks pretty good.

Only thing I dont like, is If I try to view the game right after then one on top, I have to go all the way down to get off the bubble, then go back up to see the next game. You should offset them some.

ptmuldoon
04-06-2007, 04:33 PM
Thanks

That was a just a test page anyway. I'll likely try and tinker more on offsetting it better. Its being used now here:

http://www.riskwars.com/risk/

Its the # column, where you see 5/5, 6/8, 7/7, etc.