Jump to content

[SOLVED] PHP Drop Down box help


The.Pr0fess0r

Recommended Posts

Hello, I am currently working on an admin section for my site. I have a page that pulls in all the info in my database and then gives me the option to edit or delete the row of information.

 

The problem I am having is when editing my drop down lists I cannot get it to pull in the information in the database.

 

In the record entry screen (in the main part of my site) I am using the following code to populate the drop down box from fields in one of my db tables:

 

								<tr><td>Species:</td><td>
								<?
								$sql = "SELECT species FROM hunt_species";

									// Echo

									echo "<SELECT name=\"species1\">";

								$result = mysql_query($sql);

									// Loop to get and echo the results as options

									while ($row = mysql_fetch_assoc($result)) {

									echo '<option value="'.$row["species"].'">'
										.$row["species"].'</option>';

									}

									echo "</SELECT>";
								?>

 

And that code works great to populate the dropdown box.

 

The problem I am having now is that I don't know how to manipulate the above code to have it populate my dropdown box with the current value first and then give me the option (though the dropdown list) to change the value. All that is showing now is a dropdown box with the default value blank.

 

Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/174200-solved-php-drop-down-box-help/
Share on other sites

Hi The.Pr0fess0r,

 

If you check whether the current value matches the current result you can then echo a "selected" value.  Use something like the below in your while statement:

 

$sel = ( $category == $a['category'] ) ? ( 'selected' ) : ( NULL );

 

And then, edit your echo statement to read:

 

echo '<option value="'.$row["species"].'" '.$sel.'>'
										.$row["species"].'</option>';

 

Hope this helps.

Hi The.Pr0fess0r,

 

If you check whether the current value matches the current result you can then echo a "selected" value.  Use something like the below in your while statement:

 

$sel = ( $category == $a['category'] ) ? ( 'selected' ) : ( NULL );

 

Hope this helps.

 

I'm not sure what is going on in the above code? Could you explain what it is saying a bit more? The category variable in the example, do I need to replace that with species? Also, what does the 'a' mean in statement: $a['category']?

 

I'm just trying to get my head around what this statement is saying so I know how to edit it to get it to work with my code.

 

Thanks for all your help.

I am trying to acheive similar selection but for a listbox, this code retrieve and assigns only one value as selected value for the drop down box,

but how can this be extended to assign multiple values in the list box as selected values.

I tried this but even though for the code 1000 there are two citizenship values of T and I, the code just assigns the last value (I) as selected value

in the list box. the outer loop is for oracle database and in the inner (loop) calls are from MS SQL database.

 

<?php

                            //Oracle connection

if ($c = oci_pconnect ('','', "")) {

  //echo "Successfully connected to Oracle.\n";

$stmt = oci_parse($c, "select stvcitz_code as CODE, stvcitz_code ||' - '||stvcitz_desc as \"DESC\" from stvcitz");

oci_execute($stmt, OCI_DEFAULT);

echo "<select name=\"criCitz[]\" class=\"selectionval\" multiple=\"multiple\" size=\"4\" onblur=\"checkListCITZ(this.form)\"  id =\"criCitz\">";

  echo  "<option value='')></option>";

 

// start of MS SQL connection

 

$myServer = "";

$myUser = "";

$myPass = "";

$myDB = "";

 

$dbhandle = mssql_connect('','','')

    or die("Couldn't connect to SQL Server on $myServer");

 

$selected = mssql_select_db($myDB, $dbhandle)

or die("Couldn't open database $myDB");

 

 

 

 

  $query = "select DISTINCT CITZ from SD_CITZ WHERE CITZ_DTL_CD = '1000'

  AND CITZ_SEQ_NBR = (SELECT MAX(I.CITZ_SEQ_NBR) FROM SD_CITZ I WHERE I. CITZ_DTL_CD = '1000')";

 

 

$result = mssql_query($query); 

 

 

 

  while ($msrow = mssql_fetch_array($result,MSSQL_BOTH)) {

  $CITZ = $msrow['CITZ'];

 

  while ($row = oci_fetch_assoc($stmt)) {

$sel = ( trim($CITZ) == $row['CODE'] ) ? ( 'selected' ) : ( NULL );

echo  "<option value='".$row['CODE']."' '. $sel .'>".$row['DESC']."</option>";

}

 

 

    }

 

 

mssql_close($dbhandle);

 

 

echo "</select>";

oci_close($c);

} else {

  echo "Oracle Connect Error ";

}

?>

 

 

thanks for your help

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.