Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Apr 17th, 2007, 11:36 PM   #2
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
[php]<?php
$var = $_POST['var'];

echo '<select name="var">';
for ($i = 0; $i < 10; $i++)
{
$selected = ($var == $i) ? 'selected="selected"' : '';
echo "<option value=\"$i\" $selected>$i</option>";
}
echo '</select>';
?>[/php]
The $selected part is I believe what you want. This example uses a $_POST variable though.
Styx is offline   Reply With Quote
Old Apr 18th, 2007, 10:32 PM   #3
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
seems to do what i wanted..
it also appears to be close to what i was trying, with a few exceptions..
hopefully i can sort it all out now that i have seen a script capable of this on its own.

i'll be sure to drop back in and let you know how i went

Cheers
/tAK
tAK is offline   Reply With Quote
Old Apr 23rd, 2007, 12:53 AM   #4
tAK
Programmer
 
Join Date: Mar 2007
Posts: 33
Rep Power: 0 tAK is on a distinguished road
Righto, i said i would let you know how i went, so here goes..
with great thanks to Styx, i got it working for 1 combo box, i then stuck the while loop for reading the array of data into a for loop, and was getting 5 drop downs as needed, BUT !!

when you do a mysql query:
$query = mysql_query("SELECT * FROM table");
and then fetch it in a while loop:
while($var = mysql_fetch_array($query))
{
print_r($var);
}

it somehow closes off the $query, so putting that WHILE loop into a FOR loop means that the other X number of times it runs, it doesnt output anything.

how to solve this?, well.. easy enough.. it goes something like this:

for ($i = 1; $i < 6; $i++)
{
$query = mysql_query("SELECT * FROM table");
while($var = mysql_fetch_array($query))
{
print_r($var);
}
}

You then get 5 outputs of the array, and i just did the basic IF statements etc..

eitherway, from what i can tell this is giving the database a right royal flogging, by hitting it with 5 requests for the same chunk of data when i should be able to grab it and hold it.. shouldn't i?
tAK is offline   Reply With Quote
Old Apr 23rd, 2007, 10:08 AM   #5
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
[php]$query = mysql_query("SELECT * FROM table");
for ($i = 0; $i < ($var = mysql_fetch_array($query)); $i++)
{
print_r($var);
}[/php]
Styx is offline   Reply With Quote
Old Apr 23rd, 2007, 10:29 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Try not to forget your code tags, Tak.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
error 3 Expected end-of-statement m0rb1d Other Scripting Languages 0 Dec 12th, 2006 10:05 AM
Confusion With Pointers and Arrays JawaKing00 C 10 Sep 14th, 2006 7:33 AM
anoying problem with a combo box cloud- Visual Basic 12 May 28th, 2005 1:20 PM
Java Combo Boxes Vengeance Java 0 May 4th, 2005 9:02 AM
What are advantages of Data Combo and data list over simple combo and list box? sham Visual Basic 1 Apr 12th, 2005 4:11 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC