在循环中从php下拉列表中获取值?

在循环中从php下拉列表中获取值?

问题描述:

我想从下拉列表和值索引中获取值。循环每次以不同的长度运行,例如一个时间循环运行5次并创建5行。下次可能是30次,并创建30行。每行有2个下拉列表。请帮助我如何获得每个下拉列表和索引的价值。 这里是代码:在循环中从php下拉列表中获取值?

' 
    <div> 
       <form name="viewdata" method="post" onChange="this.form.submit()"> 
        <table> 
         <tr colspan=2> 
        <td><input type="checkbox" name="BSSE" value="BSSE" >BSSE&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
        <td><input type="checkbox" name="BCS" value="BCS" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBCS&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
        <td><input type="checkbox" name="BSTN" value="BSTN" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbspBSTN&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
       </tr> 
       <tr>      
        <td><input type="checkbox" name="MTH" value="MTH" >MTH&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
        <td><input type="checkbox" name="MGT" value="MGT" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMGT&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
        <td><input type="checkbox" name="EEE" value="EEE" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbspEEE&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
        <td><input type="checkbox" name="CSC" value="CSC" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbspCSC&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
        <td><input type="checkbox" name="HUM" value="HUM" >&nbsp&nbsp&nbsp&nbsp&nbsp&nbspHUM&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp</input></td> 
       </tr> 
       <tr> 
        <td> 
       <button type="submit" class="btn btn-success btn-sm">View</button> 
      </td> 
       </tr> 
        </table> 
       </form> 
      </div> 
      <form method = "post"> 
      <table class="table table-striped table-bordered table-hover" id="dataTables-example"> 

<?php 
$programs_title = array(); 
$programs_id = array(); 


$courses_title = array(); 
$courses_id = array(); 

$classes = array(); 
$time_table = array(); 

$sections_id = array(); 
$faculty_selected_course = array(); 

$allocation1 = array(); 
$allocation2 = array(); 
$course_loop_array = array(); 

if(isset($_POST['BSSE'])) {$programs_title[]=$_POST['BSSE'];} 
if(isset($_POST['BCS'])) {$programs_title[]=$_POST['BCS'];} 
if(isset($_POST['BSTN'])) {$programs_title[]=$_POST['BSTN'];} 

if(isset($_POST['MTH'])) {$courses_id[]=$_POST['MTH'];} 
if(isset($_POST['MGT'])) {$courses_id[]=$_POST['MGT'];} 
if(isset($_POST['EEE'])) {$courses_id[]=$_POST['EEE'];} 
if(isset($_POST['CSC'])) {$courses_id[]=$_POST['CSC'];} 
if(isset($_POST['HUM'])) {$courses_id[]=$_POST['HUM'];} 

$conn = mysql_connect("localhost","root","") or die ("cannot connect"); 
mysql_select_db('dims') or die("cannot connect to database"); 

$sql = "SELECT * FROM `user` WHERE email_id='[email protected]'"; 
$result = mysql_query($sql); 
while($row1 = mysql_fetch_array($result)){ 
     $other_faculty[] = $row1['first_name']; 
} 

foreach ($programs_title as $prog) { 

$sql = "SELECT * FROM program WHERE program_title = '$prog'"; 
$result1 = mysql_query($sql); 

if(is_null($result1)){ 
    echo die(mysql_error()); 
} 

while($row = mysql_fetch_array($result1)){ 
    $programs_id[] = $row['program_id']; 
} 

} 

foreach ($programs_id as $prog_id) { 
$sql = "SELECT * FROM time_table WHERE program_id = '$prog_id'"; 
$result = mysql_query($sql); 

if(is_null($result)){ 
    echo die(mysql_error()); 
} 

while($row = mysql_fetch_array($result)){ 
    if(is_array($sections_id)){ 
     if(!in_array($prog_id."-".$row['section_id'],$sections_id)){ 
     $sections_id[] = $prog_id."-".$row['section_id']; 
    } 
    } 
    else{ 
     $sections_id[] = $prog_id."-".$row['section_id']; 
    } 


} 

} 

$section_loop = 1; 

