2017-12-18 14 views
-1

詳細をテーブルに表示し、ドロップダウンリストに値のリストを表示しようとしています。値は表に表示されますが、値が存在していてもドロップダウンリストには値が表示されません。セッションに保存して印刷すると、上部に表示されます。また、selectの名前を使用する際に問題があります。私はボタンをクリックし、オプションのポスト値を読みたいとき echoで使用する場合のPHPのオプション値の選択

enter image description here

に示すように、それは私にエラーを与えて、ここのコードです:ここで

for($i=0;$i<count($dr_ide);$i=$i+5) 
    { 
     echo "<tr>"; 
     echo "<td> Dr. " . $dr_namee[$i] . "</td>"; 
     echo "<td>" . $sub_namee[$i] . "</td>"; 
     echo "<td>" . $sub_namee[$i+1] . "</td>"; 
     echo "<td>" . $sub_namee[$i+2] . "</td>"; 
     echo "<td>" . $sub_namee[$i+3] . "</td>"; 
     echo "<td>" . $sub_namee[$i+4] . "</td>"; 
     echo "<td> <select name='perr' > 
        <option name='perr' selected=selected>Choose one</option>"; 
        foreach($Names as $name) { 
         echo"<option name='perr' value="; 
          $name ; 
          echo "> "; 
          $_SESSION["tt"]=$name; 
          $name; 
          echo"</option>"; 


        } 
        echo "</select></td>"; 
     //echo "<td>" . $dayse[$i] . "</td>"; 
     //echo "<td>" . $timing[$i] . "</td>"; 
     echo "</tr>"; 
    } 
} 

    echo $_SESSION["tt"]; 

    if(isset($_POST['confirm'])){ 
     echo $_POST['perr']; 
    } 

は私の完全なページですコード。

<html> 
<head> 
<title> Expert System </title> 

<link rel="stylesheet" type="text/css" href="style.css"> 
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'> 
</head> 
<body> 
<div > 
    <table id="cells2" border="0" cellpadding="15" cellspacing="5" font size="6"> 

<tr> 
<th><img id="header2" src="images/Kuwait_University_Logo.jpg" > </th> 
<th>KUWAIT UNIVERSITY</th> 
</tr> 
</table> 
<div class='back1'> 

<table border='1' align='center' id='customers' > 
<?php 
require "init.php"; 
session_start(); 

    global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing; 
    global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge; 


     $query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)"; 
    $result=mysqli_query($con,$query); 
    if ($result->num_rows == 0) // User doesn't exist 
     echo "Subjects doesn't exist!"; 
    else { 
    while($row = mysqli_fetch_array($result)) 
     { 

      $IDs[]=$row['sub_ID']; 
      $Names[]=$row['Name']; 
      //echo $row['Name']; 
     } 
    } 


    $query= "SELECT * FROM test order by dr_id"; 
    $result=mysqli_query($con,$query); 
    if ($result->num_rows == 0) // User doesn't exist 
     echo "Subjects doesn't exist!"; 
    else { echo " 
     <tr> 
      <th>Professor Name</th> 
      <th>First Choice</th> 
      <th>Second Choice</th> 
      <th>Third Choice</th> 
      <th>Fourth Choice</th> 
      <th>Fifth Choice</th> 
      <th>Update Subject</th> 

     </tr>"; 

     $r=0; 
     $f=0; 
     while($row = mysqli_fetch_array($result)) 
     { 
      $dr_ide[$f]=$row['dr_id']; 
      $dr_namee[$f]=$row['dr_name']; 
      $sub_namee[$f]=$row['sub_name']; 
      $dayse[$f]=$row['days']; 
      $timinge[$f]=$row['timing']; 
      $fave[$f]=$row['fav']; 

      //echo "<tr>"; 
      //echo "<td> Dr. " . $dr_namee[$f] . "</td>"; 
      //echo "<td>" . $sub_namee[$f] . "</td>"; 
      //echo "<td>" . $fave[$f] . "</td>"; 
      //echo "<td>" . $dayse[$f] . "</td>"; 
      //echo "</tr>"; 
      //$r++; 
      $f++; 
     } 
     //for($i=0;$i<count($Names);$i=$i+5) 
     //{ 
     for($i=0;$i<count($dr_ide);$i=$i+5) 
     { 
      echo "<tr>"; 
      echo "<td> Dr. " . $dr_namee[$i] . "</td>"; 
      echo "<td>" . $sub_namee[$i] . "</td>"; 
      echo "<td>" . $sub_namee[$i+1] . "</td>"; 
      echo "<td>" . $sub_namee[$i+2] . "</td>"; 
      echo "<td>" . $sub_namee[$i+3] . "</td>"; 
      echo "<td>" . $sub_namee[$i+4] . "</td>"; 

      echo "<td> <select name='perr' > 
         <option name='perr' selected=selected>Choose one</option>"; 
         foreach($Names as $name) { 
          echo"<option name='perr' value='$name'>$name</option>"; 

         }        
         echo "</select></td>"; 
      //echo "<td>" . $dayse[$i] . "</td>"; 
      //echo "<td>" . $timing[$i] . "</td>"; 
      echo "</tr>"; 
     } 
    } 

    echo $_SESSION["tt"]; 

    if(isset($_POST['confirm'])){ 
     echo $_POST['perr']; 
    } 

