Skip to main content
March 24, 2007
Answered

<option value"" selected >Info</option>

  • March 24, 2007
  • 4 replies
  • 2035 views
I have gathered some script where I created a drop down menu that I can see all images in my directory. However, I've to a point where I can add into my MySQL database the file name and using the drop down menu that uses it's own list from the directory to post it into the database. The problem I am receiving is that when I want to edit I can make my changes but the drop down menu does not select my item list that is attached. How can I change this?

See code below.

How do I get my option selected to select the correct name on the drop down menu?

Here is my MySQL

Thank you,
AdonaiEchad

-----------------------


This topic has been closed for replies.
Correct answer Günter_Schenk
Is there a way of sort ordering in ASC?
yes, ordering the file list in alphabetical order is possible -- please see the follwowing new script versions, which are somewhat rewritten to put the files in an "array" and then sort this array:

------------------ (that´s for your *update.php* file)
<?php
$dirname = $_SERVER['DOCUMENT_ROOT']."/files/media/";
$dir = opendir($dirname);

while(false !=($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != $row_rsFile['fileUpload']))
{
$filename[]=$file; // introduces the file array
sort($filename); // sorts the files array in alphabetical order
reset($filename);}
}

$file_list = "<option value='".$row_rsFile['fileUpload']."' selected>".$row_rsFile['fileUpload']."</option>\n";

$filecounter=0;
foreach($filename as $key=>$val)
{
if($filename[$filecounter])
{
$file_list .="<option value='$filename[$filecounter]'>$filename[$filecounter]</option>\n";
$filecounter++;
}
}

closedir($dir);
?>
------------------

------------------ (that´s for your *insert.php* file)
<?php
$dirname = $_SERVER['DOCUMENT_ROOT']."/files/media/";
$dir = opendir($dirname);

while(false !=($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
$filename[]=$file;
sort($filename);
reset($filename);}
}

$filecounter=0;
foreach($filename as $key=>$val)
{
if($filename[$filecounter])
{
$file_list .="<option value='$filename[$filecounter]'>$filename[$filecounter]</option>\n";
$filecounter++;
}
}

closedir($dir);
?>
------------------

just use your <?php echo($file_list); ?> as before

4 replies

April 13, 2007
Yea, that's what I thought about having more than one image in the directory. I can live with what is there. Again, Geschenk, I truly appreciate all your hard work.
April 12, 2007
There is a problem with the code.

If there is no images or files for either the insert.php or edit.php there is a error that says...
Warning: Invalid argument supplied for foreach() in C:\wamp\www\files\fileupload.php on line 46

It seems that the foreach($filename as $key=>$val) is drawing up an error.

However, if there is two images or files in the directory...
$dirname = $_SERVER['DOCUMENT_ROOT']."/files/media/";
there is no error. One or no images or files in the directory there is a error as stated above.

Can this error be fixed?
April 8, 2007
Maybe I am not getting it.

I have a list and when I edit it I can change the file name. The file name can be first or last name of a person. The file upload is actually lets say a image that I want to attach to it. So far when I create a new post I can add into my database the person's name then have a drop down menu underneath to select a image. When I pick the image it writes it into my MySQL database. So that works. Its when I want to edit it, that I can still make the changes, however, if I have lets say 4 images that are listed, when I want to edit the drop down menu that has the list of images does not select the one that was assigned to the database. The $row_rsFile['fileName'] is not my problem, it is the dropdown menu that is not selecting the correct image from the....

<td nowrap align="right">FileUpload:</td>
<td><select name="fileUpload">
<?php echo($file_list); ?>
</select>
</td>

This is what I need help in. Please see the code above for more details.
Günter_Schenk
Inspiring
April 8, 2007
Hi,

quote:

it is the dropdown menu that is not selecting the correct image from the....

<td nowrap align="right">FileUpload:</td>
<td><select name="fileUpload">
<?php echo($file_list); ?>
</select>
</td>


I think that I basically understood your problem, but please let me explain once more :: if you´re only using this:

<?php echo($file_list); ?>

within the <select>...</select>, the dropdown menu is technically not able to display a certain image as "selected" within the list.