foreach ($sections_id as $class) { 

    $courses = array(); 

    $split = preg_split('/-/', $class); 

    $program = $split[0]; 
    $section = $split[1]; 

    $sql = "SELECT * FROM time_table WHERE program_id = '$program' AND section_id = '$section'"; 
    $result = mysql_query($sql); 

    if(is_null($result)){ 
    echo die(mysql_error()); 
    } 

    while($row = mysql_fetch_array($result)){ 
     if(is_array($courses)){ 
      if(!in_array($row['course_code'], $courses)){ 
      $courses[] = $row['course_code']; 
      } 
     } 
     else{ 
      $courses[] = $row['course_code']; 
     } 

    } 

    $sql = "SELECT * FROM program WHERE program_id = '$program'"; 
    $result = mysql_query($sql); 

    if(is_null($result)){ 
    echo die(mysql_error()); 
    } 

    while($row = mysql_fetch_array($result)){ 
     $program = $row['program_title']; 
    } 

    $sql = "SELECT * FROM section WHERE section_id = '$section'"; 
    $result = mysql_query($sql); 

    if(is_null($result)){ 
    echo die(mysql_error()); 
    } 

    while($row = mysql_fetch_array($result)){ 
     $section = $row['section_title']; 
    } 

    if(!in_array($program."-".$section, $classes)){ 
     $classes[] = $program."-".$section; 
    } 

    /*foreach ($courses_id as $course) { 
     if(!in_array($program."-".$section."-".$course, $time_table)){ 
     $time_table[] = $program."-".$section."-".$course; 
    } 
    }*/ 
    ?> 

    <thead> 
     <tr class="odd gradeX"> 
      <th colspan=6><?php echo $program."-".$section."<br>"; ?></th> 
     </tr> 

     <tr class="odd gradeX"> 
      <th>#</th> 
      <th>Course Code</th> 
      <th>Course Title</th> 
      <th>Class/Lab</th> 
      <th>Faculty</th> 
      <th>Other Faculty</th> 
     </tr> 
    </thead> 
<?php 
    $course_loop =1; 

    foreach ($courses as $course) { 
     unset($faculty_selected_course); 
      if($course!='JUMMA'){ 
       foreach ($courses_id as $value) { 
        if(stristr($course, $value)){ ?> 
    <tbody> 
<?php 

         $sql = "SELECT * FROM course WHERE course_code='$course'"; 
         $result2 = mysql_query($sql); 

         if(is_null($result2)){ 
          echo die(mysql_error()); 
         } 

         while($row2 = mysql_fetch_array($result2)){ 
          $course_title = $row2['course_title']; 
          $course_credit_hour = $row2['course_credit_hour']; 
         } 

         $sql = "SELECT * FROM faculty_course_choice WHERE course_choice1='$course' OR course_choice2='$course' OR course_choice3='$course'"; 
         $result3 = mysql_query($sql); 

         if(is_null($result3)){ 
          echo die(mysql_error()); 
         } 

         while($row3 = mysql_fetch_array($result3)){ 
          if($course == $row3['course_choice1']){ 
           $course_priority = 1; 
           $user = $row3['user_id']; 
          } 

          elseif($course == $row3['course_choice2']){ 
           $course_priority = 2; 
           $user = $row3['user_id']."<br>"; 
          } 

          elseif($course == $row3['course_choice3']){ 
           $course_priority = 3; 
           $user = $row3['user_id']."<br>"; 
          } 

          $sql = "SELECT * FROM `user` WHERE user_id='$user'"; 
          $result4 = mysql_query($sql); 

          while($row4 = mysql_fetch_array($result4)){ 
           $faculty_selected_course[] = $course_priority."::".$row4['first_name']; 
          } 
         } 
         again: 
         ?> 
         <tr class="odd gradeX"> 
         <th><?php echo $course_loop++; ?></th> 
         <th><?php echo $course; ?></th> 
         <th><?php echo $course_title; ?></th> 
         <th><?php 
         if($course_credit_hour==3){ 
          echo "Class"; 
          $allocation1 [] = $program."-".$section."-".$course."-Class"; 
          $allocation2 [] = $program."-".$section."-".$course."-Class"; 
         } 
         elseif($course_credit_hour==4){ 
          echo "Lab"; 
          $allocation1 [] = $program."-".$section."-".$course."-Lab"; 
          $allocation2 [] = $program."-".$section."-".$course."-Lab"; 
          $course_credit_hour--; ?> 
         </th> 

         <th> 
          <select name= "allocation1[]" class="form-control "> 
           <option>Select Faculty</option> 
           <?php 
           foreach ($faculty_selected_course as $faculty) { ?> 
           <option><?php echo $faculty; ?></option> 
           <?php 
           } 
           ?> 
          </select> 
         </th> 

         <th> 
          <select name= "allocation2[]" class="form-control "> 
           <option>Select Other Faculty</option> 
           <?php 
           foreach ($other_faculty as $otherfaculty) { ?> 
           <option><?php echo $otherfaculty; ?></option> 
           <?php 
           } 
           ?> 
          </select> 
         </th> 

         </tr> 
         <?php goto again; 
         } 
         ?> 
         </th> 

         <th> 
          <select name= "allocation1[]" class="form-control "> 
          <option>Select Faculty</option> 
          <?php 
          foreach ($faculty_selected_course as $faculty) { ?> 
          <option><?php echo $faculty; ?></option> 
          <?php 
          } 
          ?> 
          </select> 
         </th> 

         <th> 
          <select name= "allocation2[]" class="form-control "> 
           <option>Select Other Faculty</option> 
          <?php 
          foreach ($other_faculty as $otherfaculty) { ?> 
          <option><?php echo $otherfaculty; ?></option> 
          <?php 
          } 
          ?> 
          </select> 
         </th> 

         </tr> 

         <?php 
        } 

       } 
      } 
    } 
    $get_index = $program."-".$section."-".$course_loop; 
    $course_loop_array [] = $get_index; 
} 
?> 

         <tr> 
          <th colspan="7" > 
           <button type="submit" class="btn btn-success btn-sm">Allocate</button> 
          </th> 
         </tr> 
         </tbody> 

           </table> 
          </form> 