?> 
</table> 
<form method='post' action='edit_subjects.php'> 
      <input ID="btn2" name="confirm" type="submit" value="Home"> 

      </form> 
</div> 
</body> 
</html> 
+1

注:mysqli' 'へのオブジェクト指向のインターフェイスは、大幅に少ない冗長なコードが読みやすく、監査作り、とあるが古いmysql_queryインターフェースと簡単に混同しないでください。手続き型スタイルに多額の投資をする前に、それを切り換える価値があります。例: '$ db = new mysqli(...)'と '$ db-> prepare(" ... ")'手続き型インタフェースはmysqli' APIが導入されたPHP 4時代のアーティファクトであり、コード。 – tadman

+0

あなたは正しいですが、私はこのようにすべてのプロジェクトを構築しました。配送に2日間かかるので、別の方法で再構築することはできません。この作業で私が助けてくれるなら、私は幸せになれます –

答えて

1

<options>からname='perr'を削除し、以下のようなforeach()を変更: -

foreach($Names as $name) { 
    echo"<option value='$name'>$name</option>"; 
} 

次のようなコードで変更できます: -

echo "<form method='post' action='edit_subjects.php'>"; //add before while 
     while($row = mysqli_fetch_array($result)) 
     { 
      $dr_ide[$f]=$row['dr_id']; 
      $dr_namee[$f]=$row['dr_name']; 
      $sub_namee[$f]=$row['sub_name']; 
      $dayse[$f]=$row['days']; 
      $timinge[$f]=$row['timing']; 
      $fave[$f]=$row['fav']; 

      $f++; 
     } 
     for($i=0;$i<count($dr_ide);$i=$i+5) 
     { 
      $dr_nam = $dr_namee[$i]; 
      echo "<tr>"; 
      echo "<td> Dr. " . $dr_namee[$i] . "</td>"; 
      echo "<td>" . $sub_namee[$i] . "</td>"; 
      echo "<td>" . $sub_namee[$i+1] . "</td>"; 
      echo "<td>" . $sub_namee[$i+2] . "</td>"; 
      echo "<td>" . $sub_namee[$i+3] . "</td>"; 
      echo "<td>" . $sub_namee[$i+4] . "</td>"; 

      echo "<td><select name='perr[$dr_nam]' > 
         <option name='perr' selected=selected>Choose one</option>"; 
         foreach($Names as $name) { 
          echo"<option name='perr' value='$name'>$name</option>"; 

         }        
         echo "</select></td>"; 
      echo "</tr>"; 
     } 
     echo"<input ID='btn2' name='confirm' type='submit' value='Home'></form>"; //add just after while code ended 
+1

これは最初の問題を解決し、値が表示されますが、投稿値を読み取るときにエラー "未定義インデックス" –

+1

あなたはPHPの王様です、あなたのおかげで助けになりました –

+0

@SalehRefaai :) –

0

あなたはこのような名前エコーする必要があります。

foreach($Names as $name) { 
    echo "<option value="; 
    echo $name ; 
    echo "> "; 
    $_SESSION["tt"]=$name; 
    echo $name; 
    echo"</option>"; 
} 

を、フォーム内のテーブルを置く:

<html> 
<head> 
<title> Expert System </title> 

<link rel="stylesheet" type="text/css" href="style.css"> 
<link href='https://fonts.googleapis.com/css?family=Open+Sans:700,600' rel='stylesheet' type='text/css'> 
</head> 
<body> 
<div > 
    <table id="cells2" border="0" cellpadding="15" cellspacing="5" font size="6"> 