quote:

I have gathered some script...


please don´t get me wrong, but that´s exactly the problem :: you´ve gathered some script from somewhere which is OK to use on your "insert new user" page -- but you actually cannot reuse the same script in unmodified form on your "update user" page, because this script as such has (when used on your "update" page) has 2 major shortcomings:

a) it will need a modification to display one certain image as "selected" -- right now it can´t do this

b) it will need some heavy modification for making it "connect" to your recordset -- the current script, simply said, doesn´t have any clue what you want it do

That´s why I suggested a little workaround in my first reply -- did you actually try this one, and if yes, did it work or not ? I´d really like to help you (also because this is an interesting scenario), but this is not easy without any response on things I suggested.
Günter_Schenk
Inspiring
April 9, 2007
Here you go the SQL.

- AdonaiEchad

----------------

Hi,

I managed to get it working -- please use the following modified script in your "update.php" file only:

-----------
<?php
$dirname = $_SERVER['DOCUMENT_ROOT']."/files/media/";

$dir = opendir($dirname);
$file_list = "<option value='".$row_rsFile['fileUpload']."' selected>".$row_rsFile['fileUpload']."</option>"; // new line !
while(false !=($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != $row_rsFile['fileUpload']) ) // extended condition
{
$file_list .="<option value='$file'>$file</option>";
}
}
closedir($dir);
?>
-----------

Some explanation on what´s the difference to your original script:

1. as the to-be-updated recordset must have an existing entry in the "fileUpload" field (because it´s been defined as NOT NULL), I´m skipping any "has value ?" checking and simply add the line...

$file_list = "<option value='".$row_rsFile['fileUpload']."' selected>".$row_rsFile['fileUpload']."</option>";

prior to (read: outside) the regular "file loop". You´ll notice that this line embeds the current recordset value "$row_rsFile['fileUpload']" within the option´s value and of course the displayed name -- and this option has been declared as "selected".

2. the following file loop hasn´t changed much, but I managed to exclude the existing "$row_rsFile['fileUpload']" value from the list by adding it to the "don´t show these files..." condition:

if(($file != ".") and ($file != "..") and ($file != $row_rsFile['fileUpload']))


That´s it, you should now be able to use your regular <?php echo($file_list); ?> in your select menu -- it did work for me ;-)
Günter_Schenk
Inspiring
March 26, 2007
hi,

when updating your form, using the <?php echo($file_list); ?> alone will just do what it was supposed to when inserting a new record :: display the names of all available files -- if you leave it like this, the <select>...</select> list has no binding to the recordset respectively the $row_rsFile['fileName'] - value.

I *think* you should be able to retrieve the existing record value by adding this snippet:

<?php if (strlen($row_rsFile['filename']) > 0) { // if recordset has a filename value (on update), display as selected option ?>
<option value="<?php echo $row_rsFile['filename']; ?>" selected><?php echo $row_rsFile['filename']; ?></option>
<?php }?>

as additional <option>...</option> directly after the <select name="fileUpload"> and have your regular file list added subsequently - complete example:

<select name="fileUpload">
<?php if (strlen($row_rsFile['filename']) > 0) {?>
<option value="<?php echo $row_rsFile['filename']; ?>" selected><?php echo $row_rsFile['filename']; ?></option>
<?php }?>
<?php echo($file_list); ?>
</select>

Does this work ? Please note :: if this works, you´ll of course see the stored filename twice -- both as selected value and once again within the file list -- but getting this sorted out is another topic ;-)
Günter_Schenk
Inspiring
March 26, 2007
Something else might be important to consider ::

1. as long you´ll keep the <input type="hidden" name="fileName" value="<?php echo $row_rsFile['fileName']; ?>"> - hidden field at the bottom of your form, your table field "fileName" will -- when updating the form -- always become overwritten with the previously stored file name.

2. <select name="fileUpload"> :: does your table really have a corresponding field named "fileUpload", or will you (as I reckon) be storing the file name in a field called "fileName" ? If your field is indeed called "fileName", you need to change the <select name="fileUpload"> to <select name="fileName">, because otherwise your database table can´t receive and store any selected value