Jump to content

[SOLVED] drop down menus - couple of questions


atl_andy

Recommended Posts

My form has a couple of drop down menus that are populated using a pgsql table.

 

1.  How do I get the drop down menus to separate from each other?  I have tried using <p> and <br />, but neither is working.  Currently they are right on top of each other.

 

2.  Is there a better way to handle this?  I will be using more drop down menus on the form and would like to have the code be as tight as possible.  This was the only way I could get the menus populated.

 

disclaimer:  I'm relatively new to php...only been using it for about three weeks.  Any help is appreciated.

 

<?php

  // Manufacturer Dropdown
   function create_dropdownMFR($identifierMFR,$pairsMFR,$identifierMFR,$multipleMFR="")
{
   // Start the dropdown list with the <select> element and title
   $dropdownMFR = "<select name=\"$identifierMFR\"> ";
   $dropdownMFR .= "<option name=\"\">Select Manufacturer</option>";
   // Create the dropdown elements
   foreach($pairsMFR AS $valueMFR => $nameMFR)
   {
       $dropdownMFR .= "<option name=manufacturer>$nameMFR</option>";
   }
   // Conclude the dropdown and return it
   echo "</select>";
   return $dropdownMFR;
}

   //Connect to the db server and select a database
   $db_handleMFR = pg_connect('dbname=test user=user password=secret');

   // Retrieve the manufacturer table data
   $queryMFR = "SELECT record_id, manufacturer FROM dropdown ORDER BY manufacturer";
   $resultMFR = pg_query($db_handleMFR, $queryMFR);

   // Create an associative array based on the table data
   while($rowMFR = pg_fetch_array($resultMFR))
   {
      $valueMFR = $rowMFR['record_id'];
      $nameMFR = $rowMFR['manufacturer'];
      $pairsMFR[$valueMFR] = $nameMFR;
   }

   echo "Manufacturer: ";
   echo create_dropdownMFR("dropdown",$pairsMFR,"");
?>

<?php
   // Model Number Dropdown
   function create_dropdownModel($identifierModel,$pairsModel,$identifierModel,$multipleModel="")
{
   // Start the dropdown list with the <select> element and title
   $dropdownModel = "<select name=\"$identifierModel\"> ";
   $dropdownModel .= "<option name=\"\">Select Model Number</option>";
   // Create the dropdown elements
   foreach($pairsModel AS $valueModel => $nameModel)
   {
       $dropdownModel .= "<option name=modelnumber>$nameModel</option>";
   }
   // Conclude the dropdown and return it
   echo "</select>";
   return $dropdownModel;
}

   //Connect to the db server and select a database
   $db_handleModel = pg_connect('dbname=test user=user password=secret');

   // Retrieve the manufacturer table data
   $queryModel = "SELECT record_id, manufacturer FROM dropdown ORDER BY manufacturer";
   $resultModel = pg_query($db_handleModel, $queryModel);

   // Create an associative array based on the table data
   while($rowModel = pg_fetch_array($resultModel))
   {
      $valueModel = $rowModel['record_id'];
      $nameModel = $rowModel['manufacturer'];
      $pairsModel[$valueModel] = $nameModel;
   }

   echo "Model: ";
   echo create_dropdownModel("dropdown",$pairsModel,"");

?>

 

 

<html>

   <head>
     <title>Audit Form</title>
     <link href="style.css" type="text/css" rel="stylesheet" />
   </head>
<body>
<fieldset class="fieldset">
<form action="subscribe.php" method="post">
  <div class="div0">
    <h3>Notebook Audit Form</h3><p>Project Number:<input type="text" name="projectnumber" size="10" maxlength="20" value="" />User ID:<input type="text" name="userid" size="10" maxlength="20" value="" /></p>
  </div>

<div class="div1">
<?php

  // Manufacturer Dropdown
   function create_dropdownMFR($identifierMFR,$pairsMFR,$identifierMFR,$multipleMFR="")
{
   // Start the dropdown list with the <select> element and title
   $dropdownMFR = "<select name=\"$identifierMFR\"> ";
   $dropdownMFR .= "<option name=\"\">Select Manufacturer</option>";
   // Create the dropdown elements
   foreach($pairsMFR AS $valueMFR => $nameMFR)
   {
       $dropdownMFR .= "<option name=manufacturer>$nameMFR</option>";
   }
   // Conclude the dropdown and return it
   echo "</select>";
   return $dropdownMFR;
}

   //Connect to the db server and select a database
   $db_handleMFR = pg_connect('dbname=test2 user=user password=secret');

   // Retrieve the manufacturer table data
   $queryMFR = "SELECT record_id, manufacturer FROM dropdown ORDER BY manufacturer";
   $resultMFR = pg_query($db_handleMFR, $queryMFR);

   // Create an associative array based on the table data
   while($rowMFR = pg_fetch_array($resultMFR))
   {
      $valueMFR = $rowMFR['record_id'];
      $nameMFR = $rowMFR['manufacturer'];
      $pairsMFR[$valueMFR] = $nameMFR;
   }

   echo "Manufacturer: ";
   echo create_dropdownMFR("dropdown",$pairsMFR,"");
