BigTime Posted October 11, 2011 Share Posted October 11, 2011 Running a simple query: SELECT team, rank from table ORDER BY rank ASC returns 8 records in order from 1 to 8 I then want to a assign a new variable to each that I can use for later queries ie: team with rank 1 = $team1 team with rank 2 = $team2 team with rank 3 = $team3 etc.... Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/248872-how-to-assign-variable-to-items-in-an-array/ Share on other sites More sharing options...
WebStyles Posted October 11, 2011 Share Posted October 11, 2011 I'm not really sure if I know what you mean, but it sounds like you're looking for a way to use a variable's value as another variable's name... So you can dinamically create the variables names... check out http://php.net/manual/en/language.variables.variable.php. here's an example: <?php // create the name you wish to use as a variable $VariableName = "rank"."1"; // Assign a value (team name) to the new variable $$VariableName = "teamName"; // echo results echo $rank1; ?> Hope this helps Link to comment https://forums.phpfreaks.com/topic/248872-how-to-assign-variable-to-items-in-an-array/#findComment-1278063 Share on other sites More sharing options...
Buddski Posted October 11, 2011 Share Posted October 11, 2011 $rank = 1; // This will be pulled from the DB $team_name = 'Bobs Team'; // This will be pulled from the DB ${'team' . $rank} = $team_name; echo $team1; Link to comment https://forums.phpfreaks.com/topic/248872-how-to-assign-variable-to-items-in-an-array/#findComment-1278065 Share on other sites More sharing options...
BigTime Posted October 11, 2011 Author Share Posted October 11, 2011 thank you both for the help. Im now able to reference and echo $team1 through $team8 throughout my document. Can anyone please point me to more information on $$VariableName? why the two $$? My final code that worked: # setup SQL statement $SQL = " SELECT DISTINCT team, rank FROM table ORDER BY by rank ASC"; # execute SQL statement $teamsquery = mysql_db_query($db, $SQL, $cid); # check for errors if (!$teamsquery) { echo( mysql_error()); } while($row=mysql_fetch_array($teamsquery)) { $team=$row["team"]; $rank=$row["rank"]; // create the name you wish to use as a variable $VariableName = "team"."$rank"; // Assign a value (team name) to the new variable $$VariableName = "$team"; } Link to comment https://forums.phpfreaks.com/topic/248872-how-to-assign-variable-to-items-in-an-array/#findComment-1278374 Share on other sites More sharing options...
requinix Posted October 11, 2011 Share Posted October 11, 2011 I know you want to use variables but arrays are the right way to do this. First team is $team[1], second is $team[2], and so on. $teams = array(); # setup SQL statement $SQL = " SELECT DISTINCT team, rank FROM table ORDER BY by rank ASC"; # execute SQL statement $teamsquery = mysql_db_query($db, $SQL, $cid); # check for errors if (!$teamsquery) { echo( mysql_error()); } while($row=mysql_fetch_array($teamsquery)) { // Append a value (team name) into the array $teams[$row["rank"]] = $row["team"]; } Link to comment https://forums.phpfreaks.com/topic/248872-how-to-assign-variable-to-items-in-an-array/#findComment-1278391 Share on other sites More sharing options...
BigTime Posted October 12, 2011 Author Share Posted October 12, 2011 requinex thank you very much for your assistance. Easier, and worked out just fine. Link to comment https://forums.phpfreaks.com/topic/248872-how-to-assign-variable-to-items-in-an-array/#findComment-1278503 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.