Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

populate drop down from file listing

Posted on 2009-07-15
4
155 Views
Last Modified: 2012-05-07
I am having issues connecting the dots here.  I have a bit of php that will list each file in a folder.  I want to take that data and populate a pulldown.  Here is the code I have.  I know it is something simple, but I just can't piece it together.

Thanks
<?
$path = "../media";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
        echo "$i. <a href='$path/$file'>$file</a><br />";
?>
		<script="javascript">
		var objDropdown = document.getElementById('files');
		var objOption = new Option(<? $file ?>,<? $file ?>);
		document.write(objOption.value);
		objDropdown.options[objDropdown.length] = objOption;
		</script>
<?
        $i++;
    }
}
closedir($dh);
?>

Open in new window

0
Comment
Question by:prostang
  • 2
  • 2
4 Comments
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 24865347
What is the surrounding code? Can you post the whole page?
0
 

Author Comment

by:prostang
ID: 24865525
Here is most of the code for the page (I also included the original code).  above this is database connection and behaviors in PHP.
<?
$path = "../media";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
        echo "$i. <a href='$path/$file'>$file</a><br />";
?>
		<script="javascript">
		var objDropdown = document.getElementById('files');
		var objOption = new Option(<? $file ?>,<? $file ?>);
		document.write(objOption.value);
		objDropdown.options[objDropdown.length] = objOption;
		</script>
<?
        $i++;
    }
}
closedir($dh);
?>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Award Winning Sausage</title>
<link href="../NewSite/css/JSS_styles.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
 
 
#MainDiv {
	height: 500px;
	width: 900px;
	border: medium solid #FFFFFF;
	margin-right: auto;
	margin-left: auto;
	background-color: #8CC63F;
}
 
/* CSS Document */
 
#header {
	height: 200px;
	width: 900px;
	background-image: url(../images/JoeSHeaderDatabase.png);
	margin-right: auto;
	margin-left: auto;
}
body {
	background-repeat: no-repeat;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	background-color: #FDB200;
}
-->
</style>
<script src="../NewSite/Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
<link href="../css/JSS_styles.css" rel="stylesheet" type="text/css" />
</head>
 
<body>
<div class="header" id="header"></div>
<div class="MenuDiv" id="MenuDiv"><a name="Menu" id="Menu"></a>
  <table width="900" border="0" align="center">
    <tr>
      <td width="100">&nbsp;</td>
      <td width="300"><div align="center"><a href="../NewSite" target="_blank">Main Site</a></div></td>
      <td width="300"><div align="center"><a href="./">Admin Home</a></div></td>
      <td width="100">&nbsp;</td>
    </tr>
  </table>
</div>
<div class="MainDiv" id="MainDiv">
  <p>&nbsp;</p>
    <p>&nbsp;</p>
 
    
    <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
      <table width="500" border="0" align="center">
        <tr>
          <td width="410"><div align="right">Date:</div></td>
          <td width="19">&nbsp;</td>
          <td width="57"><input type="text" name="date" id="date" /></td>
        </tr>
        <tr>
          <td><div align="right">Message:</div></td>
          <td>&nbsp;</td>
          <td><textarea name="message" cols="50" rows="3" id="message"></textarea></td>
        </tr>
        <tr>
          <td><div align="right">Permanent:</div></td>
          <td>&nbsp;</td>
          <td><input type="checkbox" name="permanent" id="permanent" /></td>
        </tr>
        <tr>
          <td colspan="3"><div align="center">
            <select name="files" id="files">
            </select>
            </div></td>
        </tr>
        <tr>
          <td colspan="3">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="3"><div align="center">
            <input type="submit" name="sumit" id="sumit" value="Submit" />
            <input type="reset" name="cancel" id="cancel" value="Cancel" />
</div></td>
        </tr>
      </table>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <input type="hidden" name="MM_insert" value="form1" />
    </form>
    <script>
var mydate=new Date()
var theyear=mydate.getYear()
if (theyear < 1000)
theyear+=1900
var theday=mydate.getDay()
var themonth=mydate.getMonth()+1
if (themonth<10)
themonth="0"+themonth
var theday=mydate.getDate()
if (theday<10)
theday="0"+theday
 
//////EDIT below three variable to customize the format of the date/////
 
var displayfirst=theyear
var displaysecond=themonth
var displaythird=theday
 
////////////////////////////////////
 
document.form1.date.value=displayfirst+"-"+displaysecond+"-"+displaythird
</script>
    </div>
<div id="Footer"> <a name="FooterAnchor" id="FooterAnchor"></a>Copyright &copy; 2009 Joe S. Sausage | All Rights Reserved | Website Designed by: <a href="http://www.mjm-consulting.com" target="_blank">MJM Consulting, LLC</a></div>
</body>
</html>
<?php
mysql_free_result($messages);
?>

Open in new window

0
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 250 total points
ID: 24865858
Try:

$selectOptions = "";
$dh = opendir($path);
$i=1;
while (($file = readdir($dh)) !== false) {
    if($file != "." && $file != "..") {
        echo "$i. <a href='$path/$file'>$file</a><br />";
        $selectOptions .= "<option value='{$file}'>{$file}</option>\n";
        $i++;
    }
}
closedir($dh);

then go down the page to your <select> tag and use $selectOptions:
            <select name="files" id="files">
            <? print $selectOptions; ?>
            </select>
0
 

Author Closing Comment

by:prostang
ID: 31604047
That was it.  Thanks!
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Undefined variable problem 5 23
Javascript 2 24
troubleshoot a python script 8 24
Generate PDF from MySQL using PHP 3 23
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article will show, step by step, how to integrate R code into a R Sweave document
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question