2016-09-08 19 views
0

好吧,我只是將它更改爲$ _POST,它現在正在工作。我不確定這是否是捷徑。至少現在工作。如果你想幫助我,你可以幫我縮小代碼。謝謝從下拉菜單中獲取相同ID的整行

<?php 

$conn = new mysqli('localhost', 'root', 'jared17', 'hbadb') 
or die ('Cannot connect to db'); 

$result = $conn->query("select * from english"); 

echo "<html>"; 
echo "<body>"; 
echo "<form method = POST>"; 
echo "<select name = 'Students'>"; 

while ($row = $result->fetch_assoc()) { 

      $LRN = $row['LRN']; 
      $Last = $row['Last_Name']; 
      $First = $row['First_Name']; 
      $Lvl = $row['Level']; 
      $Q1 = $row['Q1']; 
      $Q2 = $row['Q2']; 
      $Q3 = $row['Q3']; 
      $Q4 = $row['Q4']; 
      $Final = $row['FINAL']; 
      echo '<option value="'.$LRN.'|'.$Last.', '.$First.'|'.$Lvl.'|'.$Q1.'|'.$Q2.'|'.$Q3.'|'.$Q4.'|'.  $Final.'">'.$Last.', '.$First.'</option>'; 

} 

echo "</select>"; 
echo "<input type='submit' name='submit' value='Show'>"; 
echo "</form>"; 

$show = $_POST['Students']; 
     $show_explode = explode('|', $show); 

    echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>"; 
    echo "<tr><td>". $show_explode[0]."</td><td>". $show_explode[1]."</td><td>". $show_explode[2]."</td><td>". $show_explode[3]."</td><td>". $show_explode[4]."</td><td>". $show_explode[5]."</td><td>". $show_explode[6]."</td><td>". $show_explode[7]."</td></tr>"; 

echo "</table>"; 




echo "</body>"; 
echo "</html>"; 
?> 
+1

在分配變量之前,不需要取消設置變量。 – Barmar

+0

*我想要做的是獲得下拉菜單*要求或問題的相同ID的整行? – devpro

+0

不要把所有的信息都放在'

回答

0

不要把所有的細節都放在這樣的選項值中。只需將該ID放入該值即可。

echo "<select name = 'Students'>"; 
while ($row = $result->fetch_assoc()) { 
    $LRN = $row['LRN']; 
    $Last = $row['Last_Name']; 
    $First = $row['First_Name']; 
    echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>'; 
} 
echo "</select>"; 

然後在提交表單時在數據庫中查找它。

if (isset($_POST['Students'])) { 
    $lrn = $_POST['Students']; 
    $stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?"); 
    $stmt->bind_param('i', $lrn); 
    $stmt->execute(); 
    $stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final); 
    $stmt->fetch(); 
    echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>"; 
    echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table"; 
} 
+0

編輯了代碼。 – jaredpianist

+0

非常非常非常感謝你!這使我的代碼整齊。謝謝!現在,最後,當我點擊提交時,如何讓下拉菜單保持不變?它在提交按鈕後回到選項1。再次感謝! – jaredpianist

+0

測試'$ LRN == $ _POST ['Students']',然後將'selected'屬性添加到選項中。 – Barmar

0

您可以使用$的foreach最小的代碼時處理陣列。代碼如下

if(isset($_POST['submit'])){ 
// after post a form ur code goes here 
$show = $_POST['Students']; $show_explode = explode('|', $show); 
echo "<table><tr> 
<th>LRN</th> 
<th>Name</th> 
<th>Level</th> 
<th>Q1</th> 
<th>Q2</th> 
<th>Q3</th> 
<th>Q4</th> 
<th>Final</th> 
</tr>"; 
echo "<tr>"; 
foreach($show_explode as $value){ 
echo "<td>".$value."</td>"; 
} 
echo "</tr></table> 
} 
相關問題