Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Dynamic Drop Down Box

Codes here !

Moderators: macek, egami, gesf

Dynamic Drop Down Box

Postby fuller » Mon May 21, 2012 11:57 am

Hello all. I am pretty new to PHP programming and mysql but so far have actually had a pretty good go at teaching myself but I have finally hit a wall that I just can not comprehend. Yes, I have used google. (A LOT) but I can't not find an example that I can comprehend. I'm hoping the experts here can help me out. I have 3 tables. (2 data tables, and 1 junction table) I have a drop down that will dynamically create the table query from the first drop down but I have no idea how to make the second drop down box dynamically dependent on the first then to also update my results. Here is my code and honestly I'm not really sure how the script portion of it works that is creating the table. Any help would very much be appreciated.

Here is the code for the main page.

Code: Select all
<html>
<head>
<?php
session_start();
   
require 'connection.php';
require 'variable_set.php';
require 'auth_check.php';

?>

<script type="text/javascript">
function showProject(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getproject.php?q="+str,true);
xmlhttp.send();
}
</script>

</head>
<body>

<form>
<select name="Project" onchange="showProject(this.value)">
<option value="">Select a project:</option>                 
<?php
    $projquery="SELECT Projects.id_Project,Projects.`Project` FROM Projects";
    $projresult=mysql_query($projquery) or die ("Query to get data from firsttable failed:".mysql_error());
    while ($projrow=mysql_fetch_array($projresult)){
      $project=$projrow[Project];
      $idproject=$projrow[id_Project];
        echo "<option value=$idproject>$project</option>\n";                                                                   
        }
?>
                         </select>
</form>
<div id="txtHint"><b>Project Status will be listed here.</b></div>
<br/>
<br/>
<br />


</body>
</html>


Get Project Code

Code: Select all
<?php
session_start();

$q=$_GET["q"];
$_SESSION['$q'] = $q;

//if ($a != null) {
//   unset($a);
//   }

require 'connection.php';
require 'variable_set.php';
require 'auth_check.php';


//$sql="SELECT * FROM user WHERE id = '".$q."'";
$sql="SELECT Projects.Project AS `Project`
     , Assemblies.Assembly AS `Assembly`
     , Ditmco_Programming_Status.Programming_Status AS `Ditmco Programming`
     , Ditmco_Harness_Status.Harness_Status AS `Ditmco Adapters`
     , Torque_Programming_Status.Programming_Status AS `Torque Programming`
     , Torque_Adapter_Status.Adapter_Status AS `Torque Adapters`
     , Tooling_Status.Tooling AS Tooling
FROM
  Project_Status
INNER JOIN Projects
ON Project_Status.Project = Projects.id_Project
INNER JOIN Assemblies
ON Project_Status.Assembly = Assemblies.id_Assembly
INNER JOIN Project_Has_Assembly
ON Project_Has_Assembly.Project = Projects.id_Project AND Project_Has_Assembly.Assembly = Assemblies.id_Assembly
INNER JOIN Ditmco_Programming_Status
ON Project_Status.Ditmco_Programming = Ditmco_Programming_Status.id_Programming_Status
INNER JOIN Ditmco_Harness_Status
ON Project_Status.Ditmco_Harnesses = Ditmco_Harness_Status.id_Harness_Status
INNER JOIN Torque_Programming_Status
ON Project_Status.Torque_Program = Torque_Programming_Status.id_Programming_Status
INNER JOIN Torque_Adapter_Status
ON Project_Status.Torque_Adapters = Torque_Adapter_Status.id_Adapter_Status
INNER JOIN Tooling_Status
ON Project_Status.Tooling = Tooling_Status.id_Tooling
WHERE
  Projects.id_Project ='$q'
GROUP BY
  Projects.Project
, Assemblies.Assembly";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Project</th>
<th>Assembly</th>
<th>Ditmco Programming</th>
<th>Ditmco Adapters</th>
<th>Torque Programming</th>
<th>Torque Adapters</th>
<th>Tooling</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Project'] . "</td>";
  echo "<td>" . $row['Assembly'] . "</td>";
  echo "<td>" . $row['Ditmco Programming'] . "</td>";
  echo "<td>" . $row['Ditmco Adapters'] . "</td>";
  echo "<td>" . $row['Torque Programming'] . "</td>";
  echo "<td>" . $row['Torque Adapters'] . "</td>";
  echo "<td>" . $row['Tooling'] . "</td>";
  echo "</tr>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>


The query for the Assembly would look something like this:

Code: Select all
<?php
    $assyquery="SELECT Assemblies.Assembly FROM Project_Has_Assembly
INNER JOIN Assemblies
ON Project_Has_Assembly.Assembly = Assemblies.id_Assembly
WHERE
  Project_Has_Assembly.Project = '$idproject'";
    $assyresult=mysql_query($assyquery) or die ("Query to get data from firsttable failed:".mysql_error());
    while ($assyrow=mysql_fetch_array($assyresult)){
      $assembly=$assyrow[Assembly];
      $idassembly=$assyrow[id_Assembly];
        echo "<option value=$idassembly>$assembly</option>\n";                                                                   
        }
?>


What I would really like to do now is have a second drop down for Assembly that would be generated when the project is chosen. Which would in turn update the table.
fuller
New php-forum User
New php-forum User
 
Posts: 1
Joined: Sat May 19, 2012 8:40 am

Return to mySQL & php coding

Who is online

Users browsing this forum: CharlyAnderson and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.