<?php 

if(isset($_POST['allocation1'])){ 
$output = count($_POST['allocation1']); 
echo $output; 
foreach ($_POST['allocation1'] as $value) { 
    if($value == 'Select Other Faculty' OR $value == 'Select Faculty'){ 

    } 
    else{ 
     echo $value."<br>"; 
    } 

} 
} 
$allocation1 = $_POST['allocation1']; 
foreach ($allocation1 as $value) { 
    echo $value."<br>"; 
} 

foreach ($course_loop_array as $lue) { 
    echo $lue."<br>"; 
} 

?> 


       </div>' 

//循环结束

'

+0

在这个有很多的事情困惑我。首先,

应该只用于表头,不适用于低于头的数据元素。为什么两个选择名称都是同一个东西,为什么它们是数组名称(名称中的[]使得PHP接收到数组类型的POST数据时) - 这是否意味着有多行select元素?为什么前面带有$的PHP变量样式中的选择名称?为什么

不完全知道你是问什么,但代码应该是这个样子有道理:

<th> 
    <select name="first_allocation[<?=$rowId?>]"> 
     <option>Select Faculty</option> 
     <? foreach ($faculties as $faculty) { ?> 
      <option value="<?=$faculty?>"><?=$faculty?></option> 
     <? } ?> 
    </select> 
</th> 
<th> 
    <select name="second_allocation[<?=$rowId?>]"> 
     <option>Select Other Faculty</option> 
     <? foreach ($faculties as $faculty) { ?> 
      <option value="<?=$faculty?>"><?=$faculty?></option> 
     <? } ?> 
    </select> 
</th> 

这样,当用户提交表单时,您会收到类似这样的内容:

$_POST = array(
    'first_allocation' = array(
     232 => 'BCS-IIIB-CSC509-Class', 
     372 => 'BCS-IIIB-CSC509-Class', 
     // ... 
    ), 
    'second_allocation' = array(
     232 => 'BCS-IIIB-CSC509-Class', 
     372 => 'BCS-IIIB-CSC509-Class', 
     // ... 
    ), 
); 

其中232和373是$ rowId的相应值,在外部循环的每次迭代中发生变化。

如果你已经有了选择的院系和要标记它们的使用:

