Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Combo Box selected item - from DB (http://www.programmingforums.org/showthread.php?t=13002)

tAK Apr 15th, 2007 10:12 PM

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

Styx Apr 17th, 2007 11:36 PM

[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.

tAK Apr 18th, 2007 10:32 PM

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 Apr 23rd, 2007 12:53 AM

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?

Styx Apr 23rd, 2007 10:08 AM

[php]$query = mysql_query("SELECT * FROM table");
for ($i = 0; $i < ($var = mysql_fetch_array($query)); $i++)
{
print_r($var);
}[/php]

DaWei Apr 23rd, 2007 10:29 AM

Try not to forget your code tags, Tak.


All times are GMT -5. The time now is 12:34 PM.

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