?>

<?php
   // Model Number Dropdown
   function create_dropdownModel($identifierModel,$pairsModel,$identifierModel,$multipleModel="")
{
   // Start the dropdown list with the <select> element and title
   $dropdownModel = "<select name=\"$identifierModel\"> ";
   $dropdownModel .= "<option name=\"\">Select Model Number</option>";
   // Create the dropdown elements
   foreach($pairsModel AS $valueModel => $nameModel)
   {
       $dropdownModel .= "<option name=modelnumber>$nameModel</option>";
   }
   // Conclude the dropdown and return it
   echo "</select>";
   return $dropdownModel;
}

   //Connect to the db server and select a database
   $db_handleModel = pg_connect('dbname=test2 user=user password=secret);

   // Retrieve the manufacturer table data
   $queryModel = "SELECT record_id, manufacturer FROM dropdown ORDER BY manufacturer";
   $resultModel = pg_query($db_handleModel, $queryModel);

   // Create an associative array based on the table data
   while($rowModel = pg_fetch_array($resultModel))
   {
      $valueModel = $rowModel['record_id'];
      $nameModel = $rowModel['manufacturer'];
      $pairsModel[$valueModel] = $nameModel;
   }

   echo "Model Number: ";
   echo create_dropdownModel("dropdown",$pairsModel,"");

?>

   <input type="submit" name="submit" value="Submit to Database" />
  </div>
</form>
</fieldset>

</body>

</html>

hence the disclaimer...

 


<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
    <title>Enter New Assets</title>
    <link href="style.css" type="text/css" rel="stylesheet" />
    </title>
   </head>
<body>

  <ul class="div5" id="navcontainer">
    <li><a href="pc.html" target="iframe0">Add PC</a></li>

    <li><a href="monitor.html" target="iframe0">Add Monitor</a></li>
    <li><a href="notebook.php" target="iframe0">Add Notebook</a></li>
    <li><a href="server.html" target="iframe0">Add Server</a></li>
    <li><a href="printer.html" target="iframe0">Add Printer</a></li>
    <li><a href="home.html">Return To Main Page</a></li>
  </ul>

  <iframe name="iframe0" src="" float="right" scrolling="auto" height="100%" width="75%"></iframe>

</body>

</html>

Show me the source where the drop down menus are outputed.

 

 

weird, this should work just fine

 

   function create_dropdownMFR($identifierMFR,$pairsMFR,$identifierMFR,$multipleMFR="")
{
   // Start the dropdown list with the <select> element and title
   $dropdownMFR = "<select name=\"$identifierMFR\"> ";
   $dropdownMFR .= "<option name=\"\">Select Manufacturer</option>";
   // Create the dropdown elements
   foreach($pairsMFR AS $valueMFR => $nameMFR)
   {
       $dropdownMFR .= "<option name=manufacturer>$nameMFR</option>";
   }
   // Conclude the dropdown and return it
   echo "</select><br><br>";
   return $dropdownMFR;
}

and ...

this line

 

  foreach($pairsMFR as $valueMFR => $nameMFR)

  {

      $dropdownMFR .= "<option value='manufacturer'>$nameMFR</option>";

  }

  // Conclude the dropdown and return it

  echo "</select><br><br>";

 

 

should be

   foreach($pairsMFR as $valueMFR => $nameMFR)
   {
       $dropdownMFR .= "<option value='manufacturer'>$nameMFR</option>";
   }
   // Conclude the dropdown and return it
  $dropdownMFR .= "</select><br><br>";

Try selections

 

$slection= $_POST[something'];

}

if($something=="1")

{

$something="Something";

}

elseif($something=="2")

{

$something="Something";

}

else

{

$something="something";

}

 

And in forms

 

<select name='something'>

<option value='1'>something</option>

<option value='2'>something</option>

</select>

Eventually there will be quite a few items in the drop downs and I figured it would be easier to manage with a db table instead of a long select.  There are other features I want to add that will select only the model numbers for particular manufacturers so the list doesn't blow up the user's screen.  However, given my n00bness, will a select work better?

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.