![]() |
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(); ?>If someone has a nice simple example script for me to go by, that would be much appreciated. thanks /tAK |
[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. |
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 |
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? |
[php]$query = mysql_query("SELECT * FROM table");
for ($i = 0; $i < ($var = mysql_fetch_array($query)); $i++) { print_r($var); }[/php] |
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