蓝极冰焰 2024-04-11 00:19 采纳率: 40.7%
浏览 26
已结题

关于多级下拉列表获取数据库值内容的问题

情况说明:
数据库里有一个名为matchs的表,表中有有game、match、mname、mdate、maddress共5个关键字,
主要操作查询的网页名为:reg_match_list.php
我需要在reg_match_list.php页面中,插入3个下拉菜单分别为“game_select”、“match_select”、“mname_select”,需要三者实现联动;
第一个"game_select"下拉菜单的数值,是从数据库matchs表中game字段中获取,并且是唯一值;
当我选择第一个下拉菜单中某个数值后,第二个“match_select"下拉菜单就会匹配数据库中game字段的数据,来调取match字段的数据作为它的下拉菜单数据内容;
当我再选择第二个下单菜单中的数值后,第三个"mname_select"下拉菜单就会匹配数据库中match字段的数据和game字段的数据,来调取相应的mname字段的内容来作为下拉菜单的数据内容。
并且将对应的mname内容的mdate和maddress内容显示在页面上。
以下是我原来的代码,二级菜单无法获取数据,请问如何解决?

<?php require_once('Connections/games.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_games, $games);
$query_game = "SELECT DISTINCT game FROM matchs where state = 1 ORDER by game desc";
$game = mysql_query($query_game, $games) or die(mysql_error());
$row_game = mysql_fetch_assoc($game);
$totalRows_game = mysql_num_rows($game);

$colname_match = "-1";
if (isset($_SESSION['game'])) {
  $colname_match = $_SESSION['game'];
}
mysql_select_db($database_games, $games);
$query_match = sprintf("SELECT * FROM matchs WHERE game = %s", GetSQLValueString($colname_match, "text"));
$match = mysql_query($query_match, $games) or die(mysql_error());
$row_match = mysql_fetch_assoc($match);
$totalRows_match = mysql_num_rows($match);
?>
<?php $_SESSION['game']; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Matches Selector</title>

<form method="post" name="game">
<select id="game-select">
  <option value="请选择">请选择</option>
  <?php
do {  
?>
  <option value="请选择"><?php echo $row_game['game']?></option>
  <?php
} while ($row_game = mysql_fetch_assoc($game));
  $rows = mysql_num_rows($game);
  if($rows > 0) {
      mysql_data_seek($game, 0);
      $row_game = mysql_fetch_assoc($game);
  }
?>
</select>
</form>

<select id="matches-select">
  <?php
do {  
?>
  <option value="请选择"</option>
  <?php
} while ($row_match = mysql_fetch_assoc($match));
  $rows = mysql_num_rows($match);
  if($rows > 0) {
      mysql_data_seek($match, 0);
      $row_match = mysql_fetch_assoc($match);
  }
?>
</select>
<select id="mname-select"></select>
</form>

</body>
</html>
<?php
mysql_free_result($game);

mysql_free_result($match);
?>



  • 写回答

7条回答 默认 最新

  • 心衍 2024-04-11 01:49
    关注

    你想实现级联下拉的效果
    那么第二个下拉框就需要根据第一个下拉框的值去查询数据
    你从session里取数据 可是数据从哪里来的 不存怎么取 (你不往银行卡里存钱就想去银行取钱么)
    所以你的代码第41行的 if (isset($_SESSION['game'])) { 这个if条件是必然不为真的
    那么 现在就需要将你选择的game存到session里 也就是给$_SESSION['game']赋值才能查询到第二个下拉框
    可是在你页面显示出来之前 服务器返回页面数据之前 你的php脚本就已经执行完毕了
    那么 现在想在选择完第一个下拉框之后查询第二个下拉框的数据 就需要再次执行php脚本
    那么如何再次执行php脚本呢 2种方法: 1.刷新浏览器页面 2.网页js代码进行网络请求
    下面分开细说:
    一.刷新浏览器页面当然不是让你按F5刷新 那不相当于重新开始了
    这里说的是 提交表单 将你的game所在的form提交到服务器
    然后你就可以通过$_POST['game']或者$_GET['game']获取到你上一步选择的值
    然后就可以查询第二个下拉框 并显示到页面上
    好 我们开始修改代码 (以你上面提供的代码为基础)
    第57行

    • 1.这里先给form提交方式设置为get方便我们观看效果后面可以改回来
    • 2.这里给form设置一个id方便下面提交操作
    <form method="get" id="game">
    

    第58行 在下拉框的值发生改变的时候触发form表单的提交操作

    <select id="game-select" name="game" onchange="document.getElementById('game').submit();">
    

    第63行 你这里每一个选项的值都设置为请选择 那提交到服务器上怎么区分 去掉

      <option><?php echo $row_game['game']?></option>
    

    先看下效果

    img


    可以看到 我们切换不同的选项的时候地址栏上的参数会发生改变
    并且php代码会重新执行 这时候可以通过$_GET['game']获取到选择的值
    如果form的method设置为post地址栏上就看不到这个变化 但是仍然可以获取到值(通过$_POST['game']变量)
    下面继续修改查询代码
    第79行 与63行一样

      <option><?php echo $row_match['match']?></option>
    

    再看下效果

    img


    可以看到第二个下拉框有数据了
    不过第一个下拉框因为页面刷新 选择的值又不显示了
    第63行 这里在循环的地方判断一下

      <option <?php echo $_GET['game']==$row_game['game']?'selected':''?>><?php echo $row_game['game']?></option>
    

    再看看效果

    img


    完美 第三个下拉框的处理方式亦是如此 这里就不改了 下面是文件改后的全貌

    <?php require_once('Connections/games.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
    
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      }
      return $theValue;
    }
    }
    
    mysql_select_db($database_games, $games);
    $query_game = "SELECT DISTINCT game FROM matchs where state = 1 ORDER by game desc";
    $game = mysql_query($query_game, $games) or die(mysql_error());
    $row_game = mysql_fetch_assoc($game);
    $totalRows_game = mysql_num_rows($game);
    
    $colname_match = "-1";
    if (isset($_GET['game'])) {
      $colname_match = $_GET['game'];
    }
    mysql_select_db($database_games, $games);
    $query_match = sprintf("SELECT * FROM matchs WHERE game = %s", GetSQLValueString($colname_match, "text"));
    $match = mysql_query($query_match, $games) or die(mysql_error());
    $row_match = mysql_fetch_assoc($match);
    $totalRows_match = mysql_num_rows($match);
    ?>
    <?php $_GET['game']; ?>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Matches Selector</title>
    
    <form method="get" id="game">
    <select id="game-select" name="game" onchange="document.getElementById('game').submit();">
      <option value="请选择">请选择</option>
      <?php
    do {
    ?>
      <option <?php echo $_GET['game']==$row_game['game']?'selected':''?>><?php echo $row_game['game']?></option>
      <?php
    } while ($row_game = mysql_fetch_assoc($game));
      $rows = mysql_num_rows($game);
      if($rows > 0) {
          mysql_data_seek($game, 0);
          $row_game = mysql_fetch_assoc($game);
      }
    ?>
    </select>
    </form>
    
    <select id="matches-select" name="match">
      <?php
    do {
    ?>
      <option><?php echo $row_match['match']?></option>
      <?php
    } while ($row_match = mysql_fetch_assoc($match));
      $rows = mysql_num_rows($match);
      if($rows > 0) {
          mysql_data_seek($match, 0);
          $row_match = mysql_fetch_assoc($match);
      }
    ?>
    </select>
    <select id="mname-select" name="mname"></select>
    </form>
    
    </body>
    </html>
    <?php
    mysql_free_result($game);
    
    mysql_free_result($match);
    ?>
    

    不过可以看到每次选择完浏览器似乎刷新了一下 这是因为表单提交了
    但页面毕竟闪烁了一下 强迫症不太友好
    有没有办法不闪烁也不刷新页面呢 当然是有的
    那就是下面第二个方法

    二.通过网页javascript代码进行网络请求
    也叫ajax 实际上与php关系就不大了 主要是js实现
    如果想了解的话 上面的好几位机器人的回答都很详细了都是通过ajax实现的
    就不再补充了

    手敲了半天 如果有帮助 给个采纳?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
    1人已打赏
查看更多回答(6条)

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 创建了问题 4月11日