<option value="<?=$faculty?>"<?=($first_allocation[$rowId] === $faculty ? ' selected' : '')?>> 
    <?=$faculty?> 
</option> 
+0

这将工作在循环?我怎么知道“first_allocation”属于哪一行? – Shakeeb

+0

看我的编辑,只是正确地理解你的问题。还是我? –

你应该从name属性值删除美元符号,并为两列使用不同的名称:

<select name="allocation1[]" ... 


<select name="allocation2[]" ... 

然后,当用户提交表单,您可以访问:

$allocations1 = $_POST['allocation1']; 
$allocations2 = $_POST['allocation2']; 

这些数组的索引表示行号减1(因为它从零开始)。

这可能是你所追求的

主文件,也许“mainListOfStuff.php”你可以有这样的事情

<form action="processForm.php" method="post"> 
    <table> 
     <tr> 
      <th>Main faculty members</th> 
      <th>Other factulty members</th> 
     </tr> 
     <!-- Start of your loop for table rows --> 
     <?php 
      // YOUR LOOP FOR THE ROWS OF THE TABLE 
      // My example loop for this explanation: for (var $i = 0; $i < count([SOME LENGTH OF ROWS]); $i++) { 
      // My example start of row: echo "<tr>"; 
     ?> 
       <td> 
        <select name="allocationMain[]" class="form-control mainFaculty"> 
         <option selected value="0">Not from this faculty</option> 
         <?php 
          foreach ($faculty_selected_course as $faculty) { 
           echo "<option value=\"". $faculty ."\">". $faculty ."</option>"; 
          } 
         ?> 
        </select> 
       </td><td> 
        <select name="allocationOther[]" class="form-control otherFaculty"> 
         <option selected value="0">Not from this faculty</option> 
         <?php 
          foreach ($other_faculty as $otherfaculty) { 
           echo "<option value=\"". $otherfaculty ."\">". $otherfaculty ."</option>"; 
          } 
         ?> 
        </select> 
       </td> 
     <!-- End of your loop for table rows --> 
     <?php 
      // My example end of row: echo "<tr>"; 
      } 
     ?> 
    </table> 
</form> 

可以清理上面的页面“mainListOfStuff.php”通过把一些jQuery的说:“不从这个教师”当有人掉期选择框的上面的代码

<script> 
    $(function() { 
     $("select.otherFaculty").on("change keyup", function(){ // If they choose something in a row in the other faculty 
                   // Then change the main select to the first "0" value option 
      $(this).closest("tr").find("select.mainFaculty").val(0); 
     }); 
     $("select.mainFaculty").on("change keyup", function(){ 
      $(this).closest("tr").find("select.otherFaculty").val(0); 
     }); 
    }); 
</script> 

例这里>交换所选择的答案

然后在processForm.php你这样做:

<?php 

    if (!isset($_POST["allocationMain"])) exit(); // If no data was submitted then stop the php script here. 


    $mainAllocations = $_POST["allocationMain"]; // Both of these arrays should be the same length 
    $otherAllocations = $_POST["allocationOther"]; // if the form was submitted properly 

    $chosenAllocations = []; 

    for($i = 0; $i < count($mainAllocations); $i++) { 
     if ($mainAllocations[$i] == 0) { // If they have not selected "Not from this faculty" for the main select column, 
              // then select the value in the other select column 
      $chosenAllocations[$i] = $otherAllocations[$i]; 
     } else { 
      $chosenAllocations[$i] = $mainAllocations[$i]; 
     } 
    } 

    // Use the $chosenAllocations for the rest of your PHP processing 

?> 

不完全知道你是问什么,但代码应该是这个样子有道理:

<th> 
    <select name="first_allocation[<?=$rowId?>]"> 
     <option>Select Faculty</option> 
     <? foreach ($faculties as $faculty) { ?> 
      <option value="<?=$faculty?>"><?=$faculty?></option> 
     <? } ?> 
    </select> 
</th> 
<th> 
    <select name="second_allocation[<?=$rowId?>]"> 
     <option>Select Other Faculty</option> 
     <? foreach ($faculties as $faculty) { ?> 
      <option value="<?=$faculty?>"><?=$faculty?></option> 
     <? } ?> 
    </select> 
