Jump to content

Multiple dropdowns in loop problem


closerwalk

Recommended Posts

Hopefully I can explain this well, I have a timesheet that has two dropdowns that are populated from an access db.  I have on the first dd box onchange submit back to the page the number of the field.  What I want to do is make this unique to each drop down, in other words I want if 7:oo I select 9 from the dropdown it to retain 9 and to fileter associated data in dropbox II by passing that number to the sql statement. Also on submit the number passed filters the data in the second dd box and that would need to be independent. How would I go about this?  The code is below, and help is greatly appreciated.

 

<?php

echo '<table border="1" width="105%" style="table-layout:fixed">';
echo '<form action="'. $_SERVER['PHP_SELF'] .'" method="post">';

IF ($_GET['phase']) {
echo "<script>alert(this is what was posted:)".$_GET['phase']."</script>";
$phaseno = "like '".$_GET['phase']."'" ;
}
else {
$phaseno = "";
}
echo '<col width=10>';
echo '<col width=20>';
echo '<col width=20>';
echo '<col width=121>';
echo '<col width=45>';
echo '<col width=65>';
echo '<col width=50>';
echo '<col width=65>';
echo '<th style="height:35px">EMPL NO.</th>';
echo '<td><input type="text" name="EMP_ID" style="width=65"></td>';
echo '<th style="height:35px">NAME:</th>';
echo '<td><input type="text" name="NAME" style="width=400"></td>';
echo '<th style="height:35px">DATE:</th>';
echo '<td><input type="text" name="DATE" style="width=150"></td>';
echo '<th style="height:35px">PERIOD ENDING:</th>';
echo '<td><input type="text" name="PERIOD_END" style="width=150"></td>';
echo '<table border="1" width="100%" style="table-layout:fixed">';
echo '<tr>';
echo '<th style="height:45px">TIME</th>';
echo '<th style="height:45px">PROJECT NO.</th>';
echo '<th style="height:45px">Phase#</th>';
echo '<th style="height:45px">Cost Code</th>';
echo '<th style="height:45px">Admin.</th>';
echo '<th style="height:45px">Draft.</th>';
echo '<th style="height:45px">Feas Study</th>';
echo '<th style="height:45px">Research & Permit</th>';
echo '<th style="height:45px">Eng</th>';
echo '<th style="height:45px">JPA</th>';
echo '<th style="height:45px">Inspect</th>';
echo '<th style="height:45px">Plan Lease</th>';
echo '<th style="height:45px">Project Mgmt.</th>';
echo '<th style="height:45px" >OT</th>';
echo '<th style="height:45px">DT</th>';
echo '<th style="height:45px">MU</th>';
echo '<th style="height:45px">Miles</th>';
echo '<th style="height:45px">Holiday</th>';
echo '<th style="height:45px">P.T.O.</th>';
echo '<th style="height:45px">Vacation</th>';
echo '<th style="height:45px">UPT - Off</th>';
echo '<th style="height:45px">Drawing# /ACO#/ SBC Project#/Wire Center</th>';
echo '<col width=45>';
echo '<col width=150>';
echo '<col width=40>';
echo '<col width=35>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=20>';
echo '<col width=20>';
echo '<col width=20>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=300>';
$time[1] = '7:00';
$time[2] =  '7:15' ;
$time[3] =  '7:30' ;
$time[4] =  '7:45';
$time[5] =  '8:00 ';
$time[6] =  '8:15' ;
$time[7] =  '8:30 ';
$time[8] =  '8:45 ';
$time[9] =  '9:00' ;
$time[10] =  '9:15' ;
$time[11] =  '9:30 ';
$time[12] =  '9:45' ;
$time[13] =  '10:00 ';
$time[14] =  '10:15' ;
$time[15] =  '10:30' ;
$time[16] =  '10:45' ;
$time[17] =  '11:00' ;
$time[18] =  '11:15' ;
$time[19] =  '11:45 ';
$time[20] =  '12:00' ;
$time[21] =  '12:30 ';
$time[22] =  '12:45 ';
$time[23] =  '1:00 ';
$time[24] =  '1:15' ;
$time[25] =  '1:30' ;
$time[26] =  '1:45 ';
$time[27] =  '2:00' ;
$time[28] =  '2:15 ';
$time[29] =  '2:30 ';
$time[30] =  '2:45 ';
$time[31] =  '3:00' ;
$time[32] =  '3:15 ';
$time[33] =  '3:30';
$time[34] =  '3:45 ';
$time[35] =  '4:00 ';
$time[36] =  '4:15';
$time[37] =  '4:30 ';
$time[38] =  '4:45';