<tr> 
<th><img id="header2" src="images/Kuwait_University_Logo.jpg" > </th> 
<th>KUWAIT UNIVERSITY</th> 
</tr> 
</table> 
<div class='back1'> 
<form method='post' action='edit_subjects.php'> 
<table border='1' align='center' id='customers' > 
<?php 
require "init.php"; 
session_start(); 

    global $con,$users,$dr_id,$sub_id,$dr_name,$sub_name,$days,$fav,$timing; 
    global $dr_ide,$dr_namee,$sub_namee,$dayse,$fave,$timinge; 


     $query= "SELECT * FROM subjects_current where subjects_current.sub_ID NOT IN (SELECT test.subject_id from test)"; 
    $result=mysqli_query($con,$query); 
    if ($result->num_rows == 0) // User doesn't exist 
     echo "Subjects doesn't exist!"; 
    else { 
    while($row = mysqli_fetch_array($result)) 
     { 

      $IDs[]=$row['sub_ID']; 
      $Names[]=$row['Name']; 
      //echo $row['Name']; 
     } 
    } 


    $query= "SELECT * FROM test order by dr_id"; 
    $result=mysqli_query($con,$query); 
    if ($result->num_rows == 0) // User doesn't exist 
     echo "Subjects doesn't exist!"; 
    else { echo " 
     <tr> 
      <th>Professor Name</th> 
      <th>First Choice</th> 
      <th>Second Choice</th> 
      <th>Third Choice</th> 
      <th>Fourth Choice</th> 
      <th>Fifth Choice</th> 
      <th>Update Subject</th> 

     </tr>"; 

     $r=0; 
     $f=0; 
     while($row = mysqli_fetch_array($result)) 
     { 
      $dr_ide[$f]=$row['dr_id']; 
      $dr_namee[$f]=$row['dr_name']; 
      $sub_namee[$f]=$row['sub_name']; 
      $dayse[$f]=$row['days']; 
      $timinge[$f]=$row['timing']; 
      $fave[$f]=$row['fav']; 

      //echo "<tr>"; 
      //echo "<td> Dr. " . $dr_namee[$f] . "</td>"; 
      //echo "<td>" . $sub_namee[$f] . "</td>"; 
      //echo "<td>" . $fave[$f] . "</td>"; 
      //echo "<td>" . $dayse[$f] . "</td>"; 
      //echo "</tr>"; 
      //$r++; 
      $f++; 
     } 
     //for($i=0;$i<count($Names);$i=$i+5) 
     //{ 
     for($i=0;$i<count($dr_ide);$i=$i+5) 
     { 
      echo "<tr>"; 
      echo "<td> Dr. " . $dr_namee[$i] . "</td>"; 
      echo "<td>" . $sub_namee[$i] . "</td>"; 
      echo "<td>" . $sub_namee[$i+1] . "</td>"; 
      echo "<td>" . $sub_namee[$i+2] . "</td>"; 
      echo "<td>" . $sub_namee[$i+3] . "</td>"; 
      echo "<td>" . $sub_namee[$i+4] . "</td>"; 

      echo "<td> <select name='perr' > 
         <option selected=selected>Choose one</option>"; 
         foreach($Names as $name) { 
          echo"<option value='$name'>$name</option>"; 

         }        
         echo "</select></td>"; 
      //echo "<td>" . $dayse[$i] . "</td>"; 
      //echo "<td>" . $timing[$i] . "</td>"; 
      echo "</tr>"; 
     } 
    } 

    echo $_SESSION["tt"]; 

    if(isset($_POST['confirm'])){ 
     echo $_POST['perr']; 
    } 

?> 
</table> 

      <input ID="btn2" name="confirm" type="submit" value="Home"> 

      </form> 
</div> 
</body> 
</html> 
+0

これが最初に解決します問題が発生し、値が表示されますが、投稿値を読みたいときは "undefined index"というエラーが表示されます –

+0

@SalehRefaaiここで "confirm"という名前のフィールドを定義していますか? – claudio

+0

残りのコードがありますが、名前を確認してボタンを押したときと、echo $ _POST ['perr']を削除したときです。 echo 'hi';それは印刷されます。 –

1
echo "<td> <select name='perr' > 
     <option name='perr' selected='selected'> Choose one </option>"; 
     foreach($Names as $name) { 
     echo"<option name='perr' value='". $name ."'> ". $name ." </option>"; 
     } 
echo "</select></td>"; 
関連する問題