</th> 

这样,当用户提交表单时,您会收到类似这样的内容:

$_POST = array(
    'first_allocation' = array(
     232 => 'BCS-IIIB-CSC509-Class', 
     372 => 'BCS-IIIB-CSC509-Class', 
     // ... 
    ), 
    'second_allocation' = array(
     232 => 'BCS-IIIB-CSC509-Class', 
     372 => 'BCS-IIIB-CSC509-Class', 
     // ... 
    ), 
); 

其中232和373是$ rowId的相应值,在外部循环的每次迭代中发生变化。

如果你已经有了选择的院系和要标记它们的使用:

<option value="<?=$faculty?>"<?=($first_allocation[$rowId] === $faculty ? ' selected' : '')?>> 
    <?=$faculty?> 
</option> 
+0

这将工作在循环?我怎么知道“first_allocation”属于哪一行? – Shakeeb

+0

看我的编辑,只是正确地理解你的问题。还是我? –

你应该从name属性值删除美元符号,并为两列使用不同的名称:

<select name="allocation1[]" ... 


<select name="allocation2[]" ... 

然后,当用户提交表单,您可以访问:

$allocations1 = $_POST['allocation1']; 
$allocations2 = $_POST['allocation2']; 

这些数组的索引表示行号减1(因为它从零开始)。

这可能是你所追求的

主文件,也许“mainListOfStuff.php”你可以有这样的事情

<form action="processForm.php" method="post"> 
    <table> 
     <tr> 
      <th>Main faculty members</th> 
      <th>Other factulty members</th> 
     </tr> 
     <!-- Start of your loop for table rows --> 
     <?php 
      // YOUR LOOP FOR THE ROWS OF THE TABLE 
      // My example loop for this explanation: for (var $i = 0; $i < count([SOME LENGTH OF ROWS]); $i++) { 
      // My example start of row: echo "<tr>"; 
     ?> 
       <td> 
        <select name="allocationMain[]" class="form-control mainFaculty"> 
         <option selected value="0">Not from this faculty</option> 
         <?php 
          foreach ($faculty_selected_course as $faculty) { 
           echo "<option value=\"". $faculty ."\">". $faculty ."</option>"; 
          } 
         ?> 
        </select> 
       </td><td> 
        <select name="allocationOther[]" class="form-control otherFaculty"> 
         <option selected value="0">Not from this faculty</option> 
         <?php 
          foreach ($other_faculty as $otherfaculty) { 
           echo "<option value=\"". $otherfaculty ."\">". $otherfaculty ."</option>"; 
          } 
         ?> 
        </select> 
       </td> 
     <!-- End of your loop for table rows --> 
     <?php 
      // My example end of row: echo "<tr>"; 
      } 
     ?> 
    </table> 
</form> 

可以清理上面的页面“mainListOfStuff.php”通过把一些jQuery的说:“不从这个教师”当有人掉期选择框的上面的代码

<script> 
    $(function() { 
     $("select.otherFaculty").on("change keyup", function(){ // If they choose something in a row in the other faculty 
                   // Then change the main select to the first "0" value option 
      $(this).closest("tr").find("select.mainFaculty").val(0); 
     }); 
     $("select.mainFaculty").on("change keyup", function(){ 
      $(this).closest("tr").find("select.otherFaculty").val(0); 
     }); 
    }); 
</script> 

例这里>交换所选择的答案

然后在processForm.php你这样做:

<?php 

    if (!isset($_POST["allocationMain"])) exit(); // If no data was submitted then stop the php script here. 


    $mainAllocations = $_POST["allocationMain"]; // Both of these arrays should be the same length 
    $otherAllocations = $_POST["allocationOther"]; // if the form was submitted properly 

    $chosenAllocations = []; 

    for($i = 0; $i < count($mainAllocations); $i++) { 
     if ($mainAllocations[$i] == 0) { // If they have not selected "Not from this faculty" for the main select column, 
              // then select the value in the other select column 
      $chosenAllocations[$i] = $otherAllocations[$i]; 
     } else { 
      $chosenAllocations[$i] = $mainAllocations[$i]; 
     } 
    } 

    // Use the $chosenAllocations for the rest of your PHP processing 

?>