Jump to content

Selected variable value from populated drop down


jkatcherny

Recommended Posts

Hey all,

 

I'm using the code below to try to do 2 things

1 - populate data from a database, which is working fine

2 - choose the default selected value based on a session variable $_SESSION['requiredcomposerid'] - this is where the hang up is.

 

I added in some code to the $compOptions variable and now I'm stuck. Anyone have any thoughts? Thank you.

 

J

 

- - - - -

 

$compSql="SELECT composerid, compFirst, compLast FROM Composer_Info";
$compResult=mysql_query($compSql);
$compOptions="";
while ($row=mysql_fetch_array($compResult))
{
$composerid=$row['composerid'];
$compFirst=$row['compFirst'];
$compLast=$row['compLast'];
$compOptions.="<OPTION VALUE='$composerid'".if($_SESSION['requiredcomposerid'] == $composerid)."selected = 'selected'>".$compFirst." ".$compLast; // before I added the 'if' statement this was fine, here is where I'm trying to get the selected to work within a session
}

have you used session_start() ?

also, your html is poor in the last line. change it to the following

$compSql = "SELECT composerid, compFirst, compLast FROM Composer_Info";
$compResult = mysql_query($compSql);
$compOptions = "";
while ($row = mysql_fetch_array($compResult)) {
$composerid = $row['composerid'];
$compFirst = $row['compFirst'];
$compLast = $row['compLast'];
$selected = ($_SESSION['requiredcomposerid'] == $composerid) ? ' selected="selected"' : '';
$compOptions .= '<option value="'.$composerid.'"'.$selected.'>'.$compFirst.' '.$compLast.'>';
}

Hey Jay, thank you.

 

Yes, I do have session_start() - sorry I didn't include all the code.  What I had was this:

 

session_start();
include 'config.php';
include 'opendb.php';

// Populates COMPOSER list
$compSql="SELECT composerid, compFirst, compLast FROM Composer_Info";
$compResult=mysql_query($compSql);
$compOptions="";
while ($row=mysql_fetch_array($compResult)) 
{

    $composerid=$row['composerid'];
    $compFirst=$row['compFirst'];
    $compLast=$row['compLast'];
    $compOptions.="<OPTION VALUE='$composerid'>".$compFirst." ".$compLast;

}

 

That populated the drop down menu based on what I had in my database, which works great.  Now what I'm trying to do is, if the user needs to go back and change something (for which I already have a page coded), I want whatever they chose from the drop down menu previously, to be selected when they go back to make their changes.  This is what I was trying to achieve by the 'if' statement I had but I'm not sure of the exact syntax.  Does that make any sense? 

 

Thank you again for you help!

 

J

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.