Jump to content

Dynamic dropdown boxes


alanl1

Recommended Posts

Hi Professionals.

 

I am struggling with some code I wrote

I calculate the number of column headings from a csv and produce populated dynamic dropdown boxes based on the headings and number of columns.

 

on the other side I have dropdown box static values where underneath second dropdown changes based on the selection from the first dropdown.

 

this works fine however only for the first two boxes

 

 

EG if i have the first dropdown set to "DeviceID", "Manufacturer","Product","Version","NULL","Ignore" and i select "DeviceID"

 

the second dropdown changes to "Manufacturer","Product","Version","NULL","Ignore" so "DeviceID" is no longer available for selection

 

but in the third box or forth and so on "DeviceID" comes back which I do not want this available for selection as it has already been selected in the first box

 

hope you understand what i mean

 

Please find my code below

 

<head>
<script type="text/javascript">

var categories = [];

 categories["startList"] = ["DeviceID","Manufacturer","Product","Version","NULL","Ignore"]
 categories["Manufacturer"] = ["DeviceID","Product","Version","NULL","Ignore"];
 categories["DeviceID"] = ["Manufacturer","Product","Version","NULL","Ignore"];
 categories["Product"] = ["DeviceID","Manufacturer","Version","NULL","Ignore"];
 categories["Version"] = ["DeviceID","Manufacturer","Product","NULL","Ignore"];
 categories["NULL"] = ["DeviceID","Manufacturer","Product","Version","NULL","Ignore"];
 categories["Ignore"] = ["DeviceID","Manufacturer","Product","Version","NULL","Ignore"];

function fillSelect(sel,ary,nxt){
 if (ary&&sel.form){
  var frm=sel.form,nme=sel.name.replace(/\d/g,""),i=Number(sel.name.replace(/\D/g,""))+1,nxt=frm[nxt],z0=0;
  while (frm[nme+i]){
   frm[nme+i].length = 1;
   frm[nme+i].selectedIndex=0;
   i++;
  }
  for (;z0<ary.length;z0++){
   nxt.options[z0+1]=new Option(ary[z0],ary[z0]);
  }
  nxt.selectedIndex=0;
 }
}

function getValue(isValue) {
 alert(isValue);
}

function init() {
 fillSelect(document.forms[0]['dropdown0'],categories['startList'],'dropdown0')
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

function myfunction()
{
alert("Please ensure the file is in csv format");
}
 </script>
</head>
 <form action=""><?php
 
$newname = $_GET['newname'];  //The file that has been moved into the Uploads/ folder will need to be stripped here to retreive just the filename...
$filename = basename($newname,"/");  //basename function strips off filename based on first forward slash "/"
$counter = 1;
function ext($file) {
 return substr(strrchr($file,'.'),1);
}
if (isset($filename)) {
$cfile = $filename;
$cfile = ext($cfile);
if ($cfile == 'csv'){
//do nothing its all good..
}
else {
 
 ?><script type="text/javascript">
    myfunction();
</script><?php
header("refresh:1; url=preupload.php");
}
}
    if (($handle = fopen($filename, "r")) !== FALSE)    
 {        
 $length = 1000;        
 $delimiter = ",";   ?>   
 <?php
 $rows = 0;  // ADDED        
 while ( ( $data = fgetcsv( $handle, $length, $delimiter ) ) !== FALSE )        
 {        
 if( $rows == 0 )// or i = 3 using pipe command|| $i == 3 ) // ADDED        
 {                
   $num = count($data);                
    
   for ($c=0; $c < $num; $c++)                
    { 
   echo "<table width=100% border=0>";
   echo "<tr width=33% align=right><td>";
    ?><select name="<?php echo $data[$c]?>"><?php
    //Second array element populates the dropdown and calculates the number of columns from spreadsheet
    for ($d=0; $d < $num; $d++)                
    {
     echo "<option value='" .$data[$d]."'>" .$data[$d]."</option>";
   }
    ?></select><br /><?php
    echo "</td>";
      echo "<td width=33% align=left>";
      ?> <---- Matches to -----> <select name="<?php echo "dropdown" .$c?>" onchange="fillSelect(this,categories[this.value],'<?php echo "dropdown" .$counter++ ?>')"><?php
     
      echo "<option selected>Please Choose</option>";
      echo "<option value='DeviceID'>DeviceID</option>";
      echo "<option value='Product'>Product</option>";
      echo "<option value='Manufacturer'>Manufacturer</option>";
      echo "<option value='Version'>Version</option>";
      echo "<option value='NULL'>NULL</option>";
      echo "<option value='Ignore'>Ignore</option>";   
      //echo "<option value='" .$data[$e]."'>" .$data[$e]."</option>";
      ?></select><?php
      echo "</td>";
     echo "<td width=33%> </td>";
    echo "</tr>";
    echo "</table>"; 
    }                                 
 }       
 $rows++; // ADDED      
 }              
  fclose($handle);    
}
?></form>

 

 

Link to comment
https://forums.phpfreaks.com/topic/278623-dynamic-dropdown-boxes/
Share on other sites

  • 2 weeks later...

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.