$conn = new COM('ADODB.Connection') or die('Could not make conn'); 
$conn1 = new COM('ADODB.Connection') or die('Could not make conn'); 
$rs = new COM('ADODB.Recordset') or die('Coult not make rs'); 
$rs1 = new COM('ADODB.Recordset') or die('Coult not make rs'); 
$connstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\InetPub\\wwwroot\\phasecodes.mdb"; 
$v = 0;
$conn->Open($connstring); 
$conn1->Open($connstring);
//first dropdown box
$sql = "SELECT DISTINCT `phase` FROM cost_coding  ORDER BY `phase`"; 

$rs->Open($sql, $conn, 1, 3); 

$phase .= '<select onChange="window.location.href=\'timesheet_form.php?phase=\'+this.value" name="phase">';
	while(!$rs->EOF ) { 

$phase .= '<option name="phase" value="'.str_replace(" ","",$rs->Fields['phase']).'">'. $rs->Fields['phase']. '</option>'; 
	$rs->MoveNext(); 

	}
//second dropdown box
$sql1 = "SELECT `cost_code` FROM cost_coding WHERE `phase` ".$phaseno." ORDER BY `cost_code`"; 

$rs1->Open($sql1, $conn1, 1, 3);
$costcode .= '<select>';
	while(!$rs1->EOF ) { 

$costcode .= '<option name="cost_code'.$v++.'" value"'. $rs1->Fields['cost_code']. '">'. $rs1->Fields['cost_code']. '</option>';

	$rs1->MoveNext(); 

	}
$costcode .= '</select>';



for($i=1; $i<=37; $i++) {


 echo '<tr style="height:20px">';
	echo '<td><input type="text" name="TIME" value="'.$time[$i].'"></td>';
	echo '<td><input type="text" name="PROJECT_NO"></td>';
	echo '<td>';
	echo $phase;
	echo '</td>';
	echo '<td>';
	echo $costcode;
	echo '</td>';
	echo '<td><input type="text" name="Admin"> </td>';
	echo '<td><input type="text" name="Draft"> </td>';
	echo '<td><input type="text" name="Feas_Study"> </td>';
	echo '<td><input type="text" name="Research_Permit"> </td>';
	echo '<td><input type="text" name="ENG"> </td>';
	echo '<td><input type="text" name="JPA"> </td>';
	echo '<td><input type="text" name="Inspect"> </td>';
	echo '<td><input type="text" name="Plan_Lease"> </td>';
	echo '<td><input type="text" name="Project_Mgmt"> </td>';
	echo '<td><input type="text" name="OT"> </td>';
	echo '<td><input type="text" name="DT"> </td>';
	echo '<td><input type="text" name="MU"> </td>';
	echo '<td><input type="text" name="MILES"> </td>';
	echo '<td><input type="text" name="HOLIDAY"> </td>';
	echo '<td><input type="text" name="PTO"> </td>';
	echo '<td><input type="text" name="VACATION"> </td>';
	echo '<td><input type="text" name="UPTO"> </td>';
	echo '<td><input type="text" name="STATUS" style="width=300"> </td>';



}

echo " </td></tr></table>";

$conn->Close();
$rs=null; 
$conn=null; 
echo "<P></P>";
echo "SIGNATURE:___________________________________________________________"
//echo '</form>';
?>

Link to comment
https://forums.phpfreaks.com/topic/202280-multiple-dropdowns-in-loop-problem/
Share on other sites

  • 1 month later...

Hopefully I can explain this well, I have a timesheet that has two dropdowns that are populated from an access db.  I have on the first dd box onchange submit back to the page the number of the field.  What I want to do is make this unique to each drop down, in other words I want if 7:oo I select 9 from the dropdown it to retain 9 and to fileter associated data in dropbox II by passing that number to the sql statement. Also on submit the number passed filters the data in the second dd box and that would need to be independent. How would I go about this?  The code is below, and help is greatly appreciated.

 

<?php

echo '<table border="1" width="105%" style="table-layout:fixed">';
echo '<form action="'. $_SERVER['PHP_SELF'] .'" method="post">';

