View Single Post
Old Apr 15th, 2007, 10:12 PM   #1
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
Combo Box selected item - from DB

Hey guys,

I have a database which contains a list of game names, i am reading it and using it to create a combo box in a webpage.

When a user submits an item, and then views the same page again, i want that item to be the one automatically selected in the combo box.

i know that this creates a selectable item:
<option value="3">Age Of Empires 2</option>

and that this:
<option value="3" selected>Age Of Empires 2</option>
will force that item to be the automatically selected item. but i cannot get this to work dynamically based on the database reads,

this is the code (at its current state, i have mashed with it that much just seeing what effect different things have) but havent really gotten any closer:

<?php session_start(); ?>
<html>
<head>
	<title>Update Your Games</title>
<body>
<Form method="post" action="AddGames.php">
	<fieldset>
	<legend>Update Your Games - TEST</legend>
<?php
include ("Functions.php");
	Global $Option;
	$Username = safe_output($_SESSION['username']);
	$GamesList = mysql_query("SELECT * FROM games ORDER BY GameName");
	$ViewMyGames = mysql_query("SELECT Game1,Game2,Game3,Game4,Game5 FROM members WHERE Username='$Username'");
	If (!$GamesList) {
		Print "Unable to retrieve games list";
	} Else {
	$a = mysql_fetch_array($GamesList);
	$ChosenGames = mysql_fetch_array($ViewMyGames);
	print sizeof($a);
		While ($row = mysql_fetch_array($GamesList)) {
			If ($ChosenGames['Game1'] == $row['GameID']) {
			$Option = $Option.'<option value="'.$row['GameID'].'" selected>'.$row['GameName'].'</option><br/>';
			} ElseIf ($ChosenGames['Game2'] == $row['GameID']) {
			$Option = $Option.'<option value="'.$row['GameID'].'" selected>'.$row['GameName'].'</option><br/>';
			} ElseIf ($ChosenGames['Game3'] == $row['GameID']) {
			$Option = $Option.'<option value="'.$row['GameID'].'" selected>'.$row['GameName'].'</option><br/>';
			} ElseIf ($ChosenGames['Game4'] == $row['GameID']) {
			$Option = $Option.'<option value="'.$row['GameID'].'" selected>'.$row['GameName'].'</option><br/>';
			} ElseIf ($ChosenGames['Game5'] == $row['GameID']) {
			$Option = $Option.'<option value="'.$row['GameID'].'" selected>'.$row['GameName'].'</option><br/>';
			} Else {
			$Option = $Option.'<option value="'.$row['GameID'].'">'.$row['GameName'].'</option><br/>';
		}
		$i = 1;
		While ($i <= 5) {
			Print "<p>Game $i:<br/>";
			Print '<select name="game'.$i.'"><br>';
			Print $Option;
			Print "</select></p>";
			$i = $i + 1;
		}
	}
}
	mysql_close($con);
?>
		</p>
	<input type="submit" name="action" value="send" />
	</p>
	</fieldset>
</form>
<?php
print_menu();
?>
</body>
</html>

If someone has a nice simple example script for me to go by, that would be much appreciated.

thanks
/tAK
tAK is offline   Reply With Quote