0

こんにちは、私と一緒にこれを見てくれてありがとう。PHPを使用してMySQLselectステートメントを実行するのはまったく初めてです。そうは言っても、私はSELECTステートメントを実行してドロップダウンリストにデータを入力し、別のSELECTステートメントを実行してHTMLテーブルにデータを入力することができました。(これはロールプレイングゲーム用です)

しかし、これは私が立ち往生している場所3です...

ドロップダウンで選択された値を、テーブルに入力する2番目のselectステートメントの「WHEREracename =」値にして、すべてのデータではなく1つの行のみが返されるようにします。

これがページです: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>"; 

?> 

これが簡単だといいのですが...どうもありがとうございました:)

〜ジャック

4

1 に答える 1

0

これを行う最善の方法は、変数を渡して新しいページをロードする必要がないように、 AJAXを使用することです。

しかし、昔ながらの方法でそれを行う方法は次のとおりです。

ページが 1 つしかなく、選択した値を同じページに渡すと仮定します (ページのリロードが必要です)。

あなたのページが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
}

たとえば、渡された変数をリストから取得するために必要な「if」の「true」内

$var = $_POST['list_name'];

これで、テーブルを埋めるために必要なデータを選択するための 2 番目の変数ができました。

完全なコードは、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>"; 
}
?>

私はあなたのコードを修正し、あなたが始められるように何かを追加しました。試しずに書いたページを読み込もうとしたときにエラーが発生した場合はすみません

幸運を!

于 2012-08-01T14:28:13.280 に答える