IF ($_GET['phase']) {
echo "<script>alert(this is what was posted:)".$_GET['phase']."</script>";
$phaseno = "like '".$_GET['phase']."'" ;
}
else {
$phaseno = "";
}
echo '<col width=10>';
echo '<col width=20>';
echo '<col width=20>';
echo '<col width=121>';
echo '<col width=45>';
echo '<col width=65>';
echo '<col width=50>';
echo '<col width=65>';
echo '<th style="height:35px">EMPL NO.</th>';
echo '<td><input type="text" name="EMP_ID" style="width=65"></td>';
echo '<th style="height:35px">NAME:</th>';
echo '<td><input type="text" name="NAME" style="width=400"></td>';
echo '<th style="height:35px">DATE:</th>';
echo '<td><input type="text" name="DATE" style="width=150"></td>';
echo '<th style="height:35px">PERIOD ENDING:</th>';
echo '<td><input type="text" name="PERIOD_END" style="width=150"></td>';
echo '<table border="1" width="100%" style="table-layout:fixed">';
echo '<tr>';
echo '<th style="height:45px">TIME</th>';
echo '<th style="height:45px">PROJECT NO.</th>';
echo '<th style="height:45px">Phase#</th>';
echo '<th style="height:45px">Cost Code</th>';
echo '<th style="height:45px">Admin.</th>';
echo '<th style="height:45px">Draft.</th>';
echo '<th style="height:45px">Feas Study</th>';
echo '<th style="height:45px">Research & Permit</th>';
echo '<th style="height:45px">Eng</th>';
echo '<th style="height:45px">JPA</th>';
echo '<th style="height:45px">Inspect</th>';
echo '<th style="height:45px">Plan Lease</th>';
echo '<th style="height:45px">Project Mgmt.</th>';
echo '<th style="height:45px" >OT</th>';
echo '<th style="height:45px">DT</th>';
echo '<th style="height:45px">MU</th>';
echo '<th style="height:45px">Miles</th>';
echo '<th style="height:45px">Holiday</th>';
echo '<th style="height:45px">P.T.O.</th>';
echo '<th style="height:45px">Vacation</th>';
echo '<th style="height:45px">UPT - Off</th>';
echo '<th style="height:45px">Drawing# /ACO#/ SBC Project#/Wire Center</th>';
echo '<col width=45>';
echo '<col width=150>';
echo '<col width=40>';
echo '<col width=35>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=45>';
echo '<col width=20>';
echo '<col width=20>';
echo '<col width=20>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=65>';
echo '<col width=300>';
$time[1] = '7:00';
$time[2] =  '7:15' ;
$time[3] =  '7:30' ;
$time[4] =  '7:45';
$time[5] =  '8:00 ';
$time[6] =  '8:15' ;
$time[7] =  '8:30 ';
$time[8] =  '8:45 ';
$time[9] =  '9:00' ;
$time[10] =  '9:15' ;
$time[11] =  '9:30 ';
$time[12] =  '9:45' ;
$time[13] =  '10:00 ';
$time[14] =  '10:15' ;
$time[15] =  '10:30' ;
$time[16] =  '10:45' ;
$time[17] =  '11:00' ;
$time[18] =  '11:15' ;
$time[19] =  '11:45 ';
$time[20] =  '12:00' ;
$time[21] =  '12:30 ';
$time[22] =  '12:45 ';
$time[23] =  '1:00 ';
$time[24] =  '1:15' ;
$time[25] =  '1:30' ;
$time[26] =  '1:45 ';
$time[27] =  '2:00' ;
$time[28] =  '2:15 ';
$time[29] =  '2:30 ';
$time[30] =  '2:45 ';
$time[31] =  '3:00' ;
$time[32] =  '3:15 ';
$time[33] =  '3:30';
$time[34] =  '3:45 ';
$time[35] =  '4:00 ';
$time[36] =  '4:15';
$time[37] =  '4:30 ';
$time[38] =  '4:45';

$conn = new COM('ADODB.Connection') or die('Could not make conn'); 
$conn1 = new COM('ADODB.Connection') or die('Could not make conn'); 
$rs = new COM('ADODB.Recordset') or die('Coult not make rs'); 
$rs1 = new COM('ADODB.Recordset') or die('Coult not make rs'); 
$connstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\InetPub\\wwwroot\\phasecodes.mdb"; 
$v = 0;
$conn->Open($connstring); 
$conn1->Open($connstring);
//first dropdown box
$sql = "SELECT DISTINCT `phase` FROM cost_coding  ORDER BY `phase`"; 

$rs->Open($sql, $conn, 1, 3); 

$phase .= '<select onChange="window.location.href=\'timesheet_form.php?phase=\'+this.value" name="phase">';
	while(!$rs->EOF ) { 

$phase .= '<option name="phase" value="'.str_replace(" ","",$rs->Fields['phase']).'">'. $rs->Fields['phase']. '</option>'; 
	$rs->MoveNext(); 

	}
//second dropdown box
$sql1 = "SELECT `cost_code` FROM cost_coding WHERE `phase` ".$phaseno." ORDER BY `cost_code`"; 

$rs1->Open($sql1, $conn1, 1, 3);
$costcode .= '<select>';
	while(!$rs1->EOF ) { 

$costcode .= '<option name="cost_code'.$v++.'" value"'. $rs1->Fields['cost_code']. '">'. $rs1->Fields['cost_code']. '</option>';

	$rs1->MoveNext(); 

	}
$costcode .= '</select>';



for($i=1; $i<=37; $i++) {


 echo '<tr style="height:20px">';
	echo '<td><input type="text" name="TIME" value="'.$time[$i].'"></td>';
	echo '<td><input type="text" name="PROJECT_NO"></td>';
	echo '<td>';
	echo $phase;
	echo '</td>';
	echo '<td>';
	echo $costcode;
	echo '</td>';
	echo '<td><input type="text" name="Admin"> </td>';
	echo '<td><input type="text" name="Draft"> </td>';
	echo '<td><input type="text" name="Feas_Study"> </td>';
	echo '<td><input type="text" name="Research_Permit"> </td>';
	echo '<td><input type="text" name="ENG"> </td>';
	echo '<td><input type="text" name="JPA"> </td>';
	echo '<td><input type="text" name="Inspect"> </td>';
	echo '<td><input type="text" name="Plan_Lease"> </td>';
	echo '<td><input type="text" name="Project_Mgmt"> </td>';
	echo '<td><input type="text" name="OT"> </td>';
	echo '<td><input type="text" name="DT"> </td>';
	echo '<td><input type="text" name="MU"> </td>';
	echo '<td><input type="text" name="MILES"> </td>';
	echo '<td><input type="text" name="HOLIDAY"> </td>';
	echo '<td><input type="text" name="PTO"> </td>';
	echo '<td><input type="text" name="VACATION"> </td>';
	echo '<td><input type="text" name="UPTO"> </td>';
	echo '<td><input type="text" name="STATUS" style="width=300"> </td>';



}

echo " </td></tr></table>";

$conn->Close();
$rs=null; 
$conn=null; 
echo "<P></P>";
echo "SIGNATURE:___________________________________________________________"
//echo '</form>';
?>

 

No one?

Personally, I have no idea what you're trying to do, even though I read it through :D  ...

 

I am guessing here, so if I'm wrong, sorry ... but it seems like you're trying to populate the 2nd drop down box on the same page depending on what is picked in the first drop down box, correct?

 

If so, I think you'll need to use Javascript for that, as in PHP, nothing is done until the page is changed, such as, you could have a drop down box on the first page and populate a new drop down box on a second page using PHP, but only after the page is submitted...

 

Am I way off base here?

 

Personally, I have no idea what you're trying to do, even though I read it through :D  ...

 

I am guessing here, so if I'm wrong, sorry ... but it seems like you're trying to populate the 2nd drop down box on the same page depending on what is picked in the first drop down box, correct?

 

If so, I think you'll need to use Javascript for that, as in PHP, nothing is done until the page is changed, such as, you could have a drop down box on the first page and populate a new drop down box on a second page using PHP, but only after the page is submitted...

 

Am I way off base here?

 

 

You are right, what I am trying to do also is keep the values on a page refresh for each of the potential drop downboxes and there about 53 of them in each column.  So say I select column 1 dropdown box 1, 2 4, 6 and I select 10 from box 1, 20, from box 2, 30 from box 4, 99 from box 6 and do a java refresh I want the selected values to stick.

 

Mind you that the whole table is created with a loop as are the dropdowns.

Well, one thing you could do is just have the drop down use a quick javascript call the same page as soon as someone selects something in the first box ... the page would refresh (I.e., call itself) and you could pass the variables up to the second box...

 

As for keeping the variable in the first box, I'd try a switch statement or an if/else statement for each option and depending on what option was selected (Which is stored in $box1 variable or something) figure out how to insert "selected" in the option statement, such as

<select name="box1">

<option value="somevalue" <?php echo $is_selected;?> >some value</option>

<option value="somevalue2" <?php echo $is_selected2;?> >some value 2</option>

</select>

 

And then try something like:

if ($box1=="somevalue")

{

$is_selected="selected";

}

ELSE if ($box1=="somevalue2")

{

$is_selected2="selected";

}

ELSE

{

$is_selected3="selected";

};

 

 

Now, as I said, that could get long if you have a lot of options, so you'd have to figure out some way to shorten it, but that should get you in the right direction. You can look up how to change / refresh the page on the selection of a drop down option with javascript, it's pretty small, usually like an onsubmit or onclick or some other function. Not a JS guy myself.

 

Good luck! And if someone has a better idea how he can choose where the 'selected' goes, please do speak up lol :)

 

 

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.