Forum Moderators: coopster

Message Too Old, No Replies

displaying values from a DB in a dropdown

         

nshack31

11:36 am on Jan 16, 2005 (gmt 0)

10+ Year Member



From values of 1-20, I selected the value '3' from my dropdown box and it was inserted into the DB correctly. Now I would like a page that reads from the DB and displays a dropdown box with values 1-20 and the chosen value highlifhted (number 3). My code is this.........

while (odbc_fetch_row($rs))
$teamsize[]=odbc_result($rs,'teamsize');
{
$tsdropdown .=<<<HTML
<select name="teamsize">
<option value="All">All</option>
$options="<option value="$teamsize">$teamsize v $teamsize</option>";
echo "$options";
</select>
HTML;
}

This works great BUT it only displays the number 3. I want it to highlight the number 3 but also contain the other options also (1-20). p.s. im using odbc

please help!

Warboss Alex

6:16 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



While I'm not familiar with odbc, as far as I can tell you're echoing a seperate <select> box for every row? Try this instead:

$options = '';
while (odbc_fetch_row($rs))
$teamsize[]=odbc_result($rs,'teamsize');
{
$options .= "<option value="$teamsize">$teamsize v $teamsize</option>";
}

$tsdropdown =<<<HTML
<select name="teamsize">
<option value="All">All</option>
echo "$options";
</select>
HTML;

nshack31

6:57 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



I'll try that, thanks very much for your help

nshack31

7:11 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



nope that just shows me 3 lots of option 3 in a dropdown. I need a dropdown that contains 20 numbers, where number 3 (read from the DB) is highlighted

Warboss Alex

8:18 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



$options = '';
while (odbc_fetch_row($rs)) {
$teamsize=odbc_result($rs,'teamsize');
$options .= "<option value=\"$teamsize\">$teamsize v $teamsize</option>";
}

$tsdropdown =<<<HTML
<select name="teamsize">
<option value="All">All</option>
echo "$options";
</select>
HTML;

Ugh. Sorry. Try that. Didn't read the code I posted very well.

dmmh

9:31 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



this is one of mine. it also preselect if the select is part of a form and user presses back during the submission process OR if it matches a entry in the DB (for editing purposes) :)

$dvd_case_color_selector.= "<select name=\"case_color\" size=\"1\" class=\"menu\">\n";
$dvd_case_color_selector.= "<option value=\"\">select</option>";
{
//query database to see what CASE COLOR the dvd has so it can be pre-selected in html select object

mysql_select_db('db');
$query = 'SELECT case_color FROM dvds'." WHERE id LIKE '$id'";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$myresult = mysql_fetch_array($result);

if (empty($_POST['case_color'])){
$dvd_case_color_selected= $myresult['case_color'];
}
if (isset($_POST['case_color'])){
$dvd_case_color_selected= $_POST['case_color'];
}
//another query to see what color the user is allowed to select as case color for dvd's

$query2 = 'SELECT DISTINCT dvd_case_color FROM selectors WHERE dvd_case_color IS NOT NULL ORDER BY dvd_case_color ASC';
$result2 = mysql_query($query2) or die ("Error in query: $query2 " . mysql_error());

while ($myresult2 = mysql_fetch_array($result2)) // loop through and add each color to dvd_cases array
{
$dvd_cases[]= $myresult2;
}
foreach ($dvd_cases as $dvd_case)
{
// build the options...
$dvd_case_color_selector.= "<option value=\"$dvd_case[0]\"";
// highlight the currently associated case color
if ($dvd_case[0] == $dvd_case_color_selected)
{$dvd_case_color_selector.= " selected ";}
$dvd_case_color_selector.= ">$dvd_case[0]</option>\n";
}
// close the select tags
$dvd_case_color_selector.= "</select>"; }

next just use a echo $dvd_case_color_selector; where u need it :)

edit: sorry, missed the part where u arent using mysql...but the basics remain the same, just modify the queries etc

nshack31

9:53 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



Warboss Alex - still aint workin! :(

dmmh - I've tried to convert that code into ODBC. Couldn't do it, ODBC is completely different I'm affraid. As I have never used MYSQL, I've no hope of converting that code! Thanks anyway

dmmh

10:34 pm on Jan 16, 2005 (gmt 0)

10+ Year Member



seriously, you do know how to query the database type you use dont you? and access arrays?
the rest of the PHP can stay exactly the same, provinding you change some names perhaps

nshack31

1:00 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



I sorted it using the following.....

$data_cells="";
$data_cells .=<<<HTML
<option value="$teamsize">$teamsize v $teamsize</option>
<option>------</option>
HTML;

I then used

<select name=\"teamsize\" >
$data_cells
<option value=\"1\">1v1</option>
<option value=\"2\">2v2</option>
<option value=\"3\">3v3</option>
<option value=\"4\">4v4</option>
<option value=\"5\">5v5</option>
<option value=\"6\">6v6</option>
<option value=\"7\">7v7</option>
<option value=\"8\">8v8</option>
<option value=\"9\">9v9</option>
<option value=\"10\">10v10</option></select>

Boeboe

10:20 am on Jan 22, 2005 (gmt 0)

10+ Year Member



This isn't easy...
You have to select from 2 tables, and Left Join them..
Then you can have your #3 selected in the dropdown box, by selecting that one in a particular variable, and the rest #1 - 20 shown on the list in another variable

Not sure if this still is actual, if it is, do write to me...

I have just made a selection for my self, the code is like this':

$query = mysql_query("SELECT id,navn FROM stambog WHERE sex='M' ORDER BY navn");
echo "<tr><td>Father:&nbsp;</td><td><SELECT NAME=\"father_id\">\n";
echo "<OPTION SELECTED VALUE=\"$father_id\">Choose Father</OPTION>\n";
while ($row = mysql_fetch_array($query)) {
$fid = $row['id'];
$fnavn = $row['navn'];
echo " <option value=\"$fid\">$fnavn</option>\n";
}
echo "</SELECT></td></tr><br>\n";

As you can see, I show the name of the cat, but update the database with the ID.

The Option selected value is here "Choose father", but could be any variable, in your case a variable containing #3..

I hope, this could be used..
Regards from Denmark

scoobie

12:42 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



hi,

i have a country table that holds 5 different countries. a dropdown list in my form is populated based on this table. the problem is i want the country stored in the member table to be the default selected in the list . this is the code i tried:

<select name=CountryFrm>
<?php
$qry = "SELECT CountryName FROM Country ORDER BY CountryName";

$result = mysql_query($qry);
while($row = mysql_fetch_assoc($result))
{
$CountryNameFrm = $row['CountryName'];

echo '<option value="'.$row[CountryName].'" CountryFrm selected>'.$row[CountryName].'</option>';
}
?>
</select>

Can anybody help

Thanks in Advanced

Scoobie

jatar_k

7:13 pm on Jan 24, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



looking at your echo

<option value="'.$row[CountryName].'" CountryFrm selected>'.$row[CountryName].'</option>';

if your $row[CountryName] was Canada it would look like this

<option value="Canada" CountryFrm selected>Canada</option>

that syntax is a bit messed, your list would all be selected and I am not sure what CountryFrm is doing there.

you need something to compare the country to so you can echo selected for the appropriate option

scoobie

11:48 pm on Jan 24, 2005 (gmt 0)

10+ Year Member



hi,

i still couldn't get it to work. i was told that it is possible to open a recordset in dreamweaver mx. would you know how to do this

Scoobie.

coopster

1:56 am on Jan 30, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Hmmm, must not be too many DW users here ;)

Have you tried posting your question in the WYSIWYG and Text Code Editors [webmasterworld.com] Forum?