Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Guest
Mar 24, 2007 Mar 24, 2007
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

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


TOPICS
Server side applications
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Guide , Apr 10, 2007 Apr 10, 2007
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...
Translate
Guide ,
Mar 26, 2007 Mar 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 😉
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Mar 26, 2007 Mar 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 08, 2007 Apr 08, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 08, 2007 Apr 08, 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 08, 2007 Apr 08, 2007
It did not work. I have replaced what you asked.

I had constructed everything in Dreamweaver MX 2004 as far as the update page The code I am attaching is what dreamweaver produced..

Here is the code in all for the edit page. However I did do what you asked and it did not work. The drop down is there but it has not selected the original file.

- AdonaiEchad
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 08, 2007 Apr 08, 2007
OK, thanks, seeing the complete code of your page is better than racking my brain based on some fragments ;-)

I´ll try to reproduce this thing here and find a solution, but I´d need a SQL dump of your "files" table (e.g. by using PhpMyAdmin´s export features) -- at this point I think it´s better to not cram the DW Forums with such details, so could you please send the dump to info-at-cakmak-bauunternehmen.de ? However, reproducing this will take some time
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 08, 2007 Apr 08, 2007
Here you go the SQL.

- AdonaiEchad

----------------
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 09, 2007 Apr 09, 2007
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 😉
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 09, 2007 Apr 09, 2007
Wow, it actually works now. Thank you so much this is impressive. This helps me so much. I have been banging my head so much trying to get this to work. I am new to PHP. I have been helping a non profit organization and learning these extra scripts is quit challenging. Again, thank you for your assistance.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 09, 2007 Apr 09, 2007
Is there a way of sort ordering in ASC?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 10, 2007 Apr 10, 2007
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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 10, 2007 Apr 10, 2007
It works this is so awesome. You are brilliant. 🙂
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 13, 2007 Apr 13, 2007
Geschenk I am sorry to bother you again. Is there a solution to the error I am getting?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 13, 2007 Apr 13, 2007
Hi,

the script I provided (including the subsequent modification to cover your additional "can they be sorted ?" requirement) is definitely not something I´d personally call "perfect", because it does lack all sorts of error-trapping for situations other than your initial requirement "...that I can see all images in my directory" -- means that I´d now have to cover the previously unspecified "no or just 1 images in directory" situation as well by rewriting the scripts once again, and that´s - honestly said - not something I want to do at this point after having provided a custom-made solution which did what you wanted.

>>
Is there a solution to the error I am getting?
>>

yes -- simply make sure that you *do* have at least 2 images available in this directory, as before 😉 However, there are quite some excellent PHP forums available out there, and you´ll surely find someone there adding the additional "what if..." routines.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 11, 2007 Apr 11, 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 13, 2007 Apr 13, 2007
LATEST
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines