2012-08-01 77 views
0

嗨,謝謝你看我。我完全是使用PHP運行MySQL select語句的新手段。話雖如此,我設法運行一個SELECT語句來填充一個下拉列表......和另一個SELECT語句來填充一個HTML表格。 (這是一個角色扮演遊戲)將下拉列表值傳遞給同一頁上的另一個SELECT語句

但是,這是我where3卡住...

我想對於下拉列表選擇的值是「WHERE racename =」,在第二個SELECT語句值填充該表使得只返回一行而不是所有的數據。

這裏的頁面:http://www.gamehermit.com/racechoice.php

這裏是我到目前爲止的代碼:

<?php 

// Make a MySQL Connection 

mysql_connect("localhost", "db_username", "password") or die(mysql_error()); 
mysql_select_db("db_name") or die(mysql_error()); 

$query="SELECT * FROM Races"; 
$result = mysql_query($query); 
echo "<select name=racename>"; 
while($nt=mysql_fetch_array($result)) 
{ 
if ($nt[racename]==$_POST["racename"]) 
$selected="selected"; 
else 
$selected=""; 
echo "<option ".$selected."value=$nt[racename]>$nt[racename]</option>"; 
} 
echo "</select>"; 
echo "<br />"; 

// Get all the data from the "Race" table and create table 

$result2 = mysql_query("SELECT * FROM Races") 
or die(mysql_error()); 

echo "<table border='1'>"; 
echo "<tr> <th>Race Name</th> <th>Might Modifier</th> <th>Valor Modifier</th>   <th>Deftness 

Modifier</th> <th>Insight Modifier</th> <th>Dweomer Modifier</th> </tr>"; 

// keeps getting the next row until there are no more to get 

while($row = mysql_fetch_array($result2)) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['racename']; 
echo "</td><td>"; 
echo $row['modmight']; 
echo "</td><td>"; 
echo $row['modvalor']; 
echo "</td><td>"; 
echo $row['moddeftness']; 
echo "</td><td>"; 
echo $row['modinsight']; 
echo "</td><td>"; 
echo $row['moddweomer']; 
echo "</td></tr>"; 
} 
echo "</table>"; 

?> 

我希望這是簡單...非常感謝:)

〜傑克

+0

[Chained Select Boxes jquery php]可能的重複(http://stackoverflow.com/questions/9724087/chained-select-boxes-jquery-php) – 2012-08-01 14:12:35

回答

0

的最好的方法是使用AJAX,因此您不需要傳遞變量並加載新頁面。

但在這裏就是你可以用老式的方法做到這一點:

假設你將有隻有一個頁面,你會選擇的值傳遞到同一個頁面(需要重新加載頁面)

讓我們說你的網頁是game.php

你需要在這個頁面包括「跳躍菜單」有一個提交按鈕,用戶的頭部從列表中選擇

後的東西被壓你的網頁你需要檢查是否按鈕是使用

if(isset($_POST['button_name'])) { 

// button pressed.. perform next step and select your new data to fill the table 

} else { 
// nothing pressed and nothing to be performed load the page normally 
} 

裏面的「真」的「如果」你在這裏需要從列表中得到傳遞的變量,例如

$var = $_POST['list_name']; 

所以現在你有第二個變量的壓選擇所需的數據來填充表格。

完整的代碼應類似於以下,game.php東西:

<?php 
if(!isset($_POST['go_button'])){ //option not selected display list to choose from 
// Make a MySQL Connection 

mysql_connect("localhost", "db_username", "password") or die(mysql_error()); 
mysql_select_db("db_name") or die(mysql_error()); 

$query="SELECT * FROM Races"; 
$result = mysql_query($query); 
$num = mysql_numrows($result); 
?> 

<script type="text/javascript"> 
function MM_jumpMenuGo(objId,targ,restore){ //v9.0 
    var selObj = null; with (document) { 
    if (getElementById) selObj = getElementById(objId); 
    if (selObj) eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); 
    if (restore) selObj.selectedIndex=0; } 
} 
</script> 

<form name="form" id="form" action="game.php" method="post"> 
    <select name="jumpMenu" id="jumpMenu"> 
    <?php $i=0; while($i<$num) { ?> 
    <option value="<?php echo mysql_result($result,$i,'racename_field_value'); ?>"><?php echo mysql_result($result,$i,'racename'); ?></option> 
    <?php } ?> 
    </select> 
    <input type="button" name="go_button" id= "go_button" value="Go" onClick="MM_jumpMenuGo('jumpMenu','parent',0)"> 
</form> 

<?php 
echo "<br />"; 
} else { //option selected to get the variable and use it to select data from DB 

$var= $_POST['jumpMenu']; 

// Get all the data from the "Race" table and create table 

$result2 = mysql_query("SELECT * FROM Races WHERE racename='$var'") 
or die(mysql_error()); 

echo "<table border='1'>"; 
echo "<tr> <th>Race Name</th> <th>Might Modifier</th> <th>Valor Modifier</th>   <th>Deftness 

Modifier</th> <th>Insight Modifier</th> <th>Dweomer Modifier</th> </tr>"; 

// keeps getting the next row until there are no more to get 

while($row = mysql_fetch_array($result2)) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['racename']; 
echo "</td><td>"; 
echo $row['modmight']; 
echo "</td><td>"; 
echo $row['modvalor']; 
echo "</td><td>"; 
echo $row['moddeftness']; 
echo "</td><td>"; 
echo $row['modinsight']; 
echo "</td><td>"; 
echo $row['moddweomer']; 
echo "</td></tr>"; 
} 
echo "</table>"; 
} 
?> 

我修改你的代碼,並添加一些讓你開始,原諒我,如果有嘗試加載時的錯誤我寫的頁面沒有嘗試它

祝你好運